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.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 #getProvidersUrl()} and the generic 033 * smack configuration file returned {@link #getConfigUrl()}. 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 providerUrl = getProvidersUrl(); 046 if (providerUrl != null) { 047 try { 048 is = FileUtils.getStreamForUrl(providerUrl, classLoader); 049 050 if (is != null) { 051 LOGGER.log(Level.FINE, "Loading providers for providerUrl [" + providerUrl 052 + "]"); 053 ProviderFileLoader pfl = new ProviderFileLoader(is, classLoader); 054 ProviderManager.addLoader(pfl); 055 exceptions.addAll(pfl.getLoadingExceptions()); 056 } 057 else { 058 LOGGER.log(Level.WARNING, "No input stream created for " + providerUrl); 059 exceptions.add(new IOException("No input stream created for " + providerUrl)); 060 } 061 } 062 catch (Exception e) { 063 LOGGER.log(Level.SEVERE, "Error trying to load provider file " + providerUrl, e); 064 exceptions.add(e); 065 } 066 } 067 final String configUrl = getConfigUrl(); 068 if (configUrl != null) { 069 try { 070 is = FileUtils.getStreamForUrl(configUrl, classLoader); 071 SmackInitialization.processConfigFile(is, exceptions, classLoader); 072 } 073 catch (Exception e) { 074 exceptions.add(e); 075 } 076 } 077 return exceptions; 078 } 079 080 protected String getProvidersUrl() { 081 return null; 082 } 083 084 protected String getConfigUrl() { 085 return null; 086 } 087}