3 <title>Smack: Provider Architecture - Jive Software
</title>
4 <link rel=
"stylesheet" type=
"text/css" href=
"style.css" />
10 Provider Architecture: Packet Extensions and Custom IQ's
14 « <a href=
"index.html">Table of Contents
</a>
19 The Smack provider architecture is a system for plugging in
20 custom XML parsing of packet extensions and IQ packets. The
21 standard
<a href=
"extensions/index.html">Smack Extensions
</a>
22 are built using the provider architecture. Two types of
24 <li><tt>IQProvider
</tt> -- parses IQ requests into Java objects.
25 <li><tt>PacketExtension
</tt> -- parses XML sub-documents attached to
26 packets into PacketExtension instances.
</ul>
28 <p class=
"subheader">IQProvider
</p>
30 By default, Smack only knows how to process IQ packets with sub-packets that
31 are in a few namespaces such as:
<ul>
34 <li>jabber:iq:register
</ul>
36 Because many more IQ types are part of XMPP and its extensions, a pluggable IQ parsing
37 mechanism is provided. IQ providers are registered programatically or by creating a
38 smack.providers file in the META-INF directory of your JAR file. The file is an XML
39 document that contains one or more iqProvider entries, as in the following example:
42 <?xml
version=
"1.0"?
>
43 <smackProviders
>
45 <elementName
>query
</elementName
>
46 <namespace
>jabber:iq:time
</namespace
>
47 <className
>org.jivesoftware.smack.packet.Time</className
>
49 </smackProviders
></pre>
51 Each IQ provider is associated with an element name and a namespace. In the
52 example above, the element name is
<tt>query
</tt> and the namespace is
53 <tt>abber:iq:time
</tt>. If multiple provider entries attempt to register to
54 handle the same namespace, the first entry loaded from the classpath will
57 The IQ provider class can either implement the IQProvider
58 interface, or extend the IQ class. In the former case, each IQProvider is
59 responsible for parsing the raw XML stream to create an IQ instance. In
60 the latter case, bean introspection is used to try to automatically set
61 properties of the IQ instance using the values found in the IQ packet XML.
62 For example, an XMPP time packet resembles the following:
65 <iq type='result' to='joe@example.com' from='mary@example.com' id='time_1'
>
66 <query xmlns='jabber:iq:time'
>
67 <utc
>20020910T17:
58:
35</utc
>
68 <tz
>MDT
</tz
>
69 <display
>Tue Sep
10 12:
58:
35 2002</display
>
73 In order for this packet to be automatically mapped to the Time object listed in the
74 providers file above, it must have the methods setUtc(String), setTz(String), and
75 setDisplay(String). The introspection service will automatically try to convert the String
76 value from the XML into a boolean, int, long, float, double, or Class depending on the
77 type the IQ instance expects.
<p>
79 <p class=
"subheader">PacketExtensionProvider
</p>
81 Packet extension providers provide a pluggable system for
82 packet extensions, which are child elements in a custom namespace
83 of IQ, message and presence packets.
84 Each extension provider is registered with an element name and namespace
85 in the smack.providers file as in the following example:
88 <?xml
version=
"1.0"?
>
89 <smackProviders
>
90 <extensionProvider
>
91 <elementName
>x
</elementName
>
92 <namespace
>jabber:iq:event
</namespace
>
93 <className
>org.jivesoftware.smack.packet.MessageEvent</className
>
94 </extensionProvider
>
95 </smackProviders
></pre>
97 If multiple provider entries attempt to register to handle the same element
98 name and namespace, the first entry loaded from the classpath will take
101 Whenever a packet extension is found in a packet, parsing will
102 be passed to the correct provider. Each provider can either implement the
103 PacketExtensionProvider interface or be a standard Java Bean. In the
104 former case, each extension provider is responsible for parsing the raw
105 XML stream to contruct an object. In the latter case, bean introspection
106 is used to try to automatically set the properties of the class using
107 the values in the packet extension sub-element.
<p>
109 When an extension provider is not registered for an element name and
110 namespace combination, Smack will store all top-level elements of the
111 sub-packet in DefaultPacketExtension object and then attach it to the packet.
114 <br clear=
"all" /><br><br>
117 Copyright
© Jive Software
2002-
2008