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 import org.xmlBlaster.client.protocol.http.common.Msg;
  7 
  8 import java.applet.Applet;
  9 import java.awt.TextArea;
 10 import java.awt.Color;
 11 import java.util.Hashtable;
 12 
 13 /**
 14  * An example applet which connects to xmlBlaster using a persistent
 15  * http tunnel for callbacks. 
 16  * @author <a href="mailto:xmlBlaster@marcelruff.info">Marcel Ruff</a>
 17  * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.java.applet.html">
 18  *       Applet requirement</a>
 19  * @see <a href="http://www.xmlblaster.org/xmlBlaster/demo/http/index.html">
 20  *       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 HelloWorld3 extends Applet implements I_CallbackRaw
 25 {
 26    private static final long serialVersionUID = 1L;
 27    I_XmlBlasterAccessRaw xb;
 28    HelloWorld3 applet;
 29    TextArea textArea;
 30 
 31    public void init(){
 32       this.applet = this;
 33       System.out.println("HelloWorld3: Applet.init() called");
 34       try {
 35          setBackground(Color.white);
 36          setForeground(Color.black);
 37          this.textArea = new TextArea("", 12, 60);
 38          this.textArea.setBackground(Color.white);
 39          this.textArea.setForeground(Color.black);
 40          add(this.textArea);
 41          repaint();
 42       }
 43       catch (Exception e) {
 44          e.printStackTrace();
 45          showStatus("HelloWorld3: Can't initialize applet");
 46       }
 47    }
 48  
 49    public void start() {
 50       System.out.println("HelloWorld3: Applet.start() called");
 51       repaint();
 52       Thread t = new Thread() { // Start a new thread so that screen display is not blocked
 53          public void run() {
 54             try {
 55                Hashtable properties = new Hashtable();
 56                properties.put("servlet/xyz", "someValue"); // The servlet will see "xyz=someValue"
 57                xb = new XmlBlasterAccessRaw(applet, properties);
 58                String connectQos = null;
 59                /*
 60                String connectQos =
 61                    "<qos>" +
 62                    "   <securityService type='htpasswd' version='1.0'>" +
 63                    "     <user>eduardo</user>" +
 64                    "     <passwd>secret</passwd>" +
 65                    "   </securityService>" +
 66                    "   <session name='eduardo/1' timeout='-1'/>" +
 67                    "   <persistent>true</persistent>" +
 68                    "</qos>";
 69                */
 70                xb.connect(connectQos, applet); // registers applet.update() callback method
 71                print("Connected to xmlBlaster");
 72 
 73                Hashtable pingReturnQos = xb.ping("<qos/>");
 74                print("Ping, servlet->xmlBlaster connection state is " + pingReturnQos.get("/qos/state/@info"));
 75 
 76                Hashtable subReturnQos = xb.subscribe("<key oid='HELLO'/>", "<qos/>");
 77                print("Subscribed, id=" + subReturnQos.get("/qos/subscribe/@id"));
 78 
 79                Hashtable pubReturnQos = xb.publish("<key oid='HELLO'/>",
 80                                        "Hello World".getBytes(), "<qos/>");
 81                print("Published 'HELLO', returned status is " +
 82                            pubReturnQos.get("/qos/state/@id"));
 83 
 84                Hashtable[] unSubReturnQos = xb.unSubscribe("<key oid='" +
 85                          subReturnQos.get("/qos/subscribe/@id")+"'/>", "<qos/>");
 86                print("UnSubscribed " + unSubReturnQos.length + " topics");
 87 
 88                Msg[] msgs = xb.get("<key oid='HELLO'/>", "<qos/>");
 89                for (int i=0; i<msgs.length; i++) {
 90                   print("Get returned key=" + msgs[i].getKey().get("/key/@oid") +
 91                                           " content=" + msgs[i].getContentStr());
 92                }
 93 
 94                Hashtable[] eraseReturnQos=xb.erase("<key oid='HELLO'/>","<qos/>");
 95                print("Erase " + eraseReturnQos.length + " topics");
 96 
 97                for (int i=0; i<10; i++) {
 98                   try { Thread.sleep(2000); } catch (InterruptedException e) {}
 99                   Hashtable h = xb.ping("<qos/>");
100                   print("Ping, servlet->xmlBlaster connection state is " + h.get("/qos/state/@info"));
101                }
102 
103                print("Bye!");
104             }
105             catch (Exception e) {
106                print("No connection to xmlBlaster: " + e.toString());
107                e.printStackTrace();
108                showStatus("HelloWorld3: No connection to xmlBlaster");
109             }
110          }
111       };
112       t.start();
113    }
114  
115    private void print(String text) {
116       this.textArea.append("HelloWorld3: " + text + "\n");
117    }
118 
119    public void destroy(){
120       print("Applet destroy ...");
121       if (this.xb != null) {
122          this.xb.disconnect("<qos/>");
123          //this.xb = null; Could result in NPE in start()
124          print("Disconnected from xmlBlaster");
125       }
126    }
127 
128    /**
129     * Here you receive the callback messages from xmlBlaster. 
130     */
131    public String update(String cbSessionId, Hashtable updateKey, byte[] content,
132                         Hashtable updateQos) throws Exception {
133       print("---- START update received -----");
134       print("key=" + updateKey.get("/key/@oid") + " state=" +
135             updateQos.get("/qos/state/@id"));
136       print("update received: content=" + new String(content));
137       print("---- END   update received -----");
138       //repaint();
139       return "<qos/>";
140    }
141 }


syntax highlighted by Code2HTML, v. 0.9.1