KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Jt > axis > JtAxisAdapter


1
2 package Jt.axis;
3 import java.util.*;
4 import java.lang.reflect.*;
5 import java.beans.*;
6 import java.io.*;
7 import Jt.*;
8 import Jt.xml.*;
9                     
10 import org.apache.axis.client.Call;
11 import org.apache.axis.client.Service;
12 import org.apache.axis.encoding.XMLType;
13 import org.apache.axis.utils.Options;
14
15 import javax.xml.namespace.QName JavaDoc;
16 import javax.xml.rpc.ParameterMode JavaDoc;
17
18
19 /**
20   * Jt Adapter for the Axis API.
21   */

22
23 public class JtAxisAdapter extends JtObject {
24
25
26   private String JavaDoc url = null;
27   private Service service = null;
28   private Call call = null;
29   private String JavaDoc remoteLogFile;
30
31
32   public JtAxisAdapter() {
33   }
34
35   // Attributes
36

37
38
39  /**
40   * Specifies the service URL.
41   * @param url service url
42   */

43
44   public void setUrl (String JavaDoc url) {
45      this.url = url;
46   }
47
48  /**
49    * Returns the service URL.
50    */

51
52   public String JavaDoc getUrl () {
53      return (url);
54   }
55
56
57  /**
58    * Returns the name of the remote log file
59    */

60
61   public String JavaDoc getRemoteLogFile () {
62     return (remoteLogFile);
63   }
64
65
66  /**
67   * Specifies the name of the remote log file
68   * @param remoteLogFile remote log file
69   */

70
71   public void setRemoteLogFile (String JavaDoc remoteLogFile) {
72     this.remoteLogFile = remoteLogFile;
73   }
74
75
76   // Process object messages
77

78   public Object JavaDoc processMessage (Object JavaDoc event) {
79
80    String JavaDoc msgid = null;
81    JtMessage e = (JtMessage) event;
82    Object JavaDoc content;
83
84      if (e == null)
85     return null;
86
87      msgid = (String JavaDoc) e.getMsgId ();
88
89      if (msgid == null)
90     return null;
91
92      content = e.getMsgContent();
93
94
95      if (msgid.equals ("JtSCRIPT")) {
96
97         return (processJtScript ((String JavaDoc) content));
98
99      }
100
101      if (msgid.equals ("JtREMOVE")) {
102         return (null);
103      }
104           
105      handleError ("JtAxisAdapter.processMessage: invalid message id:" + msgid);
106      return (null);
107
108   }
109
110
111     private Object JavaDoc processJtScript (String JavaDoc xmlMsg)
112     {
113
114       String JavaDoc ret = null;
115       JtXMLHelper xmlHelper = null;
116       JtMessage msg = new JtMessage ("JtCONVERT_XML_TO_OBJECT");
117       String JavaDoc xmlOutput = null;
118       Object JavaDoc tmp, tmp1, output = null;
119       JtIterator it;
120
121    
122         if (xmlMsg == null)
123           return (null);
124
125         try { // check xmlMsg
126

127             
128             if (service == null) {
129                        
130               service = new Service();
131               //service.setMaintainSession (true);
132

133
134               call = (Call) service.createCall();
135             
136             if (url == null) {
137               handleError ("processxmlMessage: invalid attribute value: url (null)");
138               return (null);
139             }
140                   
141
142             call.setTargetEndpointAddress( new java.net.URL JavaDoc(url) );
143             call.setMaintainSession (true);
144
145             // check
146
//call.setOperationName( new QName("http://example3.userguide.samples", "processMessage") );
147
call.setOperationName("processMessage");
148
149             call.addParameter( "arg1", XMLType.XSD_STRING, ParameterMode.IN);
150             call.setReturnType( org.apache.axis.encoding.XMLType.XSD_STRING );
151             }
152             ret = (String JavaDoc) call.invoke( new Object JavaDoc[] { xmlMsg } );
153             
154         } catch (Exception JavaDoc e) {
155             handleException (e);
156             return (null);
157         }
158
159
160         if (xmlHelper == null)
161           xmlHelper = new JtXMLHelper ();
162
163         // Convert XML message back to its object
164
// representation
165

166         msg.setMsgContent (ret);
167         //handleTrace ("JtAxisAdapter returned ...." + ret);
168

169
170         tmp = sendMessage (xmlHelper, msg);
171
172
173         // If it is a list, an exception was detected
174

175         if (tmp instanceof JtList) {
176
177           it = (JtIterator) getValue (tmp, "iterator");
178
179           if (it == null)
180             return (null);
181           
182           for (;;) {
183
184             //tmp1 = sendMessage (it, new JtMessage ("JtNEXT"));
185
tmp1 = it.processMessage (new JtMessage ("JtNEXT"));
186
187             if (tmp1 == null)
188               break;
189             if (tmp1 instanceof JtRemoteException) {
190               handleError ("JtAxisAdapter.processJtScript: remote exception detected");
191
192               handleWarning ("<Remote Exception>\n" +
193                 ((JtRemoteException) tmp1).getTrace () + "</Remote Exception>\n");
194               //setValue (this, "objException", tmp1);
195
} else
196               output = tmp1; // check size
197

198           }
199           return (output); // check
200
} else
201           return (tmp);
202     }
203
204
205 /**
206    * Unit tests all the message processed by JtAxisAdapter
207    */

208
209
210   public static void main(String JavaDoc[] args) {
211
212     JtObject main = new JtObject ();
213     JtMessage msg;
214     JtAxisAdapter adapter;
215     String JavaDoc st = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
216                 "<Object>\n" +
217                 "<classname>Jt.JtList</classname>\n" +
218                 "<Object>\n" +
219                 "<classname>Jt.JtMessage</classname>\n" +
220                 "<msgId>JtCREATE_OBJECT</msgId>\n" +
221                 "<msgContent>Jt.JtObject</msgContent>\n" +
222                 "<msgData>test</msgData>\n" +
223                 "</Object>\n";
224
225
226
227
228     //main.setObjTrace (1);
229
//main.setLogFile ("log.txt");
230

231     msg = new JtMessage ();
232
233
234     msg.setMsgId ("JtSCRIPT");
235     msg.setMsgContent ("Hello world");
236
237
238     // Create template object
239

240     adapter = (JtAxisAdapter) main.createObject ("Jt.service.JtAxisAdapter", "adapter");
241     main.setValue (adapter, "url",
242       "http://localhost:8080/axis/services/MyService");
243
244     main.sendMessage (adapter, msg);
245
246
247   }
248
249 }
250
251
252
Popular Tags