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