KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > axis > deployment > wsdd > WSDDDeployment


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Axis" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation. For more
52  * information on the Apache Software Foundation, please see
53  * <http://www.apache.org/>.
54  */

55 package org.jboss.axis.deployment.wsdd;
56
57 import org.jboss.axis.AxisEngine;
58 import org.jboss.axis.ConfigurationException;
59 import org.jboss.axis.Constants;
60 import org.jboss.axis.Handler;
61 import org.jboss.axis.WSDDEngineConfiguration;
62 import org.jboss.axis.encoding.DeserializerFactory;
63 import org.jboss.axis.encoding.SerializationContext;
64 import org.jboss.axis.encoding.SerializerFactory;
65 import org.jboss.axis.encoding.TypeMapping;
66 import org.jboss.axis.encoding.TypeMappingRegistry;
67 import org.jboss.axis.encoding.TypeMappingRegistryImpl;
68 import org.jboss.axis.encoding.ser.BaseDeserializerFactory;
69 import org.jboss.axis.encoding.ser.BaseSerializerFactory;
70 import org.jboss.axis.handlers.soap.SOAPService;
71 import org.jboss.axis.utils.Messages;
72 import org.jboss.logging.Logger;
73 import org.w3c.dom.Element JavaDoc;
74
75 import javax.xml.namespace.QName JavaDoc;
76 import java.io.IOException JavaDoc;
77 import java.util.ArrayList JavaDoc;
78 import java.util.HashMap JavaDoc;
79 import java.util.Hashtable JavaDoc;
80 import java.util.Iterator JavaDoc;
81 import java.util.Vector JavaDoc;
82
83
84 /**
85  * WSDD deployment element
86  *
87  * @author James Snell
88  * @author Glen Daniels (gdaniels@apache.org)
89  */

