Package org.jivesoftware.smack.provider
Class ProviderManager
- java.lang.Object
-
- org.jivesoftware.smack.provider.ProviderManager
-
public final class ProviderManager extends java.lang.Object
Manages providers for parsing custom XML sub-documents of XMPP packets. Two types of providers exist:- IQProvider -- parses IQ requests into Java objects.
- PacketExtension -- parses XML sub-documents attached to packets into PacketExtension instances.
By default, Smack only knows how to process IQ packets with sub-packets that are in a few namespaces such as:
- jabber:iq:auth
- jabber:iq:roster
- jabber:iq:register
<?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 stanza XML. For example, an XMPP time stanza 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 stanza 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 stanza 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 stanza 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 construct an object. In the latter case, bean introspection is used to try to automatically set the properties of th class using the values in the stanza 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.
-
-
Constructor Summary
Constructors Constructor Description ProviderManager()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addExtensionProvider(java.lang.String elementName, java.lang.String namespace, java.lang.Object provider)
Adds an extension provider with the specified element name and name space.static void
addIQProvider(java.lang.String elementName, java.lang.String namespace, java.lang.Object provider)
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.static void
addLoader(ProviderLoader loader)
static void
addNonzaProvider(NonzaProvider<? extends Nonza> nonzaProvider)
static void
addStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace, ExtensionElementProvider<ExtensionElement> provider)
static ExtensionElementProvider<ExtensionElement>
getExtensionProvider(java.lang.String elementName, java.lang.String namespace)
Returns the stanza extension provider registered to the specified XML element name and namespace.static ExtensionElementProvider<ExtensionElement>
getExtensionProvider(javax.xml.namespace.QName qname)
static java.util.List<ExtensionElementProvider<ExtensionElement>>
getExtensionProviders()
Returns an unmodifiable collection of all PacketExtensionProvider instances.static IqProvider<IQ>
getIQProvider(java.lang.String elementName, java.lang.String namespace)
Returns the IQ provider registered to the specified XML element name and namespace.static java.util.List<IqProvider<IQ>>
getIQProviders()
Returns an unmodifiable collection of all IQProvider instances.static NonzaProvider<? extends Nonza>
getNonzaProvider(java.lang.String elementName, java.lang.String namespace)
static NonzaProvider<? extends Nonza>
getNonzaProvider(javax.xml.namespace.QName key)
static ExtensionElementProvider<ExtensionElement>
getStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace)
static javax.xml.namespace.QName
removeExtensionProvider(java.lang.String elementName, java.lang.String namespace)
Removes an extension provider with the specified element name and namespace.static javax.xml.namespace.QName
removeIQProvider(java.lang.String elementName, java.lang.String namespace)
Removes an IQ provider with the specified element name and namespace.static void
removeNonzaProvider(java.lang.Class<? extends Nonza> nonzaClass)
static void
removeNonzaProvider(java.lang.String elementName, java.lang.String namespace)
static void
removeStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace)
-
-
-
Constructor Detail
-
ProviderManager
public ProviderManager()
-
-
Method Detail
-
addLoader
public static void addLoader(ProviderLoader loader)
-
getIQProvider
public static IqProvider<IQ> getIQProvider(java.lang.String elementName, java.lang.String namespace)
Returns the IQ provider registered to the specified XML element name and namespace. For example, if a provider was registered to the element name "query" and the namespace "jabber:iq:time", then the following stanza would trigger the provider:<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>
Note: this method is generally only called by the internal Smack classes.
- Parameters:
elementName
- the XML element name.namespace
- the XML namespace.- Returns:
- the IQ provider.
-
getIQProviders
public static java.util.List<IqProvider<IQ>> getIQProviders()
Returns an unmodifiable collection of all IQProvider instances. Each object in the collection will either be an IQProvider instance, or a Class object that implements the IQProvider interface.- Returns:
- all IQProvider instances.
-
addIQProvider
public static void addIQProvider(java.lang.String elementName, java.lang.String namespace, java.lang.Object provider)
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.- Parameters:
elementName
- the XML element name.namespace
- the XML namespace.provider
- the IQ provider.
-
removeIQProvider
public static javax.xml.namespace.QName removeIQProvider(java.lang.String elementName, java.lang.String namespace)
Removes an IQ provider with the specified element name and namespace. This method is typically called to cleanup providers that are programmatically added using theaddIQProvider
method.- Parameters:
elementName
- the XML element name.namespace
- the XML namespace.- Returns:
- the QName of the removed provider
-
getExtensionProvider
public static ExtensionElementProvider<ExtensionElement> getExtensionProvider(java.lang.String elementName, java.lang.String namespace)
Returns the stanza 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 stanza 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.
- Parameters:
elementName
- element name associated with extension provider.namespace
- namespace associated with extension provider.- Returns:
- the extension provider.
-
getExtensionProvider
public static ExtensionElementProvider<ExtensionElement> getExtensionProvider(javax.xml.namespace.QName qname)
-
addExtensionProvider
public static void addExtensionProvider(java.lang.String elementName, java.lang.String namespace, java.lang.Object provider)
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.- Parameters:
elementName
- the XML element name.namespace
- the XML namespace.provider
- the extension provider.
-
removeExtensionProvider
public static javax.xml.namespace.QName removeExtensionProvider(java.lang.String elementName, java.lang.String namespace)
Removes an extension provider with the specified element name and namespace. This method is typically called to cleanup providers that are programmatically added using theaddExtensionProvider
method.- Parameters:
elementName
- the XML element name.namespace
- the XML namespace.- Returns:
- the QName of the removed stanza extension provider
-
getExtensionProviders
public static java.util.List<ExtensionElementProvider<ExtensionElement>> getExtensionProviders()
Returns an unmodifiable collection of all PacketExtensionProvider instances. Each object in the collection will either be a PacketExtensionProvider instance, or a Class object that implements the PacketExtensionProvider interface.- Returns:
- all PacketExtensionProvider instances.
-
getStreamFeatureProvider
public static ExtensionElementProvider<ExtensionElement> getStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace)
-
addStreamFeatureProvider
public static void addStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace, ExtensionElementProvider<ExtensionElement> provider)
-
removeStreamFeatureProvider
public static void removeStreamFeatureProvider(java.lang.String elementName, java.lang.String namespace)
-
getNonzaProvider
public static NonzaProvider<? extends Nonza> getNonzaProvider(java.lang.String elementName, java.lang.String namespace)
-
getNonzaProvider
public static NonzaProvider<? extends Nonza> getNonzaProvider(javax.xml.namespace.QName key)
-
addNonzaProvider
public static void addNonzaProvider(NonzaProvider<? extends Nonza> nonzaProvider)
-
removeNonzaProvider
public static void removeNonzaProvider(java.lang.Class<? extends Nonza> nonzaClass)
-
removeNonzaProvider
public static void removeNonzaProvider(java.lang.String elementName, java.lang.String namespace)
-
-