1 /*------------------------------------------------------------------------------
 2 Name:      ClientGet.java
 3 Project:   xmlBlaster.org
 4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
 5 Comment:   Demo code for a client using xmlBlaster
 6 Version:   $Id: ClientQuery.java 14861 2006-03-07 19:19:47Z goetzger $
 7 ------------------------------------------------------------------------------*/
 8 package javaclients;
 9 
10 import java.util.logging.Logger;
11 
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.util.XmlBlasterException;
14 import org.xmlBlaster.client.I_XmlBlasterAccess;
15 import org.xmlBlaster.client.key.UpdateKey;
16 import org.xmlBlaster.util.MsgUnit;
17 
18 
19 /**
20  * This client allows you to query xmlBlaster synchronous with method get().
21  * <p>
22  * It doesn't implement a Callback server, since it only access xmlBlaster
23  * using the synchronous get() method.
24  * <p>
25  * Invoke example:<br />
26  * <pre>
27  *    java javaclients.ClientQuery -queryXpath "//key"
28  * </pre>
29  */
30 public class ClientQuery
31 {
32    private static String ME = "ClientQuery";
33    private static Logger log = Logger.getLogger(ClientQuery.class.getName());
34    private String queryString;
35    private String queryType = "XPATH";
36 
37    public ClientQuery(String args[])
38    {
39       // Initialize command line argument handling (this is optional)
40       Global glob = new Global();
41 
42       if (glob.init(args) != 0) usage("Aborted");
43 
44       try {
45          String loginName = glob.getProperty().get("session.name", ME); // check if parameter -session.name <userName> is given at startup of client
46          String passwd = glob.getProperty().get("passwd", "secret");
47 
48          queryString = glob.getProperty().get("queryXpath", (String)null);
49          if (queryString != null)
50             queryType = "XPATH";
51          else
52             usage("Please enter a query string");
53 
54          I_XmlBlasterAccess con = glob.getXmlBlasterAccess();
55          con.connect(null, null);
56 
57 
58          String xmlKey = "<key oid='' queryType='" + queryType + "'>\n" +
59                             queryString +
60                          "</key>";
61          MsgUnit[] msgArr = null;
62          try {
63             msgArr = con.get(xmlKey, "<qos></qos>");
64             log.info("Got " + msgArr.length + " messages for query '" + queryString + "':");
65             for (int ii=0; ii<msgArr.length; ii++) {
66                UpdateKey updateKey = new UpdateKey(glob, msgArr[ii].getKey());
67                log.info("\n" + updateKey.toXml());
68                log.info("\n" + new String(msgArr[ii].getContent()) + "\n");
69             }
70          } catch(XmlBlasterException e) {
71             log.severe("XmlBlasterException: " + e.getMessage());
72          }
73 
74          con.disconnect(null);
75       }
76       catch (XmlBlasterException e) {
77           log.severe("Error occurred: " + e.toString());
78           e.printStackTrace();
79       }
80    }
81 
82    private void usage(String text)
83    {
84       System.out.println("\nAvailable options:");
85       System.out.println("   -queryXpath         \"//key\"");
86       System.out.println(Global.instance().usage());
87       System.out.println("Example: java javaclients.ClientQuery -queryXpath //key\n");
88       System.out.println(text);
89       System.exit(1);
90    }
91 
92    public static void main(String args[]) {
93       new ClientQuery(args);
94    }
95 }


syntax highlighted by Code2HTML, v. 0.9.1