1 // xmlBlaster/demo/javaclients/AllProtocols.java
  2 package javaclients;
  3 
  4 import java.util.logging.Logger;
  5 import org.xmlBlaster.util.Global;
  6 import org.xmlBlaster.util.MsgUnit;
  7 import org.xmlBlaster.util.XmlBlasterException;
  8 import org.xmlBlaster.client.qos.ConnectQos;
  9 import org.xmlBlaster.client.qos.DisconnectQos;
 10 import org.xmlBlaster.client.I_Callback;
 11 import org.xmlBlaster.client.key.UpdateKey;
 12 import org.xmlBlaster.client.key.PublishKey;
 13 import org.xmlBlaster.client.key.GetKey;
 14 import org.xmlBlaster.client.key.SubscribeKey;
 15 import org.xmlBlaster.client.key.UnSubscribeKey;
 16 import org.xmlBlaster.client.key.EraseKey;
 17 import org.xmlBlaster.client.qos.GetQos;
 18 import org.xmlBlaster.client.qos.GetReturnQos;
 19 import org.xmlBlaster.client.qos.PublishQos;
 20 import org.xmlBlaster.client.qos.PublishReturnQos;
 21 import org.xmlBlaster.client.qos.UpdateQos;
 22 import org.xmlBlaster.client.qos.UpdateReturnQos;
 23 import org.xmlBlaster.client.qos.SubscribeQos;
 24 import org.xmlBlaster.client.qos.SubscribeReturnQos;
 25 import org.xmlBlaster.client.qos.EraseQos;
 26 import org.xmlBlaster.client.qos.EraseReturnQos;
 27 import org.xmlBlaster.client.qos.UnSubscribeQos;
 28 import org.xmlBlaster.client.qos.UnSubscribeReturnQos;
 29 import org.xmlBlaster.client.I_XmlBlasterAccess;
 30 
 31 
 32 /**
 33  * This client connects to xmlBlaster and invokes all available methods with all available protocols. 
 34  * <p />
 35  * Invoke: java javaclients.AllProtocols
 36  * <p />
 37  * Invoke: java javaclients.AllProtocols -session.name joe -passwd secret
 38  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.html" target="others">xmlBlaster interface</a>
 39  */
 40 public class AllProtocols implements I_Callback
 41 {
 42    private String ME = "";
 43    private final Global glob;
 44    private static Logger log = Logger.getLogger(AllProtocols.class.getName());
 45    private final String[] argsIOR = {
 46       "-protocol",
 47       "IOR",
 48    };
 49    private final String[] argsSocket = {
 50       "-protocol",
 51       "SOCKET",
 52    };
 53    private final String[] argsXmlRpc = {
 54       "-protocol",
 55       "XMLRPC",
 56    };
 57    private final String[] argsRmi = {
 58       "-protocol",
 59       "RMI",
 60    };
 61    private final Con[] conList = {
 62       new Con(argsIOR, "IOR connection"),
 63       new Con(argsSocket, "SOCKET connection"),
 64       new Con(argsXmlRpc, "XMLRPC connection"),
 65       new Con(argsRmi, "RMI connection")
 66    };
 67 
 68    public AllProtocols(Global glob) {
 69       this.glob = glob;
 70 
 71       for(int i=0; i<conList.length; i++) {
 72          ME = conList[i].helpText;
 73          conList[i].con = conList[i].glob.getXmlBlasterAccess();
 74          I_XmlBlasterAccess con = conList[i].con;
 75          try {
 76 
 77             // Check if other login name or password was given on command line:
 78             // (This is redundant as it is done by ConnectQos already)
 79             String name = con.getGlobal().getProperty().get("session.name", "AllProtocols");
 80             String passwd = con.getGlobal().getProperty().get("passwd", "secret");
 81 
 82             ConnectQos qos = new ConnectQos(con.getGlobal(), name, passwd);
 83             con.connect(qos, this);  // Login to xmlBlaster, register for updates
 84 
 85 
 86             PublishKey pk = new PublishKey(con.getGlobal(), "AllProtocols", "text/xml", "1.0");
 87             pk.setClientTags("<org.xmlBlaster><demo/></org.xmlBlaster>");
 88             PublishQos pq = new PublishQos(con.getGlobal());
 89             MsgUnit msgUnit = new MsgUnit(pk, "Hi", pq);
 90             con.publish(msgUnit);
 91 
 92 
 93             GetKey gk = new GetKey(con.getGlobal(), "AllProtocols");
 94             GetQos gq = new GetQos(con.getGlobal());
 95             MsgUnit[] msgs = con.get(gk.toXml(), gq.toXml());
 96             GetReturnQos grq = new GetReturnQos(con.getGlobal(), msgs[0].getQos());
 97 
 98             log.info("Accessed xmlBlaster message with content '" + new String(msgs[0].getContent()) +
 99                          "' and status=" + grq.getState());
100 
101 
102             SubscribeKey sk = new SubscribeKey(con.getGlobal(), "AllProtocols");
103             SubscribeQos sq = new SubscribeQos(con.getGlobal());
104             SubscribeReturnQos subRet = con.subscribe(sk.toXml(), sq.toXml());
105 
106 
107             msgUnit = new MsgUnit(pk, "Ho".getBytes(), pq);
108             PublishReturnQos prq = con.publish(msgUnit);
109 
110             log.info("Got status='" + prq.getState() + "' for published message '" + prq.getKeyOid());
111 
112             try { Thread.sleep(1000); } 
113             catch( InterruptedException ie) {} // wait a second to receive update()
114 
115 
116             UnSubscribeKey uk = new UnSubscribeKey(con.getGlobal(), subRet.getSubscriptionId());
117             UnSubscribeQos uq = new UnSubscribeQos(con.getGlobal());
118             UnSubscribeReturnQos[] urq = con.unSubscribe(uk.toXml(), uq.toXml());
119 
120             EraseKey ek = new EraseKey(con.getGlobal(), "AllProtocols");
121             EraseQos eq = new EraseQos(con.getGlobal());
122             EraseReturnQos[] eraseArr = con.erase(ek.toXml(), eq.toXml());
123 
124             DisconnectQos dq = new DisconnectQos(con.getGlobal());
125             con.disconnect(dq);
126          }
127          catch (XmlBlasterException e) {
128             log.severe(e.getMessage());
129          }
130          catch (Throwable e) {
131             e.printStackTrace();
132             log.severe(e.toString());
133          }
134       }
135    }
136 
137    public String update(String cbSessionId, UpdateKey updateKey, byte[] content,
138                         UpdateQos updateQos)
139    {
140       if (updateKey.isInternal()) {
141          log.info("Received unexpected internal message '" +
142               updateKey.getOid() + " from xmlBlaster");
143          return "";
144       }
145 
146       log.info("Received asynchronous message '" + updateKey.getOid() +
147                    "' state=" + updateQos.getState() +
148                    " content=" + new String(content) + " from xmlBlaster");
149 
150       UpdateReturnQos uq = new UpdateReturnQos(updateKey.getGlobal());
151       return uq.toXml();
152    }
153 
154    /**
155     * Try
156     * <pre>
157     *   java javaclients.AllProtocols -help
158     * </pre>
159     * for usage help
160     */
161    public static void main(String args[]) {
162       Global glob = new Global();
163       
164       if (glob.init(args) != 0) { // Get help with -help
165          System.out.println(glob.usage());
166          System.err.println("Example: java javaclients.AllProtocols -session.name Jeff\n");
167          System.exit(1);
168       }
169 
170       new AllProtocols(glob);
171    }
172 
173 class Con {
174    public Con(String[] args, String helpText) {
175       this.glob = new Global(args, true, false);
176       this.helpText = helpText;
177    }
178    public String helpText;
179    public Global glob;
180    public I_XmlBlasterAccess con;
181 };
182 }


syntax highlighted by Code2HTML, v. 0.9.1