/** * $RCSfile$ * $Revision$ * $Date$ * * Copyright (C) 2002-2003 Jive Software. All rights reserved. * ==================================================================== * The Jive Software License (based on Apache Software License, Version 1.1) * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by * Jive Software (http://www.jivesoftware.com)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Smack" and "Jive Software" must not be used to * endorse or promote products derived from this software without * prior written permission. For written permission, please * contact webmaster@jivesoftware.com. * * 5. Products derived from this software may not be called "Smack", * nor may "Smack" appear in their name, without prior written * permission of Jive Software. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL JIVE SOFTWARE OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== */ package org.jivesoftware.smack.provider; import org.jivesoftware.smack.packet.IQ; import org.jivesoftware.smack.packet.PacketExtension; import org.xmlpull.v1.*; import java.util.*; import java.net.URL; /** * Manages providers for parsing custom XML sub-documents of XMPP packets. Two types of * providers exist:
* * By default, Smack only knows how to process IQ packets with sub-packets that * are in a few namespaces such as:
* <?xml version="1.0"?> * <smackProviders> * <iqProvider> * <elementName>query</elementName> * <namespace>jabber:iq:time</namespace> * <className>org.jivesoftware.smack.packet.Time</className> * </iqProvider> * </smackProviders>* * Each IQ provider is associated with an element name and a namespace. If multiple provider * entries attempt to register to handle the same namespace, the first entry loaded from the * classpath will take precedence. The IQ provider class can either implement the IQProvider * interface, or extend the IQ class. In the former case, each IQProvider is responsible for * parsing the raw XML stream to create an IQ instance. In the latter case, bean introspection * is used to try to automatically set properties of the IQ instance using the values found * in the IQ packet XML. For example, an XMPP time packet resembles the following: *
* <iq type='result' to='joe@example.com' from='mary@example.com' id='time_1'> * <query xmlns='jabber:iq:time'> * <utc>20020910T17:58:35</utc> * <tz>MDT</tz> * <display>Tue Sep 10 12:58:35 2002</display> * </query> * </iq>* * In order for this packet to be automatically mapped to the Time object listed in the * providers file above, it must have the methods setUtc(String), setTz(String), and * setDisplay(String). The introspection service will automatically try to convert the String * value from the XML into a boolean, int, long, float, double, or Class depending on the * type the IQ instance expects.
* * A pluggable system for packet extensions, child elements in a custom namespace for * message and presence packets, also exists. Each extension provider * is registered with a name space in the smack.providers file as in the following example: * *
* <?xml version="1.0"?> * <smackProviders> * <extensionProvider> * <elementName>x</elementName> * <namespace>jabber:iq:event</namespace> * <className>org.jivesoftware.smack.packet.MessageEvent</className> * </extensionProvider> * </smackProviders>* * If multiple provider entries attempt to register to handle the same element name and namespace, * the first entry loaded from the classpath will take precedence. Whenever a packet extension * is found in a packet, parsing will be passed to the correct provider. Each provider * can either implement the PacketExtensionProvider interface or be a standard Java Bean. In * the former case, each extension provider is responsible for parsing the raw XML stream to * contruct an object. In the latter case, bean introspection is used to try to automatically * set the properties of the class using the values in the packet extension sub-element. When an * extension provider is not registered for an element name and namespace combination, Smack will * store all top-level elements of the sub-packet in DefaultPacketExtension object and then * attach it to the packet. * * @author Matt Tucker */ public class ProviderManager { private static Map extensionProviders = new Hashtable(); private static Map iqProviders = new Hashtable(); static { // Load IQ processing providers. try { // Get an array of class loaders to try loading the providers files from. ClassLoader[] classLoaders = getClassLoaders(); for (int i=0; i
Note: this method is generally only called by the internal Smack classes. * * @param elementName the XML element name. * @param namespace the XML namespace. * @return the IQ provider. */ public static Object getIQProvider(String elementName, String namespace) { String key = getProviderKey(elementName, namespace); return iqProviders.get(key); } /** * Returns an Iterator for all IQProvider instances. * * @return an Iterator for all IQProvider instances. */ public static Iterator getIQProviders() { return Collections.unmodifiableCollection(new HashMap(iqProviders).values()).iterator(); } /** * Adds an IQ provider (must be an instance of IQProvider or Class object that is an IQ) * with the specified element name and name space. The provider will override any providers * loaded through the classpath. * * @param elementName the XML element name. * @param namespace the XML namespace. * @param provider the IQ provider. */ public static void addIQProvider(String elementName, String namespace, Object provider) { if (!(provider instanceof IQProvider || (provider instanceof Class && IQ.class.isAssignableFrom((Class)provider)))) { throw new IllegalArgumentException("Provider must be an IQProvider " + "or a Class instance."); } String key = getProviderKey(elementName, namespace); iqProviders.put(key, provider); } /** * Returns the packet extension provider registered to the specified XML element name * and namespace. For example, if a provider was registered to the element name "x" and the * namespace "jabber:x:event", then the following packet would trigger the provider: * *
* <message to='romeo@montague.net' id='message_1'> * <body>Art thou not Romeo, and a Montague?</body> * <x xmlns='jabber:x:event'> * <composing/> * </x> * </message>* *
Note: this method is generally only called by the internal Smack classes. * * @param elementName * @param namespace * @return the extenion provider. */ public static Object getExtensionProvider(String elementName, String namespace) { String key = getProviderKey(elementName, namespace); return extensionProviders.get(key); } /** * Adds an extension provider with the specified element name and name space. The provider * will override any providers loaded through the classpath. The provider must be either * a PacketExtensionProvider instance, or a Class object of a Javabean. * * @param elementName the XML element name. * @param namespace the XML namespace. * @param provider the extension provider. */ public static void addExtensionProvider(String elementName, String namespace, Object provider) { if (!(provider instanceof PacketExtensionProvider || provider instanceof Class)) { throw new IllegalArgumentException("Provider must be a PacketExtensionProvider " + "or a Class instance."); } String key = getProviderKey(elementName, namespace); extensionProviders.put(key, provider); } /** * Returns an Iterator for all PacketExtensionProvider instances. * * @return an Iterator for all PacketExtensionProvider instances. */ public static Iterator getExtensionProviders() { return Collections.unmodifiableCollection( new HashMap(extensionProviders).values()).iterator(); } /** * Returns a String key for a given element name and namespace. * * @param elementName the element name. * @param namespace the namespace. * @return a unique key for the element name and namespace pair. */ private static String getProviderKey(String elementName, String namespace) { StringBuffer buf = new StringBuffer(); buf.append("<").append(elementName).append("/><").append(namespace).append("/>"); return buf.toString(); } /** * Returns an array of class loaders to load resources from. * * @return an array of ClassLoader instances. */ private static ClassLoader[] getClassLoaders() { ClassLoader[] classLoaders = new ClassLoader[3]; classLoaders[0] = new ProviderManager().getClass().getClassLoader(); classLoaders[1] = Thread.currentThread().getContextClassLoader(); classLoaders[2] = ClassLoader.getSystemClassLoader(); return classLoaders; } private ProviderManager() { } }