001/**
002 *
003 * Copyright 2014 Florian Schmaus
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.jivesoftware.smack.initializer;
018
019import java.io.IOException;
020import java.io.InputStream;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025
026import org.jivesoftware.smack.SmackConfiguration;
027import org.jivesoftware.smack.provider.ProviderFileLoader;
028import org.jivesoftware.smack.provider.ProviderManager;
029import org.jivesoftware.smack.util.FileUtils;
030
031/**
032 * Loads the provider file defined by the URL returned by {@link #getProvidersUrl()} and the generic
033 * smack configuration file returned {@link #getConfigUrl()}.
034 * 
035 * @author Florian Schmaus
036 */
037public abstract class UrlInitializer extends SmackAndOsgiInitializer {
038    private static final Logger LOGGER = Logger.getLogger(UrlInitializer.class.getName());
039
040    @Override
041    public List<Exception> initialize() {
042        return initialize(this.getClass().getClassLoader());
043    }
044
045    @Override
046    public List<Exception> initialize(ClassLoader classLoader) {
047        InputStream is;
048        final List<Exception> exceptions = new LinkedList<Exception>();
049        final String providerUrl = getProvidersUrl();
050        if (providerUrl != null) {
051            try {
052                is = FileUtils.getStreamForUrl(providerUrl, classLoader);
053
054                if (is != null) {
055                    LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl
056                                    + "]");
057                    ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
058                    ProviderManager.addLoader(pfl);
059                    exceptions.addAll(pfl.getLoadingExceptions());
060                }
061                else {
062                    LOGGER.log(Level.WARNING, "No input stream created for " + providerUrl);
063                    exceptions.add(new IOException("No input stream created for " + providerUrl));
064                }
065            }
066            catch (Exception e) {
067                LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUrl, e);
068                exceptions.add(e);
069            }
070        }
071        final String configUrl = getConfigUrl();
072        if (configUrl != null) {
073            try {
074                is = FileUtils.getStreamForUrl(configUrl, classLoader);
075                SmackConfiguration.processConfigFile(is, exceptions, classLoader);
076            }
077            catch (Exception e) {
078                exceptions.add(e);
079            }
080        }
081        return exceptions;
082    }
083
084    protected String getProvidersUrl() {
085        return null;
086    }
087
088    protected String getConfigUrl() {
089        return null;
090    }
091}