1 /*------------------------------------------------------------------------------
  2 Name:      GetMessage.java
  3 Project:   xmlBlaster.org
  4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5 Comment:   Code to get from command line a message
  6 Version:   $Id: GetMessage.java 14857 2006-03-07 19:17:35Z goetzger $
  7 ------------------------------------------------------------------------------*/
  8 package org.xmlBlaster.client.reader;
  9 
 10 import java.util.logging.Logger;
 11 
 12 import org.xmlBlaster.client.I_XmlBlasterAccess;
 13 import org.xmlBlaster.client.key.GetKey;
 14 import org.xmlBlaster.client.qos.GetQos;
 15 import org.xmlBlaster.util.Global;
 16 import org.xmlBlaster.client.qos.ConnectQos;
 17 import org.xmlBlaster.util.XmlBlasterException;
 18 import org.xmlBlaster.util.MsgUnit;
 19 
 20 
 21 /**
 22  * Get from command line a message.
 23  * <br />
 24  * Use this as a command line tool to get for messages from xmlBlaster,
 25  * for example for debugging reasons.
 26  * Invoke examples:<br />
 27  * <pre>
 28  *    java org.xmlBlaster.client.reader.GetMessage  -loginName  Tim  -passwd  secret  -oid  __cmd:?totalMem
 29  * </pre>
 30  * For other supported options type
 31  * <pre>
 32  *    java org.xmlBlaster.client.reader.GetMessage -?
 33  * </pre>
 34  */
 35 public class GetMessage
 36 {
 37    private static final String ME = "GetMessage";
 38    private final Global glob;
 39    private static Logger log = Logger.getLogger(GetMessage.class.getName());
 40    private I_XmlBlasterAccess xmlBlasterConnection;
 41 
 42    /**
 43     * Constructs the GetMessage object.
 44     * <p />
 45     * Start with parameter -? to get a usage description.<br />
 46     * These command line parameters are not merged with xmlBlaster.property properties.
 47     * @param args      Command line arguments
 48     */
 49    public GetMessage(Global glob) {
 50       this.glob = glob;
 51 
 52    }
 53 
 54    /**
 55     * Get the message from xmlBlaster. 
 56     */
 57    public void get() throws Exception {
 58       String oidString = glob.getProperty().get("oid", (String)null);
 59       String xpathString = glob.getProperty().get("xpath", (String)null);
 60 
 61       if (oidString == null && xpathString == null) {
 62          usage();
 63          log.severe("Specify -oid <message-oid> or -xpath <query>");
 64          System.exit(1);
 65       }
 66 
 67       String xmlKey;
 68       String queryType;
 69       if (oidString != null) {
 70          xmlKey = oidString;
 71          queryType = "EXACT";
 72       }
 73       else {
 74          xmlKey = xpathString;
 75          queryType = "XPATH";
 76       }
 77 
 78       try {
 79          xmlBlasterConnection = glob.getXmlBlasterAccess();
 80          ConnectQos qos = new ConnectQos(glob);
 81          xmlBlasterConnection.connect(qos, null); // Login to xmlBlaster
 82       }
 83       catch (Exception e) {
 84          log.severe(e.toString());
 85          e.printStackTrace();
 86       }
 87 
 88       GetKey xmlKeyWr = new GetKey(glob, xmlKey, queryType);
 89       GetQos xmlQos = new GetQos(glob);
 90       MsgUnit[] msgs = xmlBlasterConnection.get(xmlKeyWr.toXml(), xmlQos.toXml());
 91       log.info("Got " + msgs.length + " messages for '" + xmlKey + "'");
 92       for (int ii=0; ii<msgs.length; ii++) {
 93          System.out.println("\n" + msgs[ii].toXml());
 94       }
 95 
 96       xmlBlasterConnection.disconnect(null);
 97    }
 98 
 99    /**
100     * Command line usage.
101     */
102    private void usage() {
103       System.out.println("----------------------------------------------------------");
104       System.out.println("java org.xmlBlaster.client.reader.GetMessage <options>");
105       System.out.println("----------------------------------------------------------");
106       System.out.println("Options:");
107       System.out.println("   -?                  Print this message.");
108       System.out.println("");
109       System.out.println("   -oid <XmlKeyOid>    The unique oid of the message");
110       System.out.println("   -xpath <XPATH>      The XPATH query");
111       //I_XmlBlasterAccess.usage();
112       //log.usage();
113       System.out.println("----------------------------------------------------------");
114       System.out.println("Example:");
115       System.out.println("java org.xmlBlaster.client.reader.GetMessage -oid mySpecialMessage");
116       System.out.println("");
117       System.out.println("java org.xmlBlaster.client.reader.GetMessage -oid __cmd:?freeMem");
118       System.out.println("");
119       System.out.println("java org.xmlBlaster.client.reader.GetMessage -xpath //key/CAR");
120       System.out.println("----------------------------------------------------------");
121       System.out.println("");
122    }
123 
124 
125    /**
126     * Invoke:  java org.xmlBlaster.client.reader.GetMessage  -loginName Tim  -passwd secret  -oid __cmd:?totalMem
127     */
128    public static void main(String args[]) {
129       Global glob = new Global();
130       if (glob.init(args) != 0) {
131          GetMessage getter = new GetMessage(glob);
132          getter.usage();
133          System.exit(0);
134       }
135       try {
136          GetMessage getter = new GetMessage(glob);
137          getter.get();
138       } 
139       catch (XmlBlasterException e) {
140          System.out.println("ERROR: " + e.getMessage());
141       }
142       catch (Throwable e) {
143          e.printStackTrace();
144          System.out.println("ERROR: " + e.toString());
145       }
146    }
147 }


syntax highlighted by Code2HTML, v. 0.9.1