1 /*------------------------------------------------------------------------------
2 Name: SubscribeMessage.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 Comment: Code to subscribe from command line for a message
6 Version: $Id: SubscribeMessage.java 17998 2011-10-12 13:55:57Z laghi $
7 ------------------------------------------------------------------------------*/
8 package org.xmlBlaster.client.reader;
9
10 import java.util.logging.Logger;
11 import java.util.logging.Level;
12
13 import org.xmlBlaster.client.I_XmlBlasterAccess;
14 import org.xmlBlaster.client.key.UpdateKey;
15 import org.xmlBlaster.client.qos.UpdateQos;
16 import org.xmlBlaster.client.I_Callback;
17 import org.xmlBlaster.client.key.SubscribeKey;
18 import org.xmlBlaster.client.qos.SubscribeQos;
19 import org.xmlBlaster.client.qos.SubscribeReturnQos;
20 import org.xmlBlaster.util.Global;
21 import org.xmlBlaster.client.qos.ConnectQos;
22 import org.xmlBlaster.util.XmlBlasterException;
23 import org.xmlBlaster.util.def.Constants;
24
25
26 /**
27 * Subscribe from command line for a message.
28 * <br />
29 * Use this as a command line tool to subscribe for messages from xmlBlaster,
30 * for example for debugging reasons.
31 * Invoke examples:<br />
32 * <pre>
33 * java org.xmlBlaster.client.reader.SubscribeMessage -session.name Tim -passwd secret -oid __cmd:?totalMem
34 * </pre>
35 * For other supported options type
36 * <pre>
37 * java org.xmlBlaster.client.reader.SubscribeMessage -?
38 * </pre>
39 */
40 public class SubscribeMessage implements I_Callback
41 {
42 private static final String ME = "SubscribeMessage";
43 private final Global glob;
44 private static Logger log = Logger.getLogger(SubscribeMessage.class.getName());
45 private I_XmlBlasterAccess xmlBlasterConnection;
46 private String subscriptionHandle;
47
48 /**
49 * Constructs the SubscribeMessage object.
50 * <p />
51 * Start with parameter -? to get a usage description.<br />
52 * These command line parameters are not merged with xmlBlaster.property properties.
53 * @param args Command line arguments
54 */
55 public SubscribeMessage(Global glob) throws XmlBlasterException
56 {
57 this.glob = glob;
58
59
60 String oidString = glob.getProperty().get("oid", (String)null);
61 String xpathString = glob.getProperty().get("xpath", (String)null);
62
63 if (oidString == null && xpathString == null) {
64 usage();
65 log.severe("Specify the message oid or a xpath query");
66 System.exit(1);
67 }
68
69 String xmlKey;
70 String queryType;
71 if (oidString != null) {
72 xmlKey = oidString;
73 queryType = Constants.EXACT;
74 }
75 else {
76 xmlKey = xpathString;
77 queryType = Constants.XPATH;
78 }
79
80 setUp(); // login
81 subscriptionHandle = subscribe(xmlKey, queryType);
82
83 try { Thread.sleep(10000000L); } catch (Exception e) { }
84 log.warning("Bye, time is over.");
85 }
86
87
88 /**
89 * Open the connection, and subscribe to the message
90 */
91 public SubscribeMessage(Global glob, String xmlKey, String queryType)
92 {
93 this.glob = glob;
94
95 setUp(); // login
96 subscriptionHandle = subscribe(xmlKey, queryType);
97 }
98
99
100 /**
101 * Sets up the fixture.
102 * <p />
103 * Connect to xmlBlaster and login
104 */
105 private void setUp()
106 {
107 try {
108 xmlBlasterConnection = glob.getXmlBlasterAccess();
109 ConnectQos qos = new ConnectQos(glob);
110 xmlBlasterConnection.connect(qos, this); // Login to xmlBlaster
111 }
112 catch (Exception e) {
113 log.severe(e.toString());
114 e.printStackTrace();
115 }
116 }
117
118
119 /**
120 * Logout from xmlBlaster
121 */
122 private void tearDown()
123 {
124 unSubscribe(subscriptionHandle);
125 xmlBlasterConnection.disconnect(null);
126 }
127
128
129 private String subscribe(String xmlKey, String queryType)
130 {
131 try {
132 SubscribeKey xmlKeyWr = new SubscribeKey(glob, xmlKey, queryType);
133 SubscribeQos xmlQos = new SubscribeQos(glob);
134 SubscribeReturnQos ret = xmlBlasterConnection.subscribe(xmlKeyWr.toXml(), xmlQos.toXml());
135 String subscriptionId = ret.getSubscriptionId();
136 log.info("Subscribed to [" + xmlKey + "] " + queryType + ", subscriptionId=" + subscriptionId);
137 return subscriptionId;
138 } catch(XmlBlasterException e) {
139 log.severe("XmlBlasterException:\n" + e.getMessage());
140 System.exit(1);
141 }
142 return null;
143 }
144
145
146 /**
147 * Unsubscribe from given subscription
148 * @param subscriptionId The id you got from your subscription
149 */
150 private void unSubscribe(String subscriptionId)
151 {
152 if (subscriptionId == null || subscriptionId.length() < 1) return;
153 try {
154 SubscribeKey xmlKey = new SubscribeKey(glob, subscriptionId);
155 SubscribeQos xmlQos = new SubscribeQos(glob);
156 xmlBlasterConnection.unSubscribe(xmlKey.toXml(), xmlQos.toXml());
157 if (log.isLoggable(Level.FINE)) log.fine("Unsubscribed from " + subscriptionId + " (GML and XML Packages)");
158 } catch(XmlBlasterException e) {
159 log.warning("unSubscribe(" + subscriptionId + ") failed: XmlBlasterException: " + e.getMessage());
160 }
161 }
162
163
164 public String update(String loginName, UpdateKey updateKey, byte[] content, UpdateQos updateQos)
165 {
166 System.out.println("");
167 System.out.println("============= START " + updateKey.getOid() + " =======================");
168 log.info("Receiving update of a message ...");
169 System.out.println("<xmlBlaster>");
170 System.out.println(updateKey.toXml());
171 System.out.println("");
172 System.out.println("<content>");
173 System.out.println(updateQos.getContentStrNoEx(content));
174 System.out.println("</content>");
175 System.out.println(updateQos.toXml());
176 System.out.println("</xmlBlaster>");
177 System.out.println("============= END " + updateKey.getOid() + " =========================");
178 System.out.println("");
179 return Constants.RET_OK; // "<qos><state id='OK'/></qos>";
180 }
181
182
183 /**
184 * Command line usage.
185 */
186 private static void usage()
187 {
188 System.out.println(Global.instance().usage());
189 System.out.println("----------------------------------------------------------");
190 System.out.println("java org.xmlBlaster.client.reader.SubscribeMessage <options>");
191 System.out.println("----------------------------------------------------------");
192 System.out.println("Options:");
193 System.out.println(" -? Print this message.");
194 System.out.println("");
195 System.out.println(" -oid <XmlKeyOid> The unique oid of the message");
196 System.out.println(" -xpath <XPATH> The XPATH query");
197 //I_XmlBlasterAccess.usage();
198 //log.usage();
199 System.out.println("----------------------------------------------------------");
200 System.out.println("Example:");
201 System.out.println("java org.xmlBlaster.client.reader.SubscribeMessage -oid mySpecialMessage");
202 System.out.println("");
203 System.out.println("java org.xmlBlaster.client.reader.SubscribeMessage -xpath //key/CAR");
204 System.out.println("----------------------------------------------------------");
205 System.out.println("");
206 }
207
208
209 /**
210 * Invoke: java org.xmlBlaster.client.reader.SubscribeMessage -loginName Tim -passwd secret -oid __cmd:?totalMem
211 */
212 public static void main(String args[])
213 {
214 Global glob = new Global();
215 if (glob.init(args) != 0) {
216 usage();
217 System.exit(1);
218 }
219 try {
220 new SubscribeMessage(glob);
221 } catch (Throwable e) {
222 e.printStackTrace();
223 System.err.println(SubscribeMessage.ME + ": " + e.toString());
224 }
225 }
226 }
syntax highlighted by Code2HTML, v. 0.9.1