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