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 with RMI
6 Version: $Id: ClientGet.java 14846 2006-03-07 17:14:22Z ruff $
7 ------------------------------------------------------------------------------*/
8 package javaclients.rmi;
9
10 import org.xmlBlaster.util.StopWatch;
11 import java.util.logging.Logger;
12 import java.util.logging.Level;
13 import org.xmlBlaster.util.Global;
14 import org.xmlBlaster.util.XmlBlasterException;
15 import org.xmlBlaster.util.def.ErrorCode;
16 import org.xmlBlaster.client.qos.ConnectQos;
17 import org.xmlBlaster.client.qos.ConnectReturnQos;
18 import org.xmlBlaster.client.qos.PublishReturnQos;
19 import org.xmlBlaster.util.MsgUnitRaw;
20 import org.xmlBlaster.protocol.rmi.I_XmlBlaster;
21 import org.xmlBlaster.protocol.rmi.I_AuthServer;
22
23 import java.rmi.Naming;
24 import java.rmi.RemoteException;
25 import java.rmi.RMISecurityManager;
26 import java.rmi.NotBoundException;
27 import java.rmi.Remote;
28 import java.net.MalformedURLException;
29
30
31 /**
32 * This client demonstrates the method get() with RMI access.
33 * <p>
34 * It doesn't implement a Callback server, since it only access xmlBlaster
35 * using the synchronous get() method.
36 * <p>
37 * Invoke examples:<br />
38 * <pre>
39 * java -Djava.security.policy=${XMLBLASTER_HOME}/config/xmlBlaster.policy javaclients.rmi.ClientGet -loginName Jeff
40 *
41 * Options:
42 * -dispatch/clientside/plugin/rmi/hostname localhost // Where the server rmi registry is
43 * -dispatch/connection/plugin/rmi/registryPort 1099 // Port of server rmi registry
44 *
45 * or directly:
46 * -plugin/rmi/AuthServerUrl "rmi://localhost:1099/I_AuthServer"
47 * -plugin/rmi/XmlBlasterUrl "rmi://localhost:1099/I_XmlBlaster"
48 *
49 * </pre>
50 * @see <a href="http://java.sun.com/products/jdk/1.2/docs/guide/rmi/faq.html" target="others">RMI FAQ</a>
51 * @see <a href="http://archives.java.sun.com/archives/rmi-users.html" target="others">RMI USERS</a>
52 */
53 public class ClientGet
54 {
55 private static String ME = "Heidi";
56 private final Global glob;
57 private static Logger log = Logger.getLogger(ClientGet.class.getName());
58 private I_AuthServer authServer = null;
59 private I_XmlBlaster blasterServer = null;
60 private String sessionId = null;
61
62 public ClientGet(Global glob) {
63 this.glob = glob;
64
65 try {
66 initRmi();
67
68 String loginName = glob.getProperty().get("loginName", "RMIClient");
69 String passwd = glob.getProperty().get("passwd", "secret");
70 ConnectQos qos = new ConnectQos(glob, loginName, passwd);
71 log.info("Trying login to '" + loginName + "'");
72 String retXml = authServer.connect(qos.toXml());
73
74 // Parse the returned XML string and retrieve the secret sessionId
75 ConnectReturnQos conRetQos = new ConnectReturnQos(glob, retXml);
76 sessionId = conRetQos.getSecretSessionId();
77
78 String publishOid = "";
79 StopWatch stop = new StopWatch();
80
81 //----------- Construct a message and publish it ---------
82 {
83 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
84 "<key oid='' contentMime='text/xml'>" +
85 " <AGENT id='192.168.124.10' subId='1' type='generic'>" +
86 " <DRIVER id='FileProof' pollingFreq='10'>" +
87 " </DRIVER>"+
88 " </AGENT>" +
89 "</key>";
90 String content = "<file><size>1024 kBytes</size><creation>1.1.2000</creation></file>";
91 MsgUnitRaw msgUnit = new MsgUnitRaw(xmlKey, content.getBytes(), "<qos></qos>");
92 log.fine("Publishing ...");
93 stop.restart();
94 try {
95 String pubXml = blasterServer.publish(sessionId, msgUnit);
96
97 // Parse the returned XML string
98 PublishReturnQos q = new PublishReturnQos(glob, pubXml);
99 publishOid = q.getKeyOid();
100 log.info(" Returned oid=" + publishOid);
101 } catch(XmlBlasterException e) {
102 log.warning("XmlBlasterException: " + e.getMessage());
103 }
104 log.fine("Publishing done" + stop.nice());
105 }
106
107
108 //----------- get() the previous message OID -------
109 {
110 log.fine("get() using the exact oid ...");
111 String xmlKey = "<key oid='" + publishOid + "' queryType='EXACT'>\n" +
112 "</key>";
113 stop.restart();
114 MsgUnitRaw[] msgArr = null;
115 try {
116 msgArr = blasterServer.get(sessionId, xmlKey, "<qos></qos>");
117
118 log.info("Got " + msgArr.length + " messages:");
119 for (int ii=0; ii<msgArr.length; ii++) {
120 System.out.println(msgArr[ii].getKey() +
121 "\n################### RETURN CONTENT: ##################\n\n" +
122 new String(msgArr[ii].getContent()) +
123 "\n\n#######################################");
124 }
125 } catch(XmlBlasterException e) {
126 log.severe("XmlBlasterException: " + e.getMessage());
127 System.exit(1);
128 }
129 }
130
131
132 //----------- Construct a second message and publish it ---------
133 {
134 String xmlKey = "<?xml version='1.0' encoding='ISO-8859-1' ?>\n" +
135 "<key oid='Export-11' contentMime='text/plain'>" +
136 "<AGENT id='192.168.124.29' subId='1' type='generic'>" +
137 "<DRIVER id='ProgramExecute'>" +
138 "<EXECUTABLE>export</EXECUTABLE>" +
139 "<FILE>out.txt</FILE>" +
140 "</DRIVER>" +
141 "</AGENT>" +
142 "</key>";
143 String content = "Export program started";
144 MsgUnitRaw msgUnit = new MsgUnitRaw(xmlKey, content.getBytes(), "<qos></qos>");
145 log.fine("Publishing ...");
146 stop.restart();
147 try {
148 String pubXml = blasterServer.publish(sessionId, msgUnit);
149
150 // Parse the returned XML string
151 PublishReturnQos q = new PublishReturnQos(glob, pubXml);
152 publishOid = q.getKeyOid();
153 log.info(" Returned oid=" + publishOid);
154 } catch(XmlBlasterException e) {
155 log.warning("XmlBlasterException: " + e.getMessage());
156 }
157 log.fine("Publishing done" + stop.nice());
158 }
159
160
161 //----------- get() with XPath -------
162 log.fine("get() using the Xpath query syntax ...");
163 String xmlKey = "<key oid='' queryType='XPATH'>\n" +
164 " //DRIVER[@id='ProgramExecute']" +
165 "</key>";
166 stop.restart();
167 MsgUnitRaw[] msgArr = null;
168 try {
169 msgArr = blasterServer.get(sessionId, xmlKey, "<qos></qos>");
170 } catch(XmlBlasterException e) {
171 log.severe("XmlBlasterException: " + e.getMessage());
172 }
173
174 if (msgArr.length == 1)
175 log.info("Got " + msgArr.length + " messages:");
176 else
177 log.severe("Got " + msgArr.length + " messages:");
178 for (int ii=0; ii<msgArr.length; ii++) {
179 System.out.println(msgArr[ii].getKey() +
180 "\n################### RETURN CONTENT: ##################\n\n" +
181 new String(msgArr[ii].getContent()) +
182 "\n\n#######################################");
183 }
184
185
186 authServer.disconnect(sessionId, "<qos/>");
187 }
188 catch (RemoteException e) {
189 log.severe("Error occurred: " + e.toString());
190 e.printStackTrace();
191 }
192 catch (XmlBlasterException e) {
193 log.severe("XmlBlaster error occurred: " + e.toString());
194 e.printStackTrace();
195 }
196 }
197
198
199 /**
200 * Connect to RMI server.
201 * @param args
202 */
203 private void initRmi() throws XmlBlasterException {
204 // Create and install a security manager
205 if (System.getSecurityManager() == null) {
206 System.setSecurityManager(new RMISecurityManager());
207 if (log.isLoggable(Level.FINE)) log.fine("Started RMISecurityManager");
208 }
209
210 String hostname;
211 try {
212 java.net.InetAddress addr = java.net.InetAddress.getLocalHost();
213 hostname = addr.getHostName();
214 } catch (Exception e) {
215 log.warning("Can't determin your hostname");
216 hostname = "localhost";
217 }
218 hostname = glob.getProperty().get("dispatch/clientside/plugin/rmi/hostname", hostname);
219
220 // default xmlBlaster RMI publishing port is 1099
221 int registryPort = glob.getProperty().get("dispatch/connection/plugin/rmi/registryPort",
222 org.xmlBlaster.protocol.rmi.RmiDriver.DEFAULT_REGISTRY_PORT);
223 String prefix = "rmi://" + hostname + ":" + registryPort + "/";
224
225
226 String authServerUrl = prefix + "I_AuthServer";
227 String addr = glob.getProperty().get("AuthServerUrl", authServerUrl);
228 Remote rem = lookup(addr);
229 if (rem instanceof org.xmlBlaster.protocol.rmi.I_AuthServer) {
230 authServer = (I_AuthServer)rem;
231 log.info("Accessing reference using given '" + addr + "' string");
232 }
233 else {
234 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "InvalidRmiCallback", "No to '" + addr + "' possible, class needs to implement interface I_AuthServer.");
235 }
236
237
238 String xmlBlasterUrl = prefix + "I_XmlBlaster";
239 addr = glob.getProperty().get("XmlBlasterUrl", xmlBlasterUrl);
240 rem = lookup(addr);
241 if (rem instanceof org.xmlBlaster.protocol.rmi.I_XmlBlaster) {
242 blasterServer = (I_XmlBlaster)rem;
243 log.info("Accessing reference using given '" + addr + "' string");
244 }
245 else {
246 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "InvalidRmiCallback", "No to '" + addr + "' possible, class needs to implement interface I_XmlBlaster.");
247 }
248 }
249
250
251 /**
252 * Connect to RMI server.
253 * @param args
254 */
255 private Remote lookup(String addr) throws XmlBlasterException {
256 try {
257 return Naming.lookup(addr);
258 }
259 catch (RemoteException e) {
260 log.severe("Can't access address ='" + addr + "', no rmi registry running");
261 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "CallbackHandleInvalid", "Can't access address ='" + addr + "', no rmi registry running");
262 }
263 catch (NotBoundException e) {
264 log.severe("The given address ='" + addr + "' is not bound to rmi registry: " + e.toString());
265 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "CallbackHandleInvalid", "The given address '" + addr + "' is not bound to rmi registry: " + e.toString());
266 }
267 catch (MalformedURLException e) {
268 log.severe("The given address ='" + addr + "' is invalid: " + e.toString());
269 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "CallbackHandleInvalid", "The given address '" + addr + "' is invalid: " + e.toString());
270 }
271 catch (Throwable e) {
272 log.severe("The given address ='" + addr + "' is invalid : " + e.toString());
273 throw new XmlBlasterException(glob, ErrorCode.USER_CLIENTCODE, "CallbackHandleInvalid", "The given address '" + addr + "' is invalid : " + e.toString());
274 }
275 }
276
277
278 /**
279 */
280 public static void main(String args[]) {
281 new ClientGet(new Global(args));
282 }
283 }
syntax highlighted by Code2HTML, v. 0.9.1