KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > axis > JtWebServicesAdapter


1 package Jt.axis;
2 import java.util.*;
3 import java.lang.reflect.*;
4 import java.beans.*;
5 import java.io.*;
6 import Jt.*;
7 import Jt.xml.*;
8
9
10
11 /**
12   * Jt Adapter for web services
13   */

14
15 public class JtWebServicesAdapter extends JtScriptInterpreter {
16
17   JtXMLConverter xmlConverter = null;
18   JtAxisAdapter axisAdapter = null;
19   private String JavaDoc url = null;
20   private String JavaDoc remoteLogFile;
21
22
23   public JtWebServicesAdapter () {
24   }
25
26
27  /**
28   * Specifies the service URL.
29   * @param url service url
30   */

31
32   public void setUrl (String JavaDoc url) {
33     this.url = url;
34   }
35
36  /**
37    * Returns the service URL.
38    */

39
40   public String JavaDoc getUrl () {
41     return (url);
42   }
43
44
45  /**
46    * Returns the name of the remote log file
47    */

48
49   public String JavaDoc getRemoteLogFile () {
50     return (remoteLogFile);
51   }
52
53
54  /**
55   * Specifies the name of the remote log file
56   * @param remoteLogFile remote log file
57   */

58
59   public void setRemoteLogFile (String JavaDoc remoteLogFile) {
60     this.remoteLogFile = remoteLogFile;
61   }
62
63
64
65
66
67   // Propagate Exceptions
68

69   private Exception JavaDoc propagateException (Object JavaDoc obj)
70   {
71     Exception JavaDoc ex;
72
73     if (obj == null)
74       return null;
75
76     ex = (Exception JavaDoc)
77      super.getValue (obj, "objException");
78
79     if (ex != null)
80       super.setValue (this, "objException", ex);
81
82     return (ex);
83   }
84
85
86   // Create the object that will perform the service
87
//
88

89
90   public Object JavaDoc createObject (Object JavaDoc class_name, Object JavaDoc id) {
91     String JavaDoc xmlMsg;
92     Object JavaDoc output;
93
94     if (class_name == null || id == null) { // check
95
handleError ("JtWebServiceAdapter.createObject: invalid parameters");
96       return (null);
97     }
98     
99     if (xmlConverter == null)
100       xmlConverter = new JtXMLConverter ();
101
102     xmlConverter.createObject (class_name, id);
103     //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));
104
xmlMsg = (String JavaDoc) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
105
106
107     output = doit (xmlMsg);
108     xmlConverter = null;
109
110     return (output);
111   }
112
113
114   private void updateRemoteLogFile () {
115
116     String JavaDoc xmlMsg;
117     JtMessage msg;
118     JtXMLConverter xmlconverter;
119
120
121     if (axisAdapter == null)
122       return;
123     
124     xmlConverter = new JtXMLConverter ();
125
126     xmlConverter.setValue ("this", "logFile", remoteLogFile);
127
128     xmlMsg = (String JavaDoc) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
129
130
131     msg = new JtMessage ("JtSCRIPT");
132     msg.setMsgContent (xmlMsg);
133
134     super.sendMessage (axisAdapter, msg);
135
136
137   }
138
139   public void removeObject (Object JavaDoc id) {
140     String JavaDoc xmlMsg;
141     Object JavaDoc output;
142
143     if (id == null) { // check
144
handleError ("JtWebServiceAdapter.createObject: invalid parameters");
145       return;
146     }
147     
148     if (xmlConverter == null)
149       xmlConverter = new JtXMLConverter ();
150
151     xmlConverter.removeObject (id);
152     //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));
153
xmlMsg = (String JavaDoc) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
154
155
156     output = doit (xmlMsg);
157     xmlConverter = null;
158
159
160   }
161
162
163   private Object JavaDoc doit (String JavaDoc xmlMsg) {
164
165     JtMessage msg;
166     Object JavaDoc output;
167
168     if (xmlMsg == null)
169      return (null); // check
170

171
172     //handleTrace ("JtWebServiceAdapter.doit() ...."
173
// + xmlMsg);
174

175     msg = new JtMessage ("JtSCRIPT");
176     msg.setMsgContent (xmlMsg);
177
178
179     if (url == null) {
180       handleError ("JtWebServiceAdapter.sendMessage: invalid url (null)");
181       return (null);
182     }
183
184     if (axisAdapter == null) {
185       axisAdapter = (JtAxisAdapter) super.createObject ("Jt.axis.JtAxisAdapter",
186         "adapter");
187       
188       super.setValue (axisAdapter, "url", url);
189       updateRemoteLogFile ();
190     }
191
192
193     output = super.sendMessage (axisAdapter, msg);
194
195     propagateException (axisAdapter);
196     return (output);
197
198
199   }
200
201
202   public Object JavaDoc sendMessage (Object JavaDoc id, Object JavaDoc msgid) {
203
204     String JavaDoc xmlMsg;
205     JtMessage msg;
206     Object JavaDoc output;
207     Exception JavaDoc ex;
208
209     if (id == null || msgid == null) {
210       handleError ("JtWebServiceAdapter.sendMessage: invalid parameters");
211       return (null);
212     }
213
214
215     if (xmlConverter == null)
216       xmlConverter = new JtXMLConverter ();
217
218
219     // Convert Jt API calls to XML calls
220

221     xmlConverter.sendMessage (id, msgid);
222
223     //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));
224
xmlMsg = (String JavaDoc) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
225
226     xmlConverter = null;
227
228     output = doit (xmlMsg);
229
230     //propagateException (axisAdapter);
231

232     return (output);
233
234   }
235
236  
237   public void setValue (Object JavaDoc id, Object JavaDoc att,
238     Object JavaDoc value) {
239     String JavaDoc xmlMsg;
240
241
242     if (id == null || att == null) { // check null value
243
handleError ("JtWebServiceAdapter.sendMessage: invalid parameters");
244       return;
245     }
246
247
248     if (xmlConverter == null)
249       xmlConverter = new JtXMLConverter ();
250
251     // Convert Jt call to XML
252

253     xmlConverter.setValue (id, att, value);
254     //xmlMsg = (String) super.sendMessage (xmlConverter, new JtMessage ("JtCONVERT"));
255
xmlMsg = (String JavaDoc) xmlConverter.processMessage (new JtMessage ("JtCONVERT"));
256
257     xmlConverter = null;
258
259     doit (xmlMsg);
260     //propagateException (axisAdapter);
261
}
262
263
264   // Process object messages
265

266   public Object JavaDoc processMessage (Object JavaDoc event) {
267
268    String JavaDoc msgid = null;
269    JtMessage e = (JtMessage) event;
270    Object JavaDoc content;
271
272      if (e == null)
273     return null;
274
275      msgid = (String JavaDoc) e.getMsgId ();
276
277      if (msgid == null)
278     return null;
279
280      content = e.getMsgContent();
281
282      handleError ("processMessage: invalid message ID:" + msgid);
283      return (null);
284
285
286
287   }
288
289
290
291  /**
292    * Unit tests all the messages processed by JtWebServiceAdapter.
293    */

294
295   public static void main(String JavaDoc[] args) {
296
297     JtObject main = new JtObject ();
298
299     JtWebServicesAdapter myService;
300     String JavaDoc str;
301     String JavaDoc reply = null;
302     Exception JavaDoc ex;
303
304     //main.setObjTrace (1);
305
//main.setLogFile ("log.txt");
306

307
308     // Create the Jt Web service Adapter
309

310     myService = (JtWebServicesAdapter) main.createObject ("Jt.axis.JtWebServicesAdapter",
311      "service");
312
313     // Set the service url property (if needed)
314

315     if (main.getValue (myService, "url") == null)
316       main.setValue (myService, "url",
317         "http://localhost:8080/axis/services/JtAxisService");
318
319     // Set the service remoteLogFile property (if needed)
320

321     if (main.getValue (myService, "remoteLogFile") == null)
322       main.setValue (myService, "remoteLogFile",
323        "c:\\log.txt");
324
325
326     // Create a remote instance of the HelloWorld class. The Jt Web service adapter
327
// can be used to create remote instances of any Jt Framework class.
328
// HelloWorld is used as an example.
329

330     myService.createObject ("Jt.examples.HelloWorld", "helloWorld");
331     //myService.createObject ("Jt.JtCommand", "command");
332

333
334     // Set the greetingMessage attribute
335

336     myService.setValue ("helloWorld", "greetingMessage", "Hello there...");
337
338
339     // Create a remote message (JtHello)
340

341     myService.createObject ("Jt.JtMessage", "message");
342     myService.setValue ("message", "msgId", "JtHello");
343
344
345     // Send JtHello to the remote helloWorld object
346

347     reply = (String JavaDoc) myService.sendMessage ("helloWorld", "message");
348
349     ex = (Exception JavaDoc) main.getValue (myService, "objException");
350     
351
352     // Display the reply unless an exception is detected
353

354     if (ex == null)
355       System.out.println (reply);
356
357     // Remove the service instance
358

359     myService.removeObject ("helloWorld");
360
361
362   }
363
364 }
365
366
367
Popular Tags