1 // xmlBlaster/demo/javaclients/HelloWorldGet.java
  2 package javaclients;
  3 
  4 import java.util.Iterator;
  5 import java.util.Map;
  6 import java.util.logging.Logger;
  7 
  8 import org.xmlBlaster.util.FileLocator;
  9 import org.xmlBlaster.util.Global;
 10 import org.xmlBlaster.util.MsgUnit;
 11 import org.xmlBlaster.util.XmlBlasterException;
 12 import org.xmlBlaster.util.def.Constants;
 13 import org.xmlBlaster.util.qos.HistoryQos;
 14 import org.xmlBlaster.client.qos.ConnectQos;
 15 import org.xmlBlaster.client.qos.ConnectReturnQos;
 16 import org.xmlBlaster.client.qos.DisconnectQos;
 17 import org.xmlBlaster.client.key.GetKey;
 18 import org.xmlBlaster.client.key.GetReturnKey;
 19 import org.xmlBlaster.client.qos.GetQos;
 20 import org.xmlBlaster.client.qos.GetReturnQos;
 21 import org.xmlBlaster.util.qos.AccessFilterQos;
 22 import org.xmlBlaster.client.I_XmlBlasterAccess;
 23 
 24 
 25 /**
 26  * This client connects to xmlBlaster and subscribes to messages. 
 27  * <p>
 28  * This is a nice client to experiment and play with xmlBlaster as there are many
 29  * command line options to specify the type and amount of messages published.
 30  * </p>
 31  * <p>
 32  * Try using 'java javaclients.HelloWorldPublish' in another window to publish some
 33  * messages.
 34  * Further you can type 'd' in the window running xmlBlaster to get a server dump.
 35  * </p>
 36  *
 37  * Invoke (after starting the xmlBlaster server):
 38  * <pre>
 39  * java javaclients.HelloWorldGet -xpath //key  -historyNum 2
 40  *
 41  * java javaclients.HelloWorldGet -interactive false -oid Hello  -historyNum -1
 42  *
 43  * java javaclients.HelloWorldGet -session.name joeGet/5 -passwd secret
 44  *
 45  * java javaclients.HelloWorldGet -xpath //key -filter.type GnuRegexFilter -filter.query "^__sys__jdbc.*"
 46  *
 47  * java javaclients.HelloWorldGet -xpath //key -filter.type XPathFilter -filter.query "//tomato"
 48  *
 49  * java javaclients.HelloWorldGet -xpath //key -filter.type ContentLenFilter -filter.query "10"
 50  * </pre>
 51  * <p>
 52  * If disconnect=false we don't logout at the end.
 53  * </p>
 54  * @see java javaclients.HelloWorldPublish
 55  * @see java javaclients.HelloWorldSubscribe
 56  * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.get.html" target="others">xmlBlaster get interface</a>
 57  */
 58 public class HelloWorldGet
 59 {
 60    private static Logger log = Logger.getLogger(HelloWorldGet.class.getName());
 61 
 62    public HelloWorldGet(Global glob) {
 63 
 64       try {
 65          boolean interactive = glob.getProperty().get("interactive", true);
 66          String oid = glob.getProperty().get("oid", "");
 67          String domain = glob.getProperty().get("domain", (String)null);
 68          String xpath = glob.getProperty().get("xpath", "");
 69          int historyNumUpdates = glob.getProperty().get("historyNum", 1);
 70          boolean historyNewestFirst = glob.getProperty().get("historyNewestFirst", true);
 71          String filterType = glob.getProperty().get("filter.type", "GnuRegexFilter");// XPathFilter | ContentLenFilter
 72          String filterVersion = glob.getProperty().get("filter.version", "1.0");
 73          String filterQuery = glob.getProperty().get("filter.query", "");
 74          boolean content = glob.getProperty().get("content", true);
 75          boolean saveToFile = glob.getProperty().get("saveToFile", false);
 76          boolean disconnect = glob.getProperty().get("disconnect", true);
 77          String queryOid = glob.getProperty().get("query.oid", (String)null);
 78          int queryMaxEntries = glob.getProperty().get("query.maxEntries", 1);
 79          long queryTimeout = glob.getProperty().get("query.timeout", 0L);
 80          boolean queryConsumable = glob.getProperty().get("query.consumable", true);
 81 
 82          Map clientPropertyMap = glob.getProperty().get("clientProperty", (Map)null);
 83          Map connectQosClientPropertyMap = glob.getProperty().get("connect/qos/clientProperty", (Map)null);
 84 
 85          if (oid.length() < 1 && xpath.length() < 1) {
 86             log.warning("No -oid or -xpath given, we subscribe to oid='Hello'.");
 87             oid = "Hello";
 88          }
 89 
 90          log.info("Used settings are:");
 91          log.info("   -interactive        " + interactive);
 92          log.info("   -oid                " + oid);
 93          log.info("   -domain             " + domain);
 94          log.info("   -xpath              " + xpath);
 95          log.info("   -historyNum         " + historyNumUpdates);
 96          log.info("   -historyNewestFirst " + historyNewestFirst);
 97          log.info("   -content            " + content);
 98          log.info("   -disconnect         " + disconnect);
 99          log.info("   -filter.type        " + filterType);
100          log.info("   -filter.version     " + filterVersion);
101          log.info("   -filter.query       " + filterQuery);
102          
103          //            "topic/hello" to access a history queue,
104          //            "client/joe" to access a subject queue or
105          //            "client/joe/session/1"
106          log.info("   -query.oid          " + queryOid);
107          log.info("   -query.maxEntries   " + queryMaxEntries);
108          log.info("   -query.timeout      " + queryTimeout);
109          log.info("   -query.consumable   " + queryConsumable);
110          log.info("   -saveToFile         " + saveToFile);
111          if (clientPropertyMap != null) {
112             Iterator it = clientPropertyMap.keySet().iterator();
113             while (it.hasNext()) {
114                String key = (String)it.next();
115                log.info("   -clientProperty["+key+"]   " + clientPropertyMap.get(key).toString());
116             }
117          }
118          else {
119             log.info("   -clientProperty[]   ");
120          }
121          if (connectQosClientPropertyMap != null) {
122             Iterator it = connectQosClientPropertyMap.keySet().iterator();
123             while (it.hasNext()) {
124                String key = (String)it.next();
125                log.info("   -connect/qos/clientProperty["+key+"]   " + connectQosClientPropertyMap.get(key).toString());
126             }
127          }
128          else {
129             log.info("   -connect/qos/clientProperty[]   ");
130          }
131          log.info("For more info please read:");
132          log.info("   http://www.xmlBlaster.org/xmlBlaster/doc/requirements/interface.get.html");
133 
134          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
135 
136          // ConnectQos checks -session.name and -passwd from command line
137          log.info("============= CreatingConnectQos");
138          ConnectQos qos = new ConnectQos(glob);
139          if (connectQosClientPropertyMap != null) {
140             Iterator it = connectQosClientPropertyMap.keySet().iterator();
141             while (it.hasNext()) {
142                String key = (String)it.next();
143                qos.addClientProperty(key, connectQosClientPropertyMap.get(key).toString());
144             }
145          }
146          log.info("ConnectQos is " + qos.toXml());
147          ConnectReturnQos crq = con.connect(qos, null);  // Login to xmlBlaster
148          log.info("Connect success as " + crq.toXml());
149          
150          MsgUnit[] msgs = null;
151          if (queryOid != null) {
152             // http://www.xmlblaster.org/xmlBlaster/doc/requirements/engine.qos.queryspec.QueueQuery.html
153             if (interactive) {
154                log.info("Hit a key to get '" + queryOid + "' maxEntries=" + queryMaxEntries + " timeout=" + queryTimeout + " consumable=" + queryConsumable);
155                try { System.in.read(); } catch(java.io.IOException e) {}
156             }
157             msgs = con.receive(queryOid, queryMaxEntries, queryTimeout, queryConsumable);
158          }
159          else {
160             GetKey gk = (oid.length() > 0) ? new GetKey(glob, oid) : new GetKey(glob, xpath, Constants.XPATH);
161             if (domain != null) gk.setDomain(domain);
162             GetQos gq = new GetQos(glob);
163             gq.setWantContent(content);
164             
165             HistoryQos historyQos = new HistoryQos(glob);
166             historyQos.setNumEntries(historyNumUpdates);
167             historyQos.setNewestFirst(historyNewestFirst);
168             gq.setHistoryQos(historyQos);
169    
170             if (filterQuery.length() > 0) {
171                AccessFilterQos filter = new AccessFilterQos(glob, filterType, filterVersion, filterQuery);
172                gq.addAccessFilter(filter);
173             }
174             if (clientPropertyMap != null) {
175                Iterator it = clientPropertyMap.keySet().iterator();
176                while (it.hasNext()) {
177                   String key = (String)it.next();
178                   gq.addClientProperty(key, clientPropertyMap.get(key).toString());
179                }
180                //Example for a typed property:
181                //pq.getData().addClientProperty("ALONG", (new Long(12)));
182             }
183 
184             log.info("GetKey=\n" + gk.toXml());
185             log.info("GetQos=\n" + gq.toXml());
186 
187             if (interactive) {
188                log.info("Hit a key to get '" + ((oid.length() > 0) ? oid : xpath) + "'");
189                try { System.in.read(); } catch(java.io.IOException e) {}
190             }
191 
192             msgs = con.get(gk.toXml(), gq.toXml());
193          }
194 
195          for(int imsg=0; imsg<msgs.length; imsg++) {
196             GetReturnKey grk = new GetReturnKey(glob, msgs[imsg].getKey());
197             GetReturnQos grq = new GetReturnQos(glob, msgs[imsg].getQos());
198 
199             System.out.println("");
200             System.out.println("============= START #" + (imsg+1) + " '" + grk.getOid() + "' =======================");
201             log.info("Receiving update #" + (imsg+1) + " of a message ...");
202             System.out.println("<xmlBlaster>");
203             System.out.println(msgs[imsg].toXml("", 100, true));
204             System.out.println("</xmlBlaster>");
205             System.out.println("============= END #" + (imsg+1) + " '" + grk.getOid() + "' =========================");
206             System.out.println("");
207 
208             if (saveToFile) {
209                String fileName = grk.getOid()+"-"+grq.getRcvTimestamp().getTimestamp();
210                try {
211                   FileLocator.writeFile(fileName, msgs[imsg].toXml("").getBytes());
212                }
213                catch (XmlBlasterException e) {
214                   System.out.println("Can't dump content to file '" + fileName + "': " + e.toString());
215                }
216             }
217             
218          }
219          if (msgs.length == 0) {
220             log.info("Sorry, no message found for '" + ((oid.length() > 0) ? oid : xpath) + "'");
221          }
222 
223          log.info("Hit a key to exit");
224          try { System.in.read(); } catch(java.io.IOException e) {}
225 
226          if (disconnect) {
227             DisconnectQos dq = new DisconnectQos(glob);
228             con.disconnect(dq);
229          }
230       }
231       catch (XmlBlasterException e) {
232          log.severe(e.getMessage());
233       }
234       catch (Exception e) {
235          e.printStackTrace();
236          log.severe(e.toString());
237       }
238    }
239 
240    /**
241     * Try
242     * <pre>
243     *   java javaclients.HelloWorldGet -help
244     * </pre>
245     * for usage help
246     */
247    public static void main(String args[]) {
248       Global glob = new Global();
249       
250       if (glob.init(args) != 0) { // Get help with -help
251          System.out.println(glob.usage());
252          System.err.println("\nExample:");
253          System.err.println("  java javaclients.HelloWorldGet -oid Hello -initialUpdate true\n");
254          System.err.println("  java javaclients.HelloWorldGet  -clientProperty[myString] Hello -clientProperty[correlationId] 100\n");
255          System.exit(1);
256       }
257 
258       new HelloWorldGet(glob);
259    }
260 }


syntax highlighted by Code2HTML, v. 0.9.1