001/**
002 *
003 * Copyright 2014-2018 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.InputStream;
020import java.net.URI;
021import java.util.LinkedList;
022import java.util.List;
023import java.util.logging.Level;
024import java.util.logging.Logger;
025
026import org.jivesoftware.smack.SmackInitialization;
027import org.jivesoftware.smack.provider.ProviderFileLoader;
028import org.jivesoftware.smack.provider.ProviderManager;
029import org.jivesoftware.smack.util.CloseableUtil;
030import org.jivesoftware.smack.util.FileUtils;
031
032/**
033 * Loads the provider file defined by the URL returned by {@link #getProvidersUri()} and the generic
034 * smack configuration file returned {@link #getConfigUri()}.
035 *
036 * @author Florian Schmaus
037 */
038public abstract class UrlInitializer implements SmackInitializer {
039    private static final Logger LOGGER = Logger.getLogger(UrlInitializer.class.getName());
040
041    @Override
042    public List<Exception> initialize() {
043        InputStream is = null;
044        final ClassLoader classLoader = this.getClass().getClassLoader();
045        final List<Exception> exceptions = new LinkedList<Exception>();
046        final String providerUriString = getProvidersUri();
047        if (providerUriString != null) {
048            try {
049                final URI providerUri = URI.create(providerUriString);
050                is = FileUtils.getStreamForUri(providerUri, classLoader);
051
052                LOGGER.log(Level.FINE, "Loading providers for providerUri [" + providerUri + "]");
053                ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader);
054                ProviderManager.addLoader(pfl);
055                exceptions.addAll(pfl.getLoadingExceptions());
056            }
057            catch (Exception e) {
058                LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUriString, e);
059                exceptions.add(e);
060            } finally {
061                maybeClose(is);
062            }
063        }
064        final String configUriString = getConfigUri();
065        if (configUriString != null) {
066            try {
067                final URI configUri = URI.create(configUriString);
068                is = FileUtils.getStreamForUri(configUri, classLoader);
069                SmackInitialization.processConfigFile(is, exceptions, classLoader);
070            }
071            catch (Exception e) {
072                exceptions.add(e);
073            } finally {
074                maybeClose(is);
075            }
076        }
077        return exceptions;
078    }
079
080    protected String getProvidersUri() {
081        return null;
082    }
083
084    protected String getConfigUri() {
085        return null;
086    }
087
088    private static void maybeClose(InputStream is) {
089        CloseableUtil.maybeClose(is, LOGGER);
090    }
091}