UrlInitializer.java

  1. /**
  2.  *
  3.  * Copyright 2014-2018 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.initializer;

  18. import java.io.InputStream;
  19. import java.net.URI;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.logging.Level;
  23. import java.util.logging.Logger;

  24. import org.jivesoftware.smack.SmackInitialization;
  25. import org.jivesoftware.smack.provider.ProviderFileLoader;
  26. import org.jivesoftware.smack.provider.ProviderManager;
  27. import org.jivesoftware.smack.util.CloseableUtil;
  28. import org.jivesoftware.smack.util.FileUtils;

  29. /**
  30.  * Loads the provider file defined by the URL returned by {@link #getProvidersUri()} and the generic
  31.  * smack configuration file returned {@link #getConfigUri()}.
  32.  *
  33.  * @author Florian Schmaus
  34.  */
  35. public abstract class UrlInitializer implements SmackInitializer {
  36.     private static final Logger LOGGER = Logger.getLogger(UrlInitializer.class.getName());

  37.     @Override
  38.     public List<Exception> initialize() {
  39.         InputStream is = null;
  40.         final ClassLoader classLoader = this.getClass().getClassLoader();
  41.         final List<Exception> exceptions = new LinkedList<Exception>();
  42.         final String providerUriString = getProvidersUri();
  43.         if (providerUriString != null) {
  44.             try {
  45.                 final URI providerUri = URI.create(providerUriString);
  46.                 is = FileUtils.getStreamForUri(providerUri, classLoader);

  47.                 LOGGER.log(Level.FINE, "Loading providers for providerUri [" + providerUri + "]");
  48.                 ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
  49.                 ProviderManager.addLoader(pfl);
  50.                 exceptions.addAll(pfl.getLoadingExceptions());
  51.             }
  52.             catch (Exception e) {
  53.                 LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUriString, e);
  54.                 exceptions.add(e);
  55.             } finally {
  56.                 maybeClose(is);
  57.             }
  58.         }
  59.         final String configUriString = getConfigUri();
  60.         if (configUriString != null) {
  61.             try {
  62.                 final URI configUri = URI.create(configUriString);
  63.                 is = FileUtils.getStreamForUri(configUri, classLoader);
  64.                 SmackInitialization.processConfigFile(is, exceptions, classLoader);
  65.             }
  66.             catch (Exception e) {
  67.                 exceptions.add(e);
  68.             } finally {
  69.                 maybeClose(is);
  70.             }
  71.         }
  72.         return exceptions;
  73.     }

  74.     protected String getProvidersUri() {
  75.         return null;
  76.     }

  77.     protected String getConfigUri() {
  78.         return null;
  79.     }

  80.     private static void maybeClose(InputStream is) {
  81.         CloseableUtil.maybeClose(is, LOGGER);
  82.     }
  83. }