KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > VersionIQ


1 import org.jivesoftware.smack.packet.IQ;
2 import org.jivesoftware.smack.provider.ProviderManager;
3
4 /** An IQ packet for Software version jabber:iq:version.
5 * See <a HREF="http://www.jabber.org/jeps/jep-0092.html">JEP92</a>.
6 * Call <code>install()</code> once at the beginning of your code to install.
7 * You also need to add a listenr to respond to queries. EG:
8 * <p>Create a packet listener:</p>
9 * <code>
10 import org.jivesoftware.smack.PacketListener;
11 import org.jivesoftware.smack.packet.Packet;
12 import org.jivesoftware.smack.packet.IQ;
13 public final class VersionIQListener implements PacketListener{
14     
15     public void processPacket(Packet packet){
16         VersionIQ in;
17         try{
18             in=(VersionIQ) packet;
19         }
20         catch(ClassCastException cce){
21             return;
22         }
23         if(in.getType()!=IQ.Type.GET){
24             return;
25         }
26         VersionIQ out=new VersionIQ("my client ","1.0",System.getProperties().getProperty("os.name"));
27         out.setType(IQ.Type.RESULT);
28         out.setTo(in.getFrom());
29         out.setPacketID(in.getPacketID());
30         try{
31             Conn.sendPacket(out);
32         }
33         catch(Exception e){
34             //swallow
35         }
36     }
37 }
38 * </code>
39 * <p>Add the listener to the connection:</p>
40 * <code>
41 * Conn.addPacketListener(new VersionIQListener(),new PacketTypeFilter(VersionIQ.class));
42 * </code>
43 */

44 public final class VersionIQ extends IQ {
45     
46     private String JavaDoc name=null;
47     private String JavaDoc version=null;
48     private String JavaDoc os=null;
49     
50     private boolean isEmpty=true;
51     
52     /** Creates an empty VersionIQ packet.*/
53     public VersionIQ(){
54         //nowt to do
55
}
56     
57     /** Creates a VersionIQ packet sitable for replying to a version request.
58     * @param name The software's name.
59     * @param version The version of the software.
60     * @param os The operating system name. Optional and may be <code>null</code>.
61     * @exception NullPointerException If <code>name<code> or <code>version<code> are <code>null<code>.*/

62     public VersionIQ(String JavaDoc name, String JavaDoc version, String JavaDoc os){
63         if(name==null || version==null){
64             throw new NullPointerException JavaDoc("Null parameter passed to VersionIQ constructor");
65         }
66         isEmpty=false;
67         this.name=name;
68         this.version=version;
69         this.os=os;
70     }
71     
72     /** Returns the software name.
73     * May return <code>null</code>.*/

74     public String JavaDoc getName(){
75         return name;
76     }
77     
78     /** Returns the software version.
79     * May return <code>null</code>.*/

80     public String JavaDoc getVersion(){
81         return version;
82     }
83     
84     /** Returns the operating system name.
85     * May return <code>null</code>.*/

86     public String JavaDoc getOs(){
87         return os;
88     }
89     
90     /** Sets the software name.*/
91     public void setName(String JavaDoc name){
92         this.name=name;
93         if(name!=null){
94             isEmpty=false;
95         }
96     }
97     
98     /** Sets the software name.*/
99     public void setVersion(String JavaDoc version){
100         this.version=version;
101         if(version!=null){
102             isEmpty=false;
103         }
104     }
105     
106     /** Sets the operating system name.*/
107     public void setOs(String JavaDoc os){
108         this.os=os;
109     }
110     
111     /** Programatically installs VersionIQ provision.*/
112     public static void install(){
113         ProviderManager.addIQProvider("query","jabber:iq:version",VersionIQ.class);
114     }
115
116     public String JavaDoc getChildElementXML() {
117         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
118         if(isEmpty){
119             buf.append("<query xmlns=\"jabber:iq:version\"/>");
120             return buf.toString();
121         }
122         buf.append("<query xmlns=\"jabber:iq:version\">");
123         buf.append("<name>").append(name).append("</name>");
124         buf.append("<version>").append(version).append("</version>");
125         if(os!=null){
126             buf.append("<os>").append(os).append("</os>");
127         }
128         buf.append("</query>");
129         return buf.toString();
130     }
131 }
Popular Tags