UrlInitializer.java

  1. /**
  2.  *
  3.  * Copyright 2014 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.IOException;
  19. import java.io.InputStream;
  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.FileUtils;

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

  36.     @Override
  37.     public List<Exception> initialize() {
  38.         InputStream is;
  39.         final ClassLoader classLoader = this.getClass().getClassLoader();
  40.         final List<Exception> exceptions = new LinkedList<Exception>();
  41.         final String providerUrl = getProvidersUrl();
  42.         if (providerUrl != null) {
  43.             try {
  44.                 is = FileUtils.getStreamForUrl(providerUrl, classLoader);

  45.                 if (is != null) {
  46.                     LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl
  47.                                     + "]");
  48.                     ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
  49.                     ProviderManager.addLoader(pfl);
  50.                     exceptions.addAll(pfl.getLoadingExceptions());
  51.                 }
  52.                 else {
  53.                     LOGGER.log(Level.WARNING, "No input stream created for " + providerUrl);
  54.                     exceptions.add(new IOException("No input stream created for " + providerUrl));
  55.                 }
  56.             }
  57.             catch (Exception e) {
  58.                 LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUrl, e);
  59.                 exceptions.add(e);
  60.             }
  61.         }
  62.         final String configUrl = getConfigUrl();
  63.         if (configUrl != null) {
  64.             try {
  65.                 is = FileUtils.getStreamForUrl(configUrl, classLoader);
  66.                 SmackInitialization.processConfigFile(is, exceptions, classLoader);
  67.             }
  68.             catch (Exception e) {
  69.                 exceptions.add(e);
  70.             }
  71.         }
  72.         return exceptions;
  73.     }

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

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