KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > dyade > aaa > jndi2 > soap > SoapExt_NamingContextImpl


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2003 - ScalAgent Distributed Technologies
4  * Copyright (C) 1996 - Dyade
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA.
20  *
21  * Initial developer(s): Frederic Maistre (INRIA)
22  * Contributor(s): Nicolas Tachker (ScalAgent DT)
23  */

24 package fr.dyade.aaa.jndi2.soap;
25
26 import fr.dyade.aaa.jndi2.client.NamingContextImpl;
27 import fr.dyade.aaa.jndi2.client.Trace;
28
29 import org.apache.soap.Constants;
30 import org.apache.soap.Fault;
31 import org.apache.soap.SOAPException;
32 import org.apache.soap.encoding.soapenc.BeanSerializer;
33 import org.apache.soap.rpc.Call;
34 import org.apache.soap.rpc.Parameter;
35 import org.apache.soap.rpc.Response;
36 import org.apache.soap.server.DeploymentDescriptor;
37 import org.apache.soap.server.ServiceManagerClient;
38
39 import java.net.MalformedURLException JavaDoc;
40 import java.net.URL JavaDoc;
41 import java.util.Vector JavaDoc;
42 import java.util.Hashtable JavaDoc;
43 import java.util.Map JavaDoc;
44
45 import javax.naming.Context JavaDoc;
46 import javax.naming.NamingEnumeration JavaDoc;
47 import javax.naming.NamingException JavaDoc;
48
49 import org.objectweb.util.monolog.api.BasicLevel;
50 import org.objectweb.util.monolog.api.Logger;
51
52
53 /**
54  * The <code>SoapExt_NamingContextImpl</code> class is an extended
55  * <code>NamingContextImpl</code> calling a JNDI SOAP service's methods
56  * rather than using a TCP connection for interacting with the JNDI server.
57  */