90 public class WSDDDeployment
91         extends WSDDElement
92         implements WSDDTypeMappingContainer,
93         WSDDEngineConfiguration
94 {
95    private static Logger log = Logger.getLogger(WSDDDeployment.class.getName());
96
97    private HashMap JavaDoc handlers = new HashMap JavaDoc();
98    private HashMap JavaDoc services = new HashMap JavaDoc();
99    private HashMap JavaDoc transports = new HashMap JavaDoc();
100    private Vector JavaDoc typeMappings = new Vector JavaDoc();
101    private WSDDGlobalConfiguration globalConfig = null;
102
103    /**
104     * Mapping of namespaces -> services
105     */

106    private HashMap JavaDoc namespaceToServices = new HashMap JavaDoc();
107    private AxisEngine engine;
108
109    protected void addHandler(WSDDHandler handler)
110    {
111       handlers.put(handler.getQName(), handler);
112    }
113
114    protected void addService(WSDDService service)
115    {
116       WSDDService oldService = (WSDDService)services.get(service.getQName());
117       if (oldService != null)
118       {
119          oldService.removeNamespaceMappings(this);
120       }
121       services.put(service.getQName(), service);
122    }
123
124    protected void addTransport(WSDDTransport transport)
125    {
126       transports.put(transport.getQName(), transport);
127    }
128
129
130    /**
131     * Put a WSDDHandler into this deployment, replacing any other
132     * WSDDHandler which might already be present with the same QName.
133     *
134     * @param handler a WSDDHandler to insert in this deployment
135     */

136    public void deployHandler(WSDDHandler handler)
137    {
138       handler.deployToRegistry(this);
139    }
140
141    /**
142     * Put a WSDDTransport into this deployment, replacing any other
143     * WSDDTransport which might already be present with the same QName.
144     *
145     * @param transport a WSDDTransport to insert in this deployment
146     */

147    public void deployTransport(WSDDTransport transport)
148    {
149       transport.deployToRegistry(this);
150    }
151
152    /**
153     * Put a WSDDService into this deployment, replacing any other
154     * WSDDService which might already be present with the same QName.
155     *
156     * @param service a WSDDHandler to insert in this deployment
157     */

158    public void deployService(WSDDService service)
159    {
160       service.deployToRegistry(this);
161    }
162
163    /**
164     * Remove a named handler
165     *
166     * @param qname the QName of the handler to remove
167     */

168    public void undeployHandler(QName JavaDoc qname)
169    {
170       handlers.remove(qname);
171    }
172
173    /**
174     * Remove a named service
175     *
176     * @param qname the QName of the service to remove
177     */

178    public void undeployService(QName JavaDoc qname)
179    {
180       WSDDService service = (WSDDService)services.get(qname);
181       if (service != null)
182       {
183          service.removeNamespaceMappings(this);
184          services.remove(qname);
185       }
186    }
187
188    /**
189     * Remove a named transport
190     *
191     * @param qname the QName of the transport to remove
192     */

193    public void undeployTransport(QName JavaDoc qname)
194    {
195       transports.remove(qname);
196    }
197
198    public void deployTypeMapping(WSDDTypeMapping typeMapping)
199            throws WSDDException
200    {
201       if (!typeMappings.contains(typeMapping))
202       {
203          typeMappings.add(typeMapping);
204       }
205       if (tmrDeployed)
206          deployMapping(typeMapping);
207    }
208
209    /**
210     * Default constructor
211     */

212    public WSDDDeployment()
213    {
214    }
215
216    /**
217     * Create an element in WSDD that wraps an extant DOM element
218     *
219     * @param e (Element) XXX
220     * @throws WSDDException XXX
221     */

222    public WSDDDeployment(Element JavaDoc e)
223            throws WSDDException
224    {
225       super(e);
226
227       Element JavaDoc[] elements = getChildElements(e, ELEM_WSDD_HANDLER);
228       int i;
229
230       for (i = 0; i < elements.length; i++)
231       {
232          WSDDHandler handler = new WSDDHandler(elements[i]);
233          deployHandler(handler);
234       }
235
236       elements = getChildElements(e, ELEM_WSDD_CHAIN);
237       for (i = 0; i < elements.length; i++)
238       {
239          WSDDChain chain = new WSDDChain(elements[i]);
240          deployHandler(chain);
241       }
242
243       elements = getChildElements(e, ELEM_WSDD_TRANSPORT);
244       for (i = 0; i < elements.length; i++)
245       {
246          WSDDTransport transport = new WSDDTransport(elements[i]);
247          deployTransport(transport);
248       }
249
250       elements = getChildElements(e, ELEM_WSDD_SERVICE);
251       for (i = 0; i < elements.length; i++)
252       {
253          try
254          {
255             WSDDService service = new WSDDService(elements[i]);
256             deployService(service);
257          }
258          catch (WSDDNonFatalException ex)
259          {
260             // If it's non-fatal, just keep on going
261
}
262          catch (WSDDException ex)
263          {
264             // otherwise throw it upwards
265
throw ex;
266          }
267       }
268
269       elements = getChildElements(e, ELEM_WSDD_TYPEMAPPING);
270       for (i = 0; i < elements.length; i++)
271       {
272          try
273          {
274             WSDDTypeMapping mapping = new WSDDTypeMapping(elements[i]);
275             deployTypeMapping(mapping);
276          }
277          catch (WSDDNonFatalException ex)
278          {
279             // If it's non-fatal, just keep on going
280
}
281          catch (WSDDException ex)
282          {
283             // otherwise throw it upwards
284
throw ex;
285          }
286       }
287
288       elements = getChildElements(e, ELEM_WSDD_BEANMAPPING);
289       for (i = 0; i < elements.length; i++)
290       {
291          WSDDBeanMapping mapping = new WSDDBeanMapping(elements[i]);
292          deployTypeMapping(mapping);
293       }
294
295       Element JavaDoc el = getChildElement(e, ELEM_WSDD_GLOBAL);
296       if (el != null)
297          globalConfig = new WSDDGlobalConfiguration(el);
298    }
299
300    protected QName JavaDoc getElementName()
301    {
302       return QNAME_DEPLOY;
303    }
304
305    public void deployToRegistry(WSDDDeployment target)
306            throws ConfigurationException
307    {
308
309       WSDDGlobalConfiguration global = getGlobalConfiguration();
310
311       if (global != null)
312       {
313          target.setGlobalConfiguration(global);
314       }
315
316       Iterator JavaDoc i = handlers.values().iterator();
317       while (i.hasNext())
318       {
319          WSDDHandler handler = (WSDDHandler)i.next();
320          target.deployHandler(handler);
321       }
322
323       i = transports.values().iterator();
324       while (i.hasNext())
325       {
326          WSDDTransport transport = (WSDDTransport)i.next();
327          target.deployTransport(transport);
328       }
329
330       i = services.values().iterator();
331       while (i.hasNext())
332       {
333          WSDDService service = (WSDDService)i.next();
334          service.deployToRegistry(target);
335       }
336
337       i = typeMappings.iterator();
338       while (i.hasNext())
339       {
340          WSDDTypeMapping mapping = (WSDDTypeMapping)i.next();
341          target.deployTypeMapping(mapping);
342       }
343    }
344
345    private void deployMapping(WSDDTypeMapping mapping)
346            throws WSDDException
347    {
348       try
349       {
350          String JavaDoc encodingStyle = mapping.getEncodingStyle();
351          if (encodingStyle == null)
352          {
353             encodingStyle = Constants.URI_DEFAULT_SOAP_ENC;
354          }
355          TypeMapping tm = tmr.getOrMakeTypeMapping(encodingStyle);
356
357          SerializerFactory ser = null;
358          DeserializerFactory deser = null;
359
360          // Try to construct a serializerFactory by introspecting for the
361
// following:
362
// public static create(Class javaType, QName xmlType)
363
// public <constructor>(Class javaType, QName xmlType)
364
// public <constructor>()
365
//
366
// The BaseSerializerFactory createFactory() method is a utility
367
// that does this for us.
368
//log.debug("start creating sf and df");
369
if (mapping.getSerializerName() != null &&
370                  !mapping.getSerializerName().equals(""))
371          {
372             ser = BaseSerializerFactory.createFactory(mapping.getSerializer(),
373                     mapping.getLanguageSpecificType(),
374                     mapping.getQName());
375          }
376          //log.debug("set ser factory");
377

378          if (mapping.getDeserializerName() != null &&
379                  !mapping.getDeserializerName().equals(""))
380          {
381             deser = BaseDeserializerFactory.createFactory(mapping.getDeserializer(),
382                     mapping.getLanguageSpecificType(),
383                     mapping.getQName());
384          }
385          //log.debug("set dser factory");
386
tm.register(mapping.getLanguageSpecificType(), mapping.getQName(), ser, deser);
387          //log.debug("registered");
388
}
389       catch (ClassNotFoundException JavaDoc e)
390       {
391          log.error(Messages.getMessage("unabletoDeployTypemapping00", mapping.getQName().toString()), e);
392          throw new WSDDNonFatalException(e);
393       }
394       catch (Exception JavaDoc e)
395       {
396          throw new WSDDException(e);
397       }
398    }
399
400    public void writeToContext(SerializationContext context)
401            throws IOException JavaDoc
402    {
403       context.registerPrefixForURI(NS_PREFIX_WSDD, URI_WSDD);
404
405       context.registerPrefixForURI(NS_PREFIX_WSDD_JAVA, URI_WSDD_JAVA);
406
407       context.startElement(QNAME_DEPLOY, null);
408
409       if (globalConfig != null)
410       {
411          globalConfig.writeToContext(context);
412       }
413
414       Iterator JavaDoc i = handlers.values().iterator();
415       while (i.hasNext())
416       {
417          WSDDHandler handler = (WSDDHandler)i.next();
418          handler.writeToContext(context);
419       }
420
421       i = services.values().iterator();
422       while (i.hasNext())
423       {
424          WSDDService service = (WSDDService)i.next();
425          service.writeToContext(context);
426       }
427
428       i = transports.values().iterator();
429       while (i.hasNext())
430       {
431          WSDDTransport transport = (WSDDTransport)i.next();
432          transport.writeToContext(context);
433       }
434
435       i = typeMappings.iterator();
436       while (i.hasNext())
437       {
438          WSDDTypeMapping mapping = (WSDDTypeMapping)i.next();
439          mapping.writeToContext(context);
440       }
441       context.endElement();
442    }
443
444    /**
445     * Get our global configuration
446     *
447     * @return XXX
448     */

449    public WSDDGlobalConfiguration getGlobalConfiguration()
450    {
451       return globalConfig;
452    }
453
454    public void setGlobalConfiguration(WSDDGlobalConfiguration globalConfig)
455    {
456       this.globalConfig = globalConfig;
457    }
458
459    /**
460     * @return XXX
461     */

462    public WSDDTypeMapping[] getTypeMappings()
463    {
464       WSDDTypeMapping[] t = new WSDDTypeMapping[typeMappings.size()];
465       typeMappings.toArray(t);
466       return t;
467    }
468
469    /**
470     * Return an array of the services in this deployment
471     */

472    public WSDDService[] getServices()
473    {
474       WSDDService[] serviceArray = new WSDDService[services.size()];
475       services.values().toArray(serviceArray);
476       return serviceArray;
477    }
478
479    /**
480     * Return the WSDD description for a given named service
481     */

482    public WSDDService getWSDDService(QName JavaDoc qname)
483    {
484       return (WSDDService)services.get(qname);
485    }
486
487    /**
488     * @param name XXX
489     * @return XXX
490     */

491    public Handler getHandler(QName JavaDoc name) throws ConfigurationException
492    {
493       WSDDHandler h = (WSDDHandler)handlers.get(name);
494       if (h != null)
495       {
496          return h.getInstance(this);
497       }
498
499       return null;
500    }
501
502    /**
503     * @param name XXX
504     * @return XXX
505     */

506    public Handler getTransport(QName JavaDoc name) throws ConfigurationException
507    {
508       WSDDTransport t = (WSDDTransport)transports.get(name);
509       if (t != null)
510       {
511          return t.getInstance(this);
512       }
513
514       return null;
515    }
516
517    /**
518     * @param name XXX
519     * @return XXX
520     */

521    public SOAPService getService(QName JavaDoc name) throws ConfigurationException
522    {
523       WSDDService s = (WSDDService)services.get(name);
524       if (s != null)
525       {
526          return (SOAPService)s.getInstance(this);
527       }
528
529       return null;
530    }
531
532    public SOAPService getServiceByNamespaceURI(String JavaDoc namespace)
533            throws ConfigurationException
534    {
535       WSDDService s = (WSDDService)namespaceToServices.get(namespace);
536       if (s != null)
537       {
538          return (SOAPService)s.getInstance(this);
539       }
540
541       return null;
542    }
543
544    public void configureEngine(AxisEngine engine)
545            throws ConfigurationException
546    {
547       this.engine = engine;
548    }
549
550    public void writeEngineConfig(AxisEngine engine) throws ConfigurationException
551    {
552    }
553
554    TypeMappingRegistry tmr = new TypeMappingRegistryImpl();
555
556    public TypeMapping getTypeMapping(String JavaDoc encodingStyle) throws ConfigurationException
557    {
558       return (TypeMapping)getTypeMappingRegistry().getTypeMapping(encodingStyle);
559    }
560
561    private boolean tmrDeployed = false;
562
563    public TypeMappingRegistry getTypeMappingRegistry() throws ConfigurationException
564    {
565       if (false == tmrDeployed)
566       {
567          for (int i = 0; i < typeMappings.size(); i++)
568          {
569             WSDDTypeMapping mapping = (WSDDTypeMapping)typeMappings.get(i);
570             deployMapping(mapping);
571          }
572          tmrDeployed = true;
573       }
574       return tmr;
575    }
576
577    public Handler getGlobalRequest() throws ConfigurationException
578    {
579       if (globalConfig != null)
580       {
581          WSDDRequestFlow reqFlow = globalConfig.getRequestFlow();
582          if (reqFlow != null)
583             return reqFlow.getInstance(this);
584       }
585
586       return null;
587    }
588
589    public Handler getGlobalResponse() throws ConfigurationException
590    {
591       if (globalConfig != null)
592       {
593          WSDDResponseFlow respFlow = globalConfig.getResponseFlow();
594          if (respFlow != null)
595             return respFlow.getInstance(this);
596       }
597
598       return null;
599    }
600
601    public Hashtable JavaDoc getGlobalOptions() throws ConfigurationException
602    {
603       return globalConfig.getParametersTable();
604    }
605
606    /**
607     * Get an enumeration of the services deployed to this engine
608     */

609    public Iterator JavaDoc getDeployedServices() throws ConfigurationException
610    {
611       ArrayList JavaDoc serviceDescs = new ArrayList JavaDoc();
612       for (Iterator JavaDoc i = services.values().iterator(); i.hasNext();)
613       {
614          WSDDService service = (WSDDService)i.next();
615          try
616          {
617             service.makeNewInstance(this);
618             serviceDescs.add(service.getServiceDesc());
619          }
620          catch (WSDDNonFatalException ex)
621          {
622             // If it's non-fatal, just keep on going
623
log.debug("Ingoring non-fatal exception: ", ex);
624          }
625       }
626       return serviceDescs.iterator();
627    }
628
629    /**
630     * Register a particular namepsace which maps to a given WSDDService.
631     * This will be used for namespace-based dispatching.
632     *
633     * @param namespace a namespace URI
634     * @param service the target WSDDService
635     */

636    public void registerNamespaceForService(String JavaDoc namespace,
637                                            WSDDService service)
638    {
639       namespaceToServices.put(namespace, service);
640    }
641
642    /**
643     * Remove a namespace -> WSDDService mapping.
644     *
645     * @param namespace the namespace URI to unmap
646     */

647    public void removeNamespaceMapping(String JavaDoc namespace)
648    {
649       namespaceToServices.remove(namespace);
650    }
651
652    public AxisEngine getEngine()
653    {
654       return engine;
655    }
656
657    public WSDDDeployment getDeployment()
658    {
659       return this;
660    }
661 }
662
Popular Tags