1 /*------------------------------------------------------------------------------
2 Name: ClientRaw.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package javaclients.corba;
7
8 import java.util.logging.Logger;
9 import java.util.logging.Level;
10
11 import org.xmlBlaster.util.FileLocator;
12 import org.xmlBlaster.util.Global;
13 import org.xmlBlaster.client.key.UpdateKey; // To SAX parse the received XML key
14 //import org.xmlBlaster.client.qos.UpdateQos; // To SAX parse the received XML QoS
15 import org.xmlBlaster.util.StopWatch;
16
17 import org.xmlBlaster.protocol.corba.authenticateIdl.AuthServer;
18 import org.xmlBlaster.protocol.corba.authenticateIdl.AuthServerHelper;
19 import org.xmlBlaster.protocol.corba.serverIdl.Server;
20 import org.xmlBlaster.protocol.corba.serverIdl.MessageUnit;
21 import org.xmlBlaster.protocol.corba.clientIdl.BlasterCallback;
22 import org.xmlBlaster.protocol.corba.clientIdl.BlasterCallbackOperations;
23 import org.xmlBlaster.protocol.corba.clientIdl.BlasterCallbackPOATie;
24 import org.xmlBlaster.protocol.corba.clientIdl.BlasterCallbackHelper;
25
26 import org.omg.CosNaming.*;
27
28
29 /**
30 * Demo code how to access xmlBlaster using CORBA, step by step without any client helper classes.
31 * <p>
32 * It uses the method subscribe() with a later publish() with XPath query.<br />
33 * The subscribe() should be recognized for this later arriving publish()
34 * <p>
35 * Invoke examples:<br />
36 * <pre>
37 * jaco org.xmlBlaster.Main -plugin/ior/iorFile /tmp/NS_Ref
38 *
39 * ${JacORB_HOME}/bin/jaco javaclients.corba.ClientRaw -dispatch/connection/plugin/ior/iorFile /tmp/NS_Ref
40 *
41 * ${JacORB_HOME}/bin/jaco javaclients.corba.ClientRaw -dispatch/connection/plugin/ior/iorString `cat /tmp/NS_Ref`
42 *
43 * ${JacORB_HOME}/bin/jaco javaclients.corba.ClientRaw -loginName "Jeff" `cat /tmp/NS_Ref`
44 * </pre>
45 */
46 public class ClientRaw
47 {
48 private static String ME = "ClientRaw";
49
50 private final org.omg.CORBA.ORB orb;
51 private final String[] args;
52 private static Logger log = Logger.getLogger(ClientRaw.class.getName());
53
54 private Server xmlBlaster = null;
55
56 public ClientRaw(String args[]) {
57 this.args = args;
58 System.out.println(" -dispatch/connection/plugin/ior/iorString The raw IOR string from xmlBlaster.");
59 System.out.println(" -dispatch/connection/plugin/ior/iorFile The raw IOR string from a file.");
60 Global glob = new Global(args);
61
62 orb = org.omg.CORBA.ORB.init(this.args,null);
63 try {
64 AuthServer authServer;
65 ME = glob.getProperty().get("loginName", ME);
66 String loginName = ME;
67
68 String fileName = glob.getProperty().get("dispatch/connection/plugin/ior/iorFile", (String)null); // a file with the IOR string
69 String authServerIOR = glob.getProperty().get("dispatch/connection/plugin/ior/iorString", (String)null); // the IOR string "IOR:00405..."
70
71 if (fileName != null) authServerIOR = FileLocator.readAsciiFile(fileName);
72
73 if (authServerIOR != null) {
74 authServer = AuthServerHelper.narrow(orb.string_to_object(authServerIOR));
75 }
76 else {
77 // asking Name Service CORBA compliant:
78 NamingContext nc = NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
79 NameComponent [] name = new NameComponent[1];
80 name[0] = new NameComponent();
81 name[0].id = "xmlBlaster-Authenticate";
82 name[0].kind = "MOM";
83 if (nc == null) {
84 System.out.println("\nSorry, please pass the server IOR string to the client, e.g.:\n"
85 + "Start the server:\n"
86 + " jaco org.xmlBlaster.Main -dispatch/connection/plugin/ior/iorFile /tmp/NS_Ref\n"
87 + "Start this client:\n"
88 + " jaco javaclients.corba.ClientRaw -dispatch/connection/plugin/ior/iorFile /tmp/NS_Ref\n");
89 usage();
90 System.err.println(ME + ": Read xmlBlaster/INSTALL for help");
91 System.exit(1);
92 }
93 authServer = AuthServerHelper.narrow(nc.resolve(name));
94 }
95
96 StopWatch stop = new StopWatch();
97
98 //---------- Building a Callback server ----------------------
99 // Getting the default POA implementation "RootPOA"
100 org.omg.PortableServer.POA rootPOA =
101 org.omg.PortableServer.POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
102
103 // Intialize my Callback interface:
104 BlasterCallbackPOATie callbackTie = new BlasterCallbackPOATie(new RawCallback(args, ME));
105 BlasterCallback callback = BlasterCallbackHelper.narrow(rootPOA.servant_to_reference( callbackTie ));
106
107 rootPOA.the_POAManager().activate();
108
109 //----------- Login to the server -----------------------
110 try {
111 String passwd = "some";
112
113 // Create a XML based qos (quality of service) which hold the IOR (the CORBA
114 // address of our callback server)
115 String qos = "<qos><callback type='IOR'>";
116 qos += orb.object_to_string(callback);
117 qos += "</callback></qos>";
118
119 // The xmlBlaster server takes this IOR string and uses it to connect
120 // to our client-side callback interface to deliver updates back
121
122 xmlBlaster = authServer.login(loginName, passwd, qos);
123 log.info("Login done");
124 } catch(org.xmlBlaster.protocol.corba.serverIdl.XmlBlasterException e) {
125 log.warning("XmlBlasterException: " + e.getMessage());
126 }
127
128
129 //----------- Subscribe to messages with XPATH -------
130 {
131 log.fine("Subscribing using XPath syntax ...");
132 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
133 "<key oid='' queryType='XPATH'>\n" +
134 "/xmlBlaster/key/AGENT" +
135 "</key>";
136 stop.restart();
137 try {
138 xmlBlaster.subscribe(xmlKey, "<qos></qos>");
139 } catch(org.xmlBlaster.protocol.corba.serverIdl.XmlBlasterException e) {
140 log.warning("XmlBlasterException: " + e.getMessage());
141 }
142 log.info("Subscribe done, there should be no Callback" + stop.nice());
143 }
144
145
146 delay(2000); // Wait some time ...
147
148
149 //----------- Construct a message and publish it ---------
150 {
151 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
152 "<key oid='' contentMime='text/xml'>\n" +
153 " <AGENT id='192.168.124.10' subId='1' type='generic'>" +
154 " <DRIVER id='FileProof' pollingFreq='10'>" +
155 " </DRIVER>"+
156 " </AGENT>" +
157 "</key>";
158 String content = "Yeahh, i'm the new content";
159 MessageUnit msgUnit = new MessageUnit(xmlKey, content.getBytes(), "<qos></qos>");
160 log.info("Publishing ...");
161 stop.restart();
162 try {
163 String publishOid = xmlBlaster.publish(msgUnit);
164 log.fine("Returned oid=" + publishOid);
165 } catch(org.xmlBlaster.protocol.corba.serverIdl.XmlBlasterException e) {
166 log.warning("XmlBlasterException: " + e.getMessage());
167 }
168 log.info("Publishing done, there should be a callback now" + stop.nice());
169 }
170
171 delay(1000); // Wait some time ...
172
173 // orb.run(); // Usually your client won't exit after this, uncomment the run() method
174
175 ask("logout()");
176
177 //----------- Logout --------------------------------------
178 log.info("Logout ...");
179 try {
180 authServer.logout(xmlBlaster);
181 } catch(org.xmlBlaster.protocol.corba.serverIdl.XmlBlasterException e) {
182 log.warning("XmlBlasterException: " + e.getMessage());
183 }
184
185 //----------- Shutdown my callback server -----------------
186 try {
187 rootPOA.deactivate_object(rootPOA.reference_to_id(callback));
188 } catch(Exception e) { log.warning("POA deactivate callback failed"); }
189
190
191 //----------- Stop the POA --------------------------------
192 try {
193 rootPOA.the_POAManager().deactivate(false, true);
194 } catch(Exception e) { log.warning("POA deactivate failed"); }
195
196 //----------- Shutdown the ORB ----------------------------
197 orb.shutdown(true);
198 }
199 catch (Exception e) {
200 log.severe(e.toString());
201 e.printStackTrace();
202 }
203 }
204
205
206 private void delay(long millis) {
207 try {
208 Thread.sleep(millis);
209 }
210 catch( InterruptedException i)
211 {}
212 }
213
214
215 private void ask(String text) {
216 System.out.println(text);
217 System.out.println("################### Hit a key to continue ###################");
218 try {
219 System.in.read();
220 } catch (java.io.IOException e) {}
221 }
222
223 static void usage() {
224 System.out.println("\nAvailable options:");
225 System.out.println(" -loginName The login name [ClientRaw].");
226 System.out.println(" -dispatch/connection/plugin/ior/iorFile File with the IOR string from xmlBlaster.");
227 System.out.println(" -dispatch/callback/plugin/ior/iorString The raw IOR string from xmlBlaster.");
228 System.out.println("Example: jaco javaclients.corba.ClientRaw -dispatch/connection/plugin/ior/iorFile /tmp/NS_Ref\n");
229 System.exit(1);
230 }
231
232 public static void main(String args[]) {
233 new ClientRaw(args);
234 }
235 } // ClientRaw
236
237
238 /**
239 * Example for a callback implementation, used by the demo ClientRaw.
240 */
241 class RawCallback implements BlasterCallbackOperations
242 {
243 final String ME;
244 final Global glob;
245 private static Logger log = Logger.getLogger(ClientRaw.class.getName());
246
247 /**
248 * Construct it.
249 */
250 public RawCallback(String[] args, java.lang.String name) {
251 this.ME = "RawCallback-" + name;
252 glob = new Global(args);
253
254 if (log.isLoggable(Level.FINER)) log.fine("Entering constructor with argument");
255 }
256
257
258 /**
259 * This is the callback method invoked from the server
260 * informing the client in an asynchronous mode about new messages
261 */
262 public String[] update(String cbSessionId, MessageUnit[] msgUnitArr)
263 {
264 String[] ret = new String[msgUnitArr.length];
265 for (int ii=0; ii<msgUnitArr.length; ii++) {
266 MessageUnit msgUnit = msgUnitArr[ii];
267 UpdateKey key = null;
268 //UpdateQos qos = null;
269 try { // SAX parse the received message key and QoS:
270 key = new UpdateKey(glob, msgUnit.xmlKey);
271 //qos = new UpdateQos(glob, msgUnit.qos);
272 } catch (org.xmlBlaster.util.XmlBlasterException e) {
273 log.severe(e.getMessage());
274 }
275 System.out.println("\n================== BlasterCallback update START =============");
276 System.out.println("Callback invoked for " + key.toString() + " content length = " + msgUnit.content.length);
277 System.out.println(new String(msgUnit.content));
278 System.out.println("================== BlasterCallback update END ===============\n");
279 ret[ii] = "<qos><state id='OK'/></qos>";
280 }
281 return ret;
282 }
283
284 /**
285 * This oneway method does not return something, it is high performing but
286 * you loose the application level hand shake.
287 * @see <a href="http://www.xmlBlaster.org/xmlBlaster/src/java/org/xmlBlaster/protocol/corba/xmlBlaster.idl" target="others">CORBA xmlBlaster.idl</a>
288 */
289 public void updateOneway(String cbSessionId, org.xmlBlaster.protocol.corba.serverIdl.MessageUnit[] msgUnitArr)
290 {
291 try {
292 update(cbSessionId, msgUnitArr);
293 }
294 catch (Throwable e) {
295 log.severe("updateOneway() failed, exception is not sent to xmlBlaster: " + e.toString());
296 e.printStackTrace();
297 }
298 }
299
300 /**
301 * Ping to check if the callback server is alive.
302 * @param qos ""
303 * @return ""
304 */
305 public String ping(String qos)
306 {
307 return "";
308 }
309 } // RawCallback
syntax highlighted by Code2HTML, v. 0.9.1