001/** 002 * 003 * Copyright the original author or authors 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.provider; 018 019import java.io.InputStream; 020import java.util.Collection; 021import java.util.Collections; 022import java.util.LinkedList; 023import java.util.List; 024import java.util.logging.Level; 025import java.util.logging.Logger; 026 027import org.jivesoftware.smack.packet.IQ; 028import org.jivesoftware.smack.packet.ExtensionElement; 029import org.xmlpull.v1.XmlPullParserFactory; 030import org.xmlpull.v1.XmlPullParser; 031 032/** 033 * Loads the {@link IQProvider} and {@link ExtensionElementProvider} information from a standard provider file in preparation 034 * for loading into the {@link ProviderManager}. 035 * 036 * @author Robin Collier 037 * 038 */ 039public class ProviderFileLoader implements ProviderLoader { 040 private static final Logger LOGGER = Logger.getLogger(ProviderFileLoader.class.getName()); 041 042 private final Collection<IQProviderInfo> iqProviders = new LinkedList<IQProviderInfo>(); 043 private final Collection<ExtensionProviderInfo> extProviders = new LinkedList<ExtensionProviderInfo>(); 044 private final Collection<StreamFeatureProviderInfo> sfProviders = new LinkedList<StreamFeatureProviderInfo>(); 045 046 private List<Exception> exceptions = new LinkedList<Exception>(); 047 048 public ProviderFileLoader(InputStream providerStream) { 049 this(providerStream, ProviderFileLoader.class.getClassLoader()); 050 } 051 052 @SuppressWarnings("unchecked") 053 public ProviderFileLoader(InputStream providerStream, ClassLoader classLoader) { 054 // Load processing providers. 055 try { 056 XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); 057 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 058 parser.setInput(providerStream, "UTF-8"); 059 int eventType = parser.getEventType(); 060 do { 061 if (eventType == XmlPullParser.START_TAG) { 062 final String typeName = parser.getName(); 063 064 try { 065 if (!"smackProviders".equals(typeName)) { 066 parser.next(); 067 parser.next(); 068 String elementName = parser.nextText(); 069 parser.next(); 070 parser.next(); 071 String namespace = parser.nextText(); 072 parser.next(); 073 parser.next(); 074 String className = parser.nextText(); 075 076 try { 077 final Class<?> provider = classLoader.loadClass(className); 078 switch (typeName) { 079 case "iqProvider": 080 // Attempt to load the provider class and then create 081 // a new instance if it's an IQProvider. Otherwise, if it's 082 // an IQ class, add the class object itself, then we'll use 083 // reflection later to create instances of the class. 084 // Add the provider to the map. 085 if (IQProvider.class.isAssignableFrom(provider)) { 086 IQProvider<IQ> iqProvider = (IQProvider<IQ>) provider.getConstructor().newInstance(); 087 iqProviders.add(new IQProviderInfo(elementName, namespace, iqProvider)); 088 } 089 else { 090 exceptions.add(new IllegalArgumentException(className + " is not a IQProvider")); 091 } 092 break; 093 case "extensionProvider": 094 // Attempt to load the provider class and then create 095 // a new instance if it's an ExtensionProvider. Otherwise, if it's 096 // a PacketExtension, add the class object itself and 097 // then we'll use reflection later to create instances 098 // of the class. 099 if (ExtensionElementProvider.class.isAssignableFrom(provider)) { 100 ExtensionElementProvider<ExtensionElement> extensionElementProvider = (ExtensionElementProvider<ExtensionElement>) provider.getConstructor().newInstance(); 101 extProviders.add(new ExtensionProviderInfo(elementName, namespace, 102 extensionElementProvider)); 103 } 104 else { 105 exceptions.add(new IllegalArgumentException(className 106 + " is not a PacketExtensionProvider")); 107 } 108 break; 109 case "streamFeatureProvider": 110 ExtensionElementProvider<ExtensionElement> streamFeatureProvider = (ExtensionElementProvider<ExtensionElement>) provider.getConstructor().newInstance(); 111 sfProviders.add(new StreamFeatureProviderInfo(elementName, 112 namespace, 113 streamFeatureProvider)); 114 break; 115 default: 116 LOGGER.warning("Unknown provider type: " + typeName); 117 } 118 } 119 catch (ClassNotFoundException cnfe) { 120 LOGGER.log(Level.SEVERE, "Could not find provider class", cnfe); 121 exceptions.add(cnfe); 122 } 123 catch (InstantiationException ie) { 124 LOGGER.log(Level.SEVERE, "Could not instanciate " + className, ie); 125 exceptions.add(ie); 126 } 127 } 128 } 129 catch (IllegalArgumentException illExc) { 130 LOGGER.log(Level.SEVERE, "Invalid provider type found [" + typeName + "] when expecting iqProvider or extensionProvider", illExc); 131 exceptions.add(illExc); 132 } 133 } 134 eventType = parser.next(); 135 } 136 while (eventType != XmlPullParser.END_DOCUMENT); 137 } 138 catch (Exception e){ 139 LOGGER.log(Level.SEVERE, "Unknown error occurred while parsing provider file", e); 140 exceptions.add(e); 141 } 142 finally { 143 try { 144 providerStream.close(); 145 } 146 catch (Exception e) { 147 // Ignore. 148 } 149 } 150 } 151 152 @Override 153 public Collection<IQProviderInfo> getIQProviderInfo() { 154 return iqProviders; 155 } 156 157 @Override 158 public Collection<ExtensionProviderInfo> getExtensionProviderInfo() { 159 return extProviders; 160 } 161 162 @Override 163 public Collection<StreamFeatureProviderInfo> getStreamFeatureProviderInfo() { 164 return sfProviders; 165 } 166 167 public List<Exception> getLoadingExceptions() { 168 return Collections.unmodifiableList(exceptions); 169 } 170}