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.awt.event.ActionEvent;
12 import java.awt.event.ActionListener;
13 import java.awt.TextField;
14 import java.awt.Button;
15
16 /**
17 * An example applet which connects to xmlBlaster using xml scripting and a persistent http tunnel for callbacks.
18 * <p>Is used with AppletServlet.java running inside tomcat 5</p>
19 * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
20 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.script.html">client.script requirement</a>
21 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.java.applet.html">
22 * Applet requirement</a>
23 * @see <a href="http://www.xmlblaster.org/xmlBlaster/demo/http/index.html">http://www.xmlblaster.org/xmlBlaster/demo/http/index.html</a>
24 */
25 public class XmlScript extends Applet implements I_CallbackRaw, ActionListener
26 {
27 I_XmlBlasterAccessRaw xmlBlasterAccess;
28 TextArea textArea, requestArea;
29 Button connectButton, sendButton;
30 boolean runAsApplet;
31
32 public void init(){
33 this.runAsApplet = true;
34 System.out.println("XmlScript: Applet.init() called");
35 try {
36 initUI();
37 }
38 catch (Exception e) {
39 showStatus("XmlScript: Problem in init(): " + e.toString());
40 }
41 }
42
43 private void initUI() throws Exception {
44 setBackground(Color.white);
45 setForeground(Color.black);
46 this.connectButton = new Button("Connect");
47 this.connectButton.setActionCommand("connect");
48 this.connectButton.addActionListener(this);
49 add(this.connectButton);
50 this.sendButton = new Button("Send");
51 this.sendButton.setActionCommand("send");
52 this.sendButton.addActionListener(this);
53 add(this.sendButton);
54 this.requestArea = new TextArea("", 9, 62);
55 this.requestArea.setBackground(Color.white);
56 this.requestArea.setForeground(Color.black);
57 // add an example request
58 this.requestArea.setText("<xmlBlaster>\n" +
59 " <get>\n" +
60 " <key queryType='XPATH'>\n" +
61 " /xmlBlaster/key\n" +
62 " </key>\n" +
63 " </get>\n" +
64 "</xmlBlaster>\n" +
65 "<!-- See http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.script.html -->");
66 add(this.requestArea);
67 this.textArea = new TextArea("", 24, 80);
68 this.textArea.setBackground(Color.white);
69 this.textArea.setForeground(Color.black);
70 add(this.textArea);
71 repaint();
72 }
73
74 /** Event-handler called on button click */
75 public void actionPerformed(ActionEvent ev) {
76 String command = ev.getActionCommand();
77 try {
78 if(command.equals("connect")){
79 // Connect to xmlBlaster server
80 if((this.connectButton.getLabel()).equals("Connect")){
81 this.xmlBlasterAccess = new XmlBlasterAccessRaw(this);
82 this.xmlBlasterAccess.connect(null, this);
83 print("Connected to xmlBlaster");
84 showStatus("XmlScript: Connected to xmlBlaster, please send a request.");
85 this.connectButton.setLabel("Logout");
86 }
87 //logout from server
88 else if((this.connectButton.getLabel()).equals("Logout")){
89 this.xmlBlasterAccess.disconnect(null);
90 this.xmlBlasterAccess = null;
91 showStatus("XmlScript: Not connected to xmlBlaster.");
92 this.connectButton.setLabel("Connect");
93 }
94 }
95 else if( command.equals("send") || ( (ev.getSource()) instanceof TextField )){
96 if (this.xmlBlasterAccess == null || !this.xmlBlasterAccess.isConnected()) {
97 showStatus("XmlScript: Please log in first.");
98 return;
99 }
100 String xmlRequest = this.requestArea.getText();
101 String response = this.xmlBlasterAccess.sendXmlScript(xmlRequest);
102 this.textArea.setText(this.textArea.getText()+response);
103 showStatus("XmlScript: Request done.");
104 }
105 }
106 catch (Exception e) {
107 print(e.toString());
108 e.printStackTrace();
109 showStatus("XmlScript: " + e.toString());
110 }
111 }
112
113 /** For testing without applet GUI */
114 public void init(Hashtable properties){
115 try {
116 this.xmlBlasterAccess = new XmlBlasterAccessRaw(properties);
117 this.xmlBlasterAccess.connect(null, this);
118
119 Hashtable subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='cpuinfo'/>", "<qos/>");
120 subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='meminfo'/>", "<qos/>");
121 }
122 catch (Exception e) {
123 e.printStackTrace();
124 }
125 }
126
127 private void print(String text) {
128 if (this.runAsApplet)
129 this.textArea.append("XmlScript: " + text + "\n");
130 else
131 System.out.println("SystemInfo: " + text + "\n");
132 }
133
134 public void destroy(){
135 print("Applet destroy ...");
136 if (this.xmlBlasterAccess != null) {
137 try {
138 this.xmlBlasterAccess.unSubscribe("<key oid='cpuinfo'/>", "<qos/>");
139 this.xmlBlasterAccess.unSubscribe("<key oid='meminfo'/>", "<qos/>");
140 }
141 catch (Exception e) {
142 print("UnSubscribe problem: " + e.toString());
143 }
144 this.xmlBlasterAccess.disconnect("<qos/>");
145 this.xmlBlasterAccess = null;
146 print("Disconnected from xmlBlaster");
147 }
148 }
149
150 /**
151 * Here you receive the callback messages from xmlBlaster.
152 */
153 public String update(String cbSessionId, Hashtable updateKey, byte[] content, Hashtable updateQos) throws Exception {
154 print("Asynchronous update arrived: key=" + updateKey.get("/key/@oid") + ", status=" + updateQos.get("/qos/state/@id") + ":");
155 print("------------------------------------------------");
156 print(new String(content));
157 print("------------------------------------------------");
158 //repaint();
159 return "<qos/>";
160 }
161
162 /** java http.applet.XmlScript */
163 public static void main(String[] args) {
164 XmlScript appl = new XmlScript();
165 Hashtable properties = new Hashtable();
166 if (args.length < 1)
167 properties.put("xmlBlaster/servletUrl", "http://localhost:8080/xmlBlaster/AppletServlet");
168 else properties.put("xmlBlaster/servletUrl", args[0]);
169 //properties.put("xmlBlaster/loginName", "tester");
170 //properties.put("xmlBlaster/passwd", "secret");
171 properties.put("xmlBlaster/logLevels", "ERROR,WARN,INFO");
172 appl.init(properties);
173 }
174 }
syntax highlighted by Code2HTML, v. 0.9.1