1 package javaclients.graphical;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 
  6 import java.util.logging.Logger;
  7 import java.util.logging.Level;
  8 import org.xmlBlaster.util.Global;
  9 import org.xmlBlaster.client.qos.ConnectQos;
 10 import org.xmlBlaster.client.I_Callback;
 11 import org.xmlBlaster.client.key.UpdateKey;
 12 import org.xmlBlaster.client.qos.UpdateQos;
 13 import org.xmlBlaster.client.I_XmlBlasterAccess;
 14 
 15 import CH.ifa.draw.framework.Drawing;
 16 import CH.ifa.draw.framework.DrawingView;
 17 import CH.ifa.draw.framework.Figure;
 18 import CH.ifa.draw.framework.FigureEnumeration;
 19 
 20 
 21 /**
 22  * This client connects to xmlBlaster and subscribes to a message.
 23  * <p />
 24  * We then publish the message and receive it asynchronous in the update() method.
 25  * <p />
 26  * Invoke: java MonitorSubscriber
 27  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 28  */
 29 public class MonitorSubscriber implements I_Callback {
 30    
 31    private final static String ME = "MonitorSubscriber";
 32    private Global  global;
 33    private static Logger log = Logger.getLogger(MonitorSubscriber.class.getName());
 34    private boolean isRunning;
 35    
 36    private HashMap cmdInstances;
 37    private HashMap cmdTemplates;
 38    /** 
 39     * instances created from the templated. Needs to be different from cmdTemplates since 
 40     * as they are created on the fly they also need to be removed once the topic dies.
 41     */
 42    private HashMap dynamicInstances;
 43    private DrawingView view;
 44    
 45    public MonitorSubscriber(Global glob, DrawingView view) {
 46       global = glob.getClone(null);
 47       this.isRunning = false;
 48 
 49       this.cmdInstances = new HashMap();
 50       this.cmdTemplates = new HashMap();
 51       this.dynamicInstances = new HashMap(); 
 52       this.view = view;
 53    }
 54    
 55    private boolean isCommand(String txt) {
 56       return txt.startsWith("&");
 57    }
 58    
 59    /**
 60     * Gets the first connected (associated command text to the figure) found if any.
 61     * @param fig
 62     * @return null if none is found or the command text if one is found.
 63     */
 64    private String[] getAssociatedTexts(Figure fig) {
 65       if (fig == null) return null;
 66       ArrayList list = new ArrayList();
 67       FigureEnumeration enumer = fig.figures();
 68       while (enumer.hasNextFigure()) {
 69          Figure fig1 = enumer.nextFigure();
 70          if (fig1.getTextHolder() != null) {
 71             String txt = fig1.getTextHolder().getText();
 72             if (isCommand(txt)) list.add(txt);
 73          }
 74       }
 75       if (list.size() < 1) return null;
 76       return (String[])list.toArray(new String[list.size()]);
 77    }
 78 
 79 
 80    private boolean prepareClient(Drawing drawing) {
 81       FigureEnumeration enumer = drawing.figures();
 82       boolean ret = false;
 83       while (enumer.hasNextFigure()) {
 84          Figure fig = enumer.nextFigure();
 85          String[] txt = getAssociatedTexts(fig);
 86          if (txt != null) {
 87             for (int i=0; i < txt.length; i++) {
 88                try {
 89                   MonitorCommand cmd = new MonitorCommand(this.global, txt[i], fig);
 90                   if (cmd.isInstance()) {
 91                      this.cmdInstances.put(cmd.getOid(), cmd);
 92                   }
 93                   else if (cmd.isTemplate()) {
 94                      this.cmdTemplates.put(cmd.getType(), cmd);
 95                   }
 96                   ret = true;
 97                }   
 98                catch (Exception ex) {
 99                   log.warning(ex.getMessage());
100                }
101             }
102          }
103       }
104       return ret;
105    }
106 
107 
108    synchronized public boolean start(String name) {
109       if (this.isRunning) return true;
110       try {
111          if (!prepareClient(this.view.drawing())) return this.isRunning;
112 
113          I_XmlBlasterAccess con = global.getXmlBlasterAccess();
114          ConnectQos qos = new ConnectQos(this.global, /*drawing.getTitle()*/ name, "secret");
115          con.connect(qos, this);  // Login to xmlBlaster, register for updates
116 
117          this.isRunning = true;
118       
119          if (this.cmdInstances.size() > 0) {
120             MonitorCommand[] oidCommands = (MonitorCommand[])this.cmdInstances.values().toArray(new MonitorCommand[this.cmdInstances.size()]);
121             for (int i=0; i < oidCommands.length; i++)
122                con.subscribe("<key oid='" + oidCommands[i].getOid() + "'/>", "<qos/>");
123          }
124          if (this.cmdTemplates.size() > 0) {
125             MonitorCommand[] xpathCommands = (MonitorCommand[])this.cmdTemplates.values().toArray(new MonitorCommand[this.cmdTemplates.size()]);
126             for (int i=0; i < xpathCommands.length; i++)
127                con.subscribe("<key oid='' queryType='XPATH'>//" + xpathCommands[i].getType() + "</key>", "<qos/>");
128          }
129       }
130       catch (Exception e) {
131          System.err.println(e.getMessage());
132       }
133       return this.isRunning;
134    }
135 
136 
137    public void stop() {
138       I_XmlBlasterAccess con = global.getXmlBlasterAccess();
139       con.disconnect(null);
140    }
141 
142    private MonitorCommand searchInTemplates(String oid) {
143       String[] keys = (String[])this.cmdTemplates.keySet().toArray(new String[this.cmdTemplates.size()]);
144       for (int i=0; i < keys.length; i++) {
145          if (oid.startsWith(keys[i]))
146             return ((MonitorCommand)this.cmdTemplates.get(keys[i])).createInstance(oid);
147       }
148       return null;      
149    }
150 
151 
152    synchronized public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
153                         UpdateQos updateQos) {
154       System.out.println("\nHelloWorld: Received asynchronous message '" +
155          updateKey.getOid() + "' state=" + updateQos.getState() + " from xmlBlaster");
156          
157       String oid = updateKey.getOid();
158       if (updateQos.isOk()) {
159          MonitorCommand command = (MonitorCommand)this.cmdInstances.get(oid);
160          if (command == null) {
161             command = (MonitorCommand)this.dynamicInstances.get(oid);
162          }
163          if (command == null) {
164             command = searchInTemplates(oid);
165             if (command != null) {
166                this.dynamicInstances.put(oid, command);
167                this.view.drawing().add(command.getFigure());
168             }
169          }
170          if (command != null) {
171             command.doAction(content, this.view);
172          }
173       }
174       else { // then it is erased ...
175          MonitorCommand command = (MonitorCommand)this.dynamicInstances.remove(oid);
176          if (command != null) {
177             command.remove(this.view);
178          }
179       }
180       return "";
181    }
182 
183 }


syntax highlighted by Code2HTML, v. 0.9.1