1 20 21 package org.jivesoftware.smack.provider; 22 23 import org.jivesoftware.smack.packet.IQ; 24 import org.jivesoftware.smack.packet.PacketExtension; 25 import org.xmlpull.v1.*; 26 import org.xmlpull.mxp1.MXParser; 27 28 import java.util.*; 29 import java.net.URL ; 30 31 110 public class ProviderManager { 111 112 private static Map extensionProviders = new Hashtable(); 113 private static Map iqProviders = new Hashtable(); 114 115 static { 116 try { 118 ClassLoader [] classLoaders = getClassLoaders(); 120 for (int i=0; i<classLoaders.length; i++) { 121 Enumeration providerEnum = classLoaders[i].getResources( 122 "META-INF/smack.providers"); 123 while (providerEnum.hasMoreElements()) { 124 URL url = (URL )providerEnum.nextElement(); 125 java.io.InputStream providerStream = null; 126 try { 127 providerStream = url.openStream(); 128 XmlPullParser parser = new MXParser(); 129 parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true); 130 parser.setInput(providerStream, "UTF-8"); 131 int eventType = parser.getEventType(); 132 do { 133 if (eventType == XmlPullParser.START_TAG) { 134 if (parser.getName().equals("iqProvider")) { 135 parser.next(); 136 parser.next(); 137 String elementName = parser.nextText(); 138 parser.next(); 139 parser.next(); 140 String namespace = parser.nextText(); 141 parser.next(); 142 parser.next(); 143 String className = parser.nextText(); 144 String key = getProviderKey(elementName, namespace); 147 if (!iqProviders.containsKey(key)) { 148 try { 153 Class provider = Class.forName(className); 155 if (IQProvider.class.isAssignableFrom(provider)) { 156 iqProviders.put(key, provider.newInstance()); 157 } 158 else if (IQ.class.isAssignableFrom(provider)) { 159 iqProviders.put(key, provider); 160 } 161 } 162 catch (ClassNotFoundException cnfe) { 163 cnfe.printStackTrace(); 164 } 165 } 166 } 167 else if (parser.getName().equals("extensionProvider")) { 168 parser.next(); 169 parser.next(); 170 String elementName = parser.nextText(); 171 parser.next(); 172 parser.next(); 173 String namespace = parser.nextText(); 174 parser.next(); 175 parser.next(); 176 String className = parser.nextText(); 177 String key = getProviderKey(elementName, namespace); 180 if (!extensionProviders.containsKey(key)) { 181 try { 187 Class provider = Class.forName(className); 189 if (PacketExtensionProvider.class.isAssignableFrom( 190 provider)) 191 { 192 extensionProviders.put(key, provider.newInstance()); 193 } 194 else if (PacketExtension.class.isAssignableFrom( 195 provider)) 196 { 197 extensionProviders.put(key, provider); 198 } 199 } 200 catch (ClassNotFoundException cnfe) { 201 cnfe.printStackTrace(); 202 } 203 } 204 } 205 } 206 eventType = parser.next(); 207 } while (eventType != XmlPullParser.END_DOCUMENT); 208 } 209 finally { 210 try { providerStream.close(); } 211 catch (Exception e) { } 212 } 213 } 214 } 215 } 216 catch (Exception e) { 217 e.printStackTrace(); 218 } 219 } 220 221 241 public static Object getIQProvider(String elementName, String namespace) { 242 String key = getProviderKey(elementName, namespace); 243 return iqProviders.get(key); 244 } 245 246 251 public static Iterator getIQProviders() { 252 return Collections.unmodifiableCollection(new HashMap(iqProviders).values()).iterator(); 253 } 254 255 264 public static void addIQProvider(String elementName, String namespace, 265 Object provider) 266 { 267 if (!(provider instanceof IQProvider || (provider instanceof Class && 268 IQ.class.isAssignableFrom((Class )provider)))) 269 { 270 throw new IllegalArgumentException ("Provider must be an IQProvider " + 271 "or a Class instance."); 272 } 273 String key = getProviderKey(elementName, namespace); 274 iqProviders.put(key, provider); 275 } 276 277 296 public static Object getExtensionProvider(String elementName, String namespace) { 297 String key = getProviderKey(elementName, namespace); 298 return extensionProviders.get(key); 299 } 300 301 310 public static void addExtensionProvider(String elementName, String namespace, 311 Object provider) 312 { 313 if (!(provider instanceof PacketExtensionProvider || provider instanceof Class )) { 314 throw new IllegalArgumentException ("Provider must be a PacketExtensionProvider " + 315 "or a Class instance."); 316 } 317 String key = getProviderKey(elementName, namespace); 318 extensionProviders.put(key, provider); 319 } 320 321 326 public static Iterator getExtensionProviders() { 327 return Collections.unmodifiableCollection( 328 new HashMap(extensionProviders).values()).iterator(); 329 } 330 331 338 private static String getProviderKey(String elementName, String namespace) { 339 StringBuffer buf = new StringBuffer (); 340 buf.append("<").append(elementName).append("/><").append(namespace).append("/>"); 341 return buf.toString(); 342 } 343 344 349 private static ClassLoader [] getClassLoaders() { 350 ClassLoader [] classLoaders = new ClassLoader [2]; 351 classLoaders[0] = new ProviderManager().getClass().getClassLoader(); 352 classLoaders[1] = Thread.currentThread().getContextClassLoader(); 353 return classLoaders; 354 } 355 356 private ProviderManager() { 357 358 } 359 } | Popular Tags |