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 iqProviders.add(new IQProviderInfo(elementName, namespace, (IQProvider<IQ>) provider.newInstance())); 087 } 088 else { 089 exceptions.add(new IllegalArgumentException(className + " is not a IQProvider")); 090 } 091 break; 092 case "extensionProvider": 093 // Attempt to load the provider class and then create 094 // a new instance if it's an ExtensionProvider. Otherwise, if it's 095 // a PacketExtension, add the class object itself and 096 // then we'll use reflection later to create instances 097 // of the class. 098 if (ExtensionElementProvider.class.isAssignableFrom(provider)) { 099 extProviders.add(new ExtensionProviderInfo(elementName, namespace, (ExtensionElementProvider<ExtensionElement>) provider.newInstance())); 100 } 101 else { 102 exceptions.add(new IllegalArgumentException(className 103 + " is not a PacketExtensionProvider")); 104 } 105 break; 106 case "streamFeatureProvider": 107 sfProviders.add(new StreamFeatureProviderInfo(elementName, 108 namespace, 109 (ExtensionElementProvider<ExtensionElement>) provider.newInstance())); 110 break; 111 default: 112 LOGGER.warning("Unknown provider type: " + typeName); 113 } 114 } 115 catch (ClassNotFoundException cnfe) { 116 LOGGER.log(Level.SEVERE, "Could not find provider class", cnfe); 117 exceptions.add(cnfe); 118 } 119 catch (InstantiationException ie) { 120 LOGGER.log(Level.SEVERE, "Could not instanciate " + className, ie); 121 exceptions.add(ie); 122 } 123 } 124 } 125 catch (IllegalArgumentException illExc) { 126 LOGGER.log(Level.SEVERE, "Invalid provider type found [" + typeName + "] when expecting iqProvider or extensionProvider", illExc); 127 exceptions.add(illExc); 128 } 129 } 130 eventType = parser.next(); 131 } 132 while (eventType != XmlPullParser.END_DOCUMENT); 133 } 134 catch (Exception e){ 135 LOGGER.log(Level.SEVERE, "Unknown error occurred while parsing provider file", e); 136 exceptions.add(e); 137 } 138 finally { 139 try { 140 providerStream.close(); 141 } 142 catch (Exception e) { 143 // Ignore. 144 } 145 } 146 } 147 148 @Override 149 public Collection<IQProviderInfo> getIQProviderInfo() { 150 return iqProviders; 151 } 152 153 @Override 154 public Collection<ExtensionProviderInfo> getExtensionProviderInfo() { 155 return extProviders; 156 } 157 158 @Override 159 public Collection<StreamFeatureProviderInfo> getStreamFeatureProviderInfo() { 160 return sfProviders; 161 } 162 163 public List<Exception> getLoadingExceptions() { 164 return Collections.unmodifiableList(exceptions); 165 } 166}