1 package http.applet;
2
3 import org.xmlBlaster.client.protocol.http.applet.XmlBlasterAccessRaw;
4 import org.xmlBlaster.client.protocol.http.common.I_CallbackRaw;
5 import org.xmlBlaster.client.protocol.http.common.I_XmlBlasterAccessRaw;
6
7 import java.applet.Applet;
8 import java.awt.TextArea;
9 import java.awt.Color;
10 import java.util.Hashtable;
11 //import java.util.Map;
12
13 /**
14 * An example applet which connects to xmlBlaster using a persistent http tunnel for callbacks
15 * and displays the asynchronous delivered <i>cpuinfo</i> and <i>meminfo</i> messages.
16 *
17 * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
18 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.java.applet.html">
19 * Applet requirement</a>
20 * @see <a href="http://www.xmlblaster.org/xmlBlaster/demo/http/index.html">http://www.xmlblaster.org/xmlBlaster/demo/http/index.html</a>
21 * @see org.xmlBlaster.util.qos.MsgQosData#toJXPath()
22 * @see org.xmlBlaster.util.key.MsgKeyData#toJXPath()
23 */
24 public class SystemInfoApplet extends Applet implements I_CallbackRaw
25 {
26 I_XmlBlasterAccessRaw xmlBlasterAccess;
27 TextArea textArea;
28 boolean runAsApplet;
29
30 public void init(){
31 this.runAsApplet = true;
32 print("Applet.init() called");
33 try {
34 setBackground(Color.white);
35 setForeground(Color.black);
36 this.textArea = new TextArea("", 12, 60);
37 this.textArea.setBackground(Color.white);
38 this.textArea.setForeground(Color.black);
39 add(this.textArea);
40 repaint();
41
42 this.xmlBlasterAccess = new XmlBlasterAccessRaw(this);
43 this.xmlBlasterAccess.connect(null, this);
44 print("Connected to xmlBlaster");
45
46 Hashtable subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='cpuinfo'/>", "<qos/>");
47 subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='meminfo'/>", "<qos/>");
48 print("Subscribed on 'cpuinfo' and 'meminfo' topics");
49
50 showStatus("SystemInfoApplet: Connected to xmlBlaster");
51 }
52 catch (Exception e) {
53 print("No connection to xmlBlaster: " + e.toString());
54 e.printStackTrace();
55 showStatus("SystemInfoApplet: No connection to xmlBlaster: " + e.toString());
56 }
57 }
58
59 public void init(Hashtable properties){
60 try {
61 this.xmlBlasterAccess = new XmlBlasterAccessRaw(properties);
62 this.xmlBlasterAccess.connect(null, this);
63
64 Hashtable subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='cpuinfo'/>", "<qos/>");
65 subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='meminfo'/>", "<qos/>");
66 }
67 catch (Exception e) {
68 e.printStackTrace();
69 }
70 }
71
72 private void print(String text) {
73 String id = (this.xmlBlasterAccess==null) ? ": " : (" #"+this.xmlBlasterAccess.getInstanceId()+": ");
74 if (this.runAsApplet && this.textArea!=null)
75 this.textArea.append("SystemInfoApplet" + id + text + "\n");
76 else
77 System.out.println("SystemInfo" + id + text + "\n");
78 }
79
80 public void destroy(){
81 print("Applet destroy ...");
82 if (this.xmlBlasterAccess != null) {
83 try {
84 this.xmlBlasterAccess.unSubscribe("<key oid='cpuinfo'/>", "<qos/>");
85 this.xmlBlasterAccess.unSubscribe("<key oid='meminfo'/>", "<qos/>");
86 }
87 catch (Exception e) {
88 print("UnSubscribe problem: " + e.toString());
89 }
90 this.xmlBlasterAccess.disconnect("<qos/>");
91 this.xmlBlasterAccess = null;
92 print("Disconnected from xmlBlaster");
93 }
94 }
95
96 /**
97 * Here you receive the callback messages from xmlBlaster.
98 */
99 public String update(String cbSessionId, Hashtable updateKey, byte[] content, Hashtable updateQos) throws Exception {
100 print("Asynchronous Update: key=" + updateKey.get("/key/@oid") + "=" + new String(content));
101 //repaint();
102 return "<qos/>";
103 }
104
105 /**
106 * Start outside of browser: java http.applet.SystemInfoApplet
107 */
108 public static void main(String[] args) {
109 SystemInfoApplet appl = new SystemInfoApplet();
110 Hashtable properties = new Hashtable();
111 if (args.length < 1)
112 properties.put("xmlBlaster/servletUrl", "http://localhost:8080/xmlBlaster/AppletServlet");
113 else properties.put("xmlBlaster/servletUrl", args[0]);
114 //properties.put("xmlBlaster/loginName", "tester");
115 //properties.put("xmlBlaster/passwd", "secret");
116 properties.put("xmlBlaster/logLevels", "ERROR,WARN,INFO");
117 appl.init(properties);
118 }
119
120 }
syntax highlighted by Code2HTML, v. 0.9.1