58 public class SoapExt_NamingContextImpl extends NamingContextImpl
59 {
60   /** SOAP service's URL. */
61   private URL JavaDoc serviceUrl;
62   /** Call object used for binding requests. */
63   private Call bindCall = null;
64   /** Call object used for rebinding requests. */
65   private Call rebindCall = null;
66   /** Call object used for lookup requests. */
67   private Call lookupCall = null;
68   /** Call object used for unbinding requests. */
69   private Call unbindCall = null;
70
71
72   /**
73    * Constructs a <code>SoapExt_NamingContextImpl</code>, deploys and
74    * initializes the JNDI SOAP service.
75    *
76    * @param soapHost Host hosting the SOAP service.
77    * @param soapPort SOAP service's port.
78    * @param jndiHost Host hosting the JNDI server.
79    * @param jndiPort JNDI server's port.
80    *
81    * @exception NamingException If the SOAP service could not be initialized.
82    */

83   public SoapExt_NamingContextImpl(String JavaDoc soapHost, int soapPort,
84                                    String JavaDoc jndiHost, int jndiPort)
85          throws NamingException JavaDoc
86   {
87     super();
88
89     // Building the service URL:
90
try {
91       serviceUrl = new URL JavaDoc("http://" + soapHost + ":" + soapPort
92                            + "/soap/servlet/rpcrouter");
93     }
94     catch (MalformedURLException JavaDoc exc) {}
95
96     // Deploying and starting the service:
97
if (Trace.logger.isLoggable(BasicLevel.DEBUG))
98       Trace.logger.log(BasicLevel.DEBUG, "Starting the SOAP service on host "
99                                          + soapHost
100                                          + " listening on port "
101                                          + soapPort);
102     try {
103       ServiceManagerClient smc = new ServiceManagerClient(serviceUrl);
104       smc.deploy(getDeploymentDescriptor());
105     }
106     catch (Exception JavaDoc exc) {
107       NamingException JavaDoc nEx =
108         new NamingException JavaDoc("Could not deploy the SOAP service");
109       nEx.setRootCause(exc);
110       throw nEx;
111     }
112
113     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
114       Trace.logger.log(BasicLevel.DEBUG, "SOAP service deployed.");
115
116     // Initializing the service:
117
Call initCall = new Call();
118     initCall.setTargetObjectURI("urn:JndiService");
119     initCall.setMethodName("init");
120
121     Vector JavaDoc params = new Vector JavaDoc();
122     params.add(new Parameter("jndiHost", String JavaDoc.class, jndiHost, null));
123     params.add(new Parameter("jndiPort", Integer JavaDoc.class,
124                              new Integer JavaDoc(jndiPort), null));
125     initCall.setParams(params);
126
127     try {
128       Response resp = initCall.invoke(serviceUrl,"");
129     }
130     catch (Exception JavaDoc exc) {}
131
132     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
133       Trace.logger.log(BasicLevel.DEBUG, "SOAP service initialized.");
134   }
135
136
137   /**
138    * Binds an object.
139    *
140    * @exception NamingException If the binding fails or if the object could
141    * not be coded for the SOAP protocol.
142    */

143   public void bind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc
144   {
145     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
146       Trace.logger.log(BasicLevel.DEBUG, "SoapExt_NamingContextImpl.bind("
147                                          + name + ',' + obj + ')');
148
149     // Building the binding call if needed:
150
if (bindCall == null) {
151       bindCall = new Call();
152       bindCall.setTargetObjectURI("urn:JndiService");
153       bindCall.setMethodName("bind");
154       bindCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
155     }
156
157     // Coding the object:
158
Hashtable JavaDoc codedObj = SoapObjectHelper.soapCode(obj);
159
160     Vector JavaDoc params = new Vector JavaDoc();
161     params.add(new Parameter("name", String JavaDoc.class, name, null));
162     params.add(new Parameter("map", Hashtable JavaDoc.class, codedObj, null));
163
164     bindCall.setParams(params);
165
166     try {
167       Response resp = bindCall.invoke(serviceUrl,"");
168
169       // Check the response.
170
if (resp.generatedFault ()) {
171         throw new NamingException JavaDoc("The SOAP service failed to process"
172                                   + " the call: "
173                                   + resp.getFault().getFaultString());
174       }
175     }
176     catch (SOAPException exc) {
177       throw new NamingException JavaDoc("The SOAP call failed: " + exc.getMessage());
178     }
179   }
180
181   /**
182    * Rebinds an object.
183    *
184    * @exception NamingException If the binding fails or if the object could
185    * not be coded for the SOAP protocol.
186    */

187   public void rebind(String JavaDoc name, Object JavaDoc obj) throws NamingException JavaDoc
188   {
189     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
190       Trace.logger.log(BasicLevel.DEBUG, "SoapExt_NamingContextImpl.rebind("
191                                          + name + ',' + obj + ')');
192
193     // Building the rebinding call if needed:
194
if (rebindCall == null) {
195       rebindCall = new Call();
196       rebindCall.setTargetObjectURI("urn:JndiService");
197       rebindCall.setMethodName("rebind");
198       rebindCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
199     }
200
201     // Coding the object:
202
Hashtable JavaDoc codedObj = SoapObjectHelper.soapCode(obj);
203
204     Vector JavaDoc params = new Vector JavaDoc();
205     params.add(new Parameter("name", String JavaDoc.class, name, null));
206     params.add(new Parameter("map", Hashtable JavaDoc.class, codedObj, null));
207
208     rebindCall.setParams(params);
209
210     try {
211       Response resp = rebindCall.invoke(serviceUrl,"");
212
213       // Check the response.
214
if (resp.generatedFault ()) {
215         throw new NamingException JavaDoc("The SOAP service failed to process"
216                                   + " the call: "
217                                   + resp.getFault().getFaultString());
218       }
219     }
220     catch (SOAPException exc) {
221       throw new NamingException JavaDoc("The SOAP call failed: " + exc.getMessage());
222     }
223   }
224
225   /**
226    * Retrieves an object.
227    *
228    * @exception NamingException If the lookup fails or if the object could
229    * not be decoded.
230    */

231   public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
232   {
233     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
234       Trace.logger.log(BasicLevel.DEBUG, "SoapExt_NamingContextImpl.lookup("
235                                          + name + ')');
236
237     // Building the lookup call if needed:
238
if (lookupCall == null) {
239       lookupCall = new Call();
240       lookupCall.setTargetObjectURI("urn:JndiService");
241       lookupCall.setMethodName("lookup");
242       lookupCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
243     }
244
245     Vector JavaDoc params = new Vector JavaDoc();
246     params.add(new Parameter("name", String JavaDoc.class, name, null));
247
248     lookupCall.setParams(params);
249
250     Response resp = null;
251     try {
252       resp = lookupCall.invoke(serviceUrl,"");
253
254       // Check the response.
255
if (resp.generatedFault ()) {
256         throw new NamingException JavaDoc("The SOAP service failed to process"
257                                   + " the call: "
258                                   + resp.getFault().getFaultString());
259       }
260     }
261     catch (SOAPException exc) {
262       throw new NamingException JavaDoc("The SOAP call failed: " + exc.getMessage());
263     }
264
265     Map JavaDoc codedObj = (Map JavaDoc) resp.getReturnValue().getValue();
266     return SoapObjectHelper.soapDecode((Hashtable JavaDoc) codedObj);
267   }
268
269   /**
270    * Unbinds an object.
271    *
272    * @exception NamingException If the unbind fails.
273    */

274   public void unbind(String JavaDoc name) throws NamingException JavaDoc
275   {
276     if (Trace.logger.isLoggable(BasicLevel.DEBUG))
277       Trace.logger.log(BasicLevel.DEBUG, "SoapExt_NamingContextImpl.unbind("
278                                          + name + ')');
279     
280     // Building the unbind call if needed:
281
if (unbindCall == null) {
282       unbindCall = new Call();
283       unbindCall.setTargetObjectURI("urn:JndiService");
284       unbindCall.setMethodName("unbind");
285       unbindCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);
286     }
287
288     Vector JavaDoc params = new Vector JavaDoc();
289     params.add(new Parameter("name", String JavaDoc.class, name, null));
290
291     unbindCall.setParams(params);
292
293     Response resp = null;
294     try {
295       resp = unbindCall.invoke(serviceUrl,"");
296
297       // Check the response.
298
if (resp.generatedFault ()) {
299         throw new NamingException JavaDoc("The SOAP service failed to process"
300                                   + " the call: "
301                                   + resp.getFault().getFaultString());
302       }
303     }
304     catch (SOAPException exc) {
305       throw new NamingException JavaDoc("The SOAP call failed: " + exc.getMessage());
306     }
307   }
308
309   /**
310    * Method not implemented.
311    *
312    * @exception NamingException Systematically.
313    */

314   public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc
315   {
316     throw new NamingException JavaDoc("Method not implemented.");
317   }
318
319   /**
320    * Method not implemented.
321    *
322    * @exception NamingException Systematically.
323    */

324   public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc
325   {
326     throw new NamingException JavaDoc("Method not implemented.");
327   }
328
329   /**
330    * Method not implemented.
331    *
332    * @exception NamingException Systematically.
333    */

334   public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc
335   {
336     throw new NamingException JavaDoc("Method not implemented.");
337   }
338
339   /**
340    * Method not implemented.
341    *
342    * @exception NamingException Systematically.
343    */

344   public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc
345   {
346     throw new NamingException JavaDoc("Method not implemented.");
347   }
348
349
350   /**
351    * Returns a <code>DeploymentDescriptor</code> describing the JNDI SOAP
352    * service.
353    */

354   private DeploymentDescriptor getDeploymentDescriptor()
355   {
356     DeploymentDescriptor dd = new DeploymentDescriptor();
357     
358     dd.setID("urn:JndiService");
359
360     dd.setProviderType(DeploymentDescriptor.PROVIDER_JAVA);
361     dd.setProviderClass("fr.dyade.aaa.jndi2.soap.JndiSoapService");
362     dd.setScope(DeploymentDescriptor.SCOPE_APPLICATION);
363     
364     String JavaDoc[] methods = {"init", "bind", "rebind", "lookup", "unbind"};
365     dd.setMethods(methods);
366
367     String JavaDoc[] listener = {"org.apache.soap.server.DOMFaultListener"};
368     dd.setFaultListener(listener);
369
370     return dd;
371   }
372 }
373
Popular Tags