1 import java.util.Hashtable;
  2 
  3 import javax.microedition.lcdui.*;
  4 import javax.microedition.midlet.*;
  5 
  6 import org.xmlBlaster.client.protocol.http.common.I_CallbackRaw;
  7 import org.xmlBlaster.client.protocol.http.j2me.XmlBlasterAccessJ2ME;
  8 
  9 public class SystemInfoMidlet extends MIDlet implements CommandListener, I_CallbackRaw, Runnable {
 10    // the display manager
 11    Display display;
 12 
 13    // ticker
 14    Ticker ticker = new Ticker("XmlBlaster: SystemInfo Demo");
 15 
 16    Form form, mainForm;
 17    StringItem[] lines;
 18    int count = 0;
 19    TextField addr;
 20    
 21    // command
 22    static final Command exitCommand = new Command("Exit", Command.STOP, 2);
 23    static final Command backCommand = new Command("Back", Command.STOP, 2);
 24    static final Command goCommand = new Command("Go", Command.SCREEN, 2);
 25    
 26    private XmlBlasterAccessJ2ME xmlBlasterAccess;
 27    private Hashtable properties;
 28    
 29    // constructor.
 30    public SystemInfoMidlet() {
 31       this.properties = new Hashtable();
 32       String servletUrl = this.getAppProperty("servletUrl");
 33       if (servletUrl == null) servletUrl = "http://localhost:8080/xmlBlaster/AppletServlet";
 34       this.properties.put("xmlBlaster/servletUrl", servletUrl);
 35       this.properties.put("xmlBlaster/logLevels", "ERROR,WARN,INFO");
 36    }
 37 
 38    /**
 39     * Start the MIDlet by creating a list of
 40     * items and associating the
 41     * exit command with it.
 42     */
 43    public void startApp() throws MIDletStateChangeException {
 44       display = Display.getDisplay(this);
 45       //startXmlBlaster();
 46       mainMenu();
 47    }
 48 
 49    private void mainMenu() {
 50       this.mainForm = new Form("Entry Point");
 51       String url = (String)this.properties.get("xmlBlaster/servletUrl");
 52       this.addr = new TextField("url", url, 100, 0);
 53       this.mainForm.append(addr);      
 54       this.mainForm.addCommand(exitCommand);
 55       this.mainForm.addCommand(goCommand);
 56       this.mainForm.setCommandListener(this);
 57       this.display.setCurrent(this.mainForm);
 58    }
 59 
 60    public void run() {
 61       startXmlBlaster();
 62    }
 63 
 64    private void startXmlBlaster() {
 65       this.form = new Form("SystemInfo");
 66       this.lines = new StringItem[4];
 67       this.lines[0] = new StringItem("key       :", "   ");
 68       this.form.append(lines[0]);
 69       this.lines[1] = new StringItem("value     :", "   ");
 70       this.form.append(lines[1]);
 71       this.lines[2] = new StringItem("update nr.:", "   ");
 72       this.form.append(lines[2]);
 73       this.lines[3] = new StringItem("comments  :", "   ");
 74       this.form.append(lines[3]);
 75 
 76       this.form.addCommand(backCommand);
 77       this.form.setCommandListener(this);
 78       this.form.setTicker(ticker);
 79       display.setCurrent(this.form);
 80       init(this.properties);
 81    }
 82 
 83    private void stopXmlBlaster() {
 84       if (this.xmlBlasterAccess != null) {
 85          print("Applet destroy ...");
 86          try {
 87             this.xmlBlasterAccess.unSubscribe("<key oid='cpuinfo'/>", "<qos/>");
 88             this.xmlBlasterAccess.unSubscribe("<key oid='meminfo'/>", "<qos/>");
 89          }
 90          catch (Exception e) {
 91             print("UnSubscribe problem");
 92          }
 93          this.xmlBlasterAccess.disconnect("<qos/>");
 94          this.xmlBlasterAccess = null;
 95          print("Disconnected");
 96       }
 97    }
 98 
 99 
100    public void pauseApp() {
101       display = null;
102       ticker = null;
103       lines = null;
104       this.addr = null;
105    }
106 
107    public void destroyApp(boolean unconditional) {
108       stopXmlBlaster();
109       notifyDestroyed();
110    }
111 
112    /**
113     * Handle events.
114     */  
115    public void commandAction(Command c, Displayable d) {
116       String label = c.getLabel();
117       if (label.equals("Exit")) {
118          destroyApp(true);
119       }
120       else if(label.equals("Go")) {
121          String val = this.addr.getString();
122          this.properties.put("xmlBlaster/servletUrl", val);
123          //startXmlBlaster();
124          // to be able to use the commands
125          Thread thread = new Thread(this);
126          thread.start();
127       }
128       else if(label.equals("Back")) {
129          stopXmlBlaster();
130          mainMenu();
131       }
132    }
133   
134    public void init(Hashtable properties){
135       try {
136          this.xmlBlasterAccess = new XmlBlasterAccessJ2ME(properties);
137          this.xmlBlasterAccess.connect(null, this);
138 
139          Hashtable subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='cpuinfo'/>", "<qos/>");
140          subReturnQos = this.xmlBlasterAccess.subscribe("<key oid='meminfo'/>", "<qos/>");
141       }
142       catch (Exception e) {
143          e.printStackTrace();
144       }
145    }
146 
147    private void print(String text) {
148       this.lines[3].setText(text);
149    }
150 
151    /**
152     * Here you receive the callback messages from xmlBlaster. 
153     */
154    public String update(String cbSessionId, Hashtable updateKey, byte[] content, Hashtable updateQos) throws Exception {
155       this.count++;
156       this.lines[0].setText((String)updateKey.get("/key/@oid"));
157       this.lines[1].setText(new String(content));
158       this.lines[2].setText(String.valueOf(this.count));
159       return "<qos/>";
160    }
161 }


syntax highlighted by Code2HTML, v. 0.9.1