KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > net > axis > server > AxisService


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 // $Id: AxisService.java,v 1.47.2.4 2005/03/02 14:19:50 tdiesler Exp $
9

10 package org.jboss.net.axis.server;
11
12 import org.jboss.axis.AxisProperties;
13 import org.jboss.axis.EngineConfiguration;
14 import org.jboss.axis.EngineConfigurationFactory;
15 import org.jboss.axis.configuration.EngineConfigurationFactoryFinder;
16 import org.jboss.axis.deployment.wsdd.WSDDUndeployment;
17 import org.jboss.axis.server.AxisServer;
18 import org.jboss.deployment.DeploymentException;
19 import org.jboss.deployment.DeploymentInfo;
20 import org.jboss.deployment.SubDeployerSupport;
21 import org.jboss.metadata.MetaData;
22 import org.jboss.net.axis.AttacheableService;
23 import org.jboss.net.axis.Deployment;
24 import org.jboss.net.axis.XMLResourceProvider;
25 import org.jboss.util.xml.JBossEntityResolver;
26 import org.w3c.dom.Attr JavaDoc;
27 import org.w3c.dom.Document JavaDoc;
28 import org.w3c.dom.Element JavaDoc;
29 import org.w3c.dom.NamedNodeMap JavaDoc;
30 import org.w3c.dom.Node JavaDoc;
31 import org.w3c.dom.NodeList JavaDoc;
32
33 import javax.management.MBeanRegistration JavaDoc;
34 import javax.naming.InitialContext JavaDoc;
35 import javax.naming.NamingException JavaDoc;
36 import javax.xml.parsers.DocumentBuilder JavaDoc;
37 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
38 import javax.xml.rpc.server.ServiceLifecycle JavaDoc;
39 import java.net.URL JavaDoc;
40
41 /**
42  * <p>
43  * A deployer service that installs Axis and manages Web-Services
44  * within the Jboss JMX environment. It is the base for all plain axis
45  * deployments, web service archive support and also (via an additional
46  * layer) WS4EE functionality.
47  * </p>
48  * @since 27. September 2001
49  * @author <a HREF="mailto:Christoph.Jung@infor.de">Christoph G. Jung</a>
50  * @version $Revision: 1.47.2.4 $
51  * @jmx.mbean
52  * name="jboss.net:service=Axis"
53  * description="web service deployer"
54  * extends="org.jboss.deployment.SubDeployerMBean"
55  */

56
57 public class AxisService
58         extends SubDeployerSupport
59         implements AxisServiceMBean, MBeanRegistration JavaDoc
60 {
61
62    //
63
// Attributes
64
//
65

66    /** the engine belonging to this service */
67    protected AxisServer axisServer;
68
69    /** the client configuration belonging to this service*/
70    protected XMLResourceProvider clientConfiguration;
71
72    /** the server configuration belonging to this service*/
73    protected XMLResourceProvider serverConfiguration;
74
75    /** the initial context into which we bind external web-service proxies */
76    protected InitialContext JavaDoc initialContext;
77
78    /** document builder */
79    protected DocumentBuilder JavaDoc documentBuilder;
80
81    /** whether we are validating */
82    protected boolean validateDTDs;
83
84    //
85
// Constructors
86
//
87

88    /** default */
89    public AxisService()
90    {
91    }
92
93    //
94
// Some helper methods
95
//
96

97    /**
98     * this tiny helper copies all children of the given element that
99     * are elements and match the given name to the other element
100     */

101    protected void copyChildren(Document JavaDoc sourceDoc,
102                                Element JavaDoc source,
103                                String JavaDoc match,
104                                Element JavaDoc target)
105    {
106       NodeList JavaDoc children = source.getChildNodes();
107       for (int count = 0; count < children.getLength(); count++)
108       {
109          Node JavaDoc actNode = children.item(count);
110          if (actNode instanceof Element JavaDoc)
111          {
112             if (((Element JavaDoc)actNode).getLocalName().equals(match))
113             {
114                target.appendChild(sourceDoc.importNode(actNode, true));
115             }
116          }
117       }
118    }
119
120    /** starts a single axis deployment */
121    protected void startAxisDeployment(String JavaDoc deploymentName, Element JavaDoc root, ClassLoader JavaDoc deploymentLoader)
122            throws DeploymentException
123    {
124
125       try
126       {
127
128          // the deployment command document
129
Document JavaDoc deployDoc = documentBuilder.newDocument();
130          // the client deployment command document
131
Document JavaDoc deployClientDoc = documentBuilder.newDocument();
132          // create command
133
Element JavaDoc deploy =
134                  deployDoc.createElementNS(root.getNamespaceURI(), "deployment");
135          // create command
136
Element JavaDoc deployClient =
137                  deployClientDoc.createElementNS(root.getNamespaceURI(),
138                          "deployment");
139
140          NamedNodeMap JavaDoc attributes = root.getAttributes();
141          for (int count = 0; count < attributes.getLength(); count++)
142          {
143             Attr JavaDoc attribute = (Attr JavaDoc)attributes.item(count);
144             deploy.setAttributeNodeNS((Attr JavaDoc)deployDoc.importNode(attribute, true));
145             deployClient.setAttributeNodeNS((Attr JavaDoc)deployClientDoc.importNode(attribute, true));
146          }
147
148          // and insert the nodes from the original document
149
// and sort out the ejb-ref extensions
150
NodeList JavaDoc children = root.getChildNodes();
151          for (int count = 0; count < children.getLength(); count++)
152          {
153             Node JavaDoc actNode = children.item(count);
154             if (actNode instanceof Element JavaDoc)
155             {
156                Element JavaDoc actElement = (Element JavaDoc)actNode;
157
158                if (actElement.getTagName().equals("ejb-ref"))
159                {
160
161                   String JavaDoc refName =
162                           MetaData.getElementContent(MetaData.getUniqueChild((Element JavaDoc)actNode,
163                                   "ejb-ref-name"));
164                   String JavaDoc linkName =
165                           MetaData.getElementContent(MetaData.getUniqueChild((Element JavaDoc)actNode, "ejb-link"));
166
167                   log.warn("Web Service Deployment "
168                           + deploymentName
169                           + " makes use of the deprecated ejb-ref feature. "
170                           + "Please adjust any ejb-providing service tag inside your web-service.xml pointing to "
171                           + refName
172                           + " to use the absolute "
173                           + linkName
174                           + " instead.");
175
176                }
177                else if (actElement.getTagName().equals("ext-service"))
178                {
179                   deployExternalWebService(actElement);
180                }
181                else
182                {
183                   if (!actElement.getTagName().equals("service"))
184                   {
185                      deployClient.appendChild(deployClientDoc.importNode(actNode, true));
186                   }
187                   deploy.appendChild(deployDoc.importNode(actNode, true));
188                }
189             }
190             else
191             {
192                deployClient.appendChild(deployClientDoc.importNode(actNode, true));
193                deploy.appendChild(deployDoc.importNode(actNode, true));
194             }
195          }
196
197          // insert command into document
198
deployDoc.appendChild(deploy);
199          deployClientDoc.appendChild(deployClient);
200
201          Deployment.makeSafeDeployment(deploy, deploymentLoader).deployToRegistry(((XMLResourceProvider)axisServer.getConfig()).getDeployment());
202          Deployment.makeSafeDeployment(deployClient, deploymentLoader).deployToRegistry(clientConfiguration.getDeployment());
203          axisServer.refreshGlobalOptions();
204          axisServer.saveConfiguration();
205       }
206       catch (Exception JavaDoc e)
207       {
208          throw new DeploymentException(Constants.COULD_NOT_DEPLOY_DESCRIPTOR,
209                  e);
210       }
211    }
212
213    /** stops a single axis deployment */
214    protected void stopAxisDeployment(Element JavaDoc root) throws DeploymentException
215    {
216       try
217       {
218          // from which we extract an undeployment counterpart
219
Document JavaDoc undeployDoc = documentBuilder.newDocument();
220          Element JavaDoc undeploy =
221                  undeployDoc.createElementNS(root.getNamespaceURI(), "undeployment");
222
223          // copy over administrational attributes
224
NamedNodeMap JavaDoc attributes = root.getAttributes();
225          for (int count = 0; count < attributes.getLength(); count++)
226          {
227             Attr JavaDoc attribute = (Attr JavaDoc)attributes.item(count);
228             undeploy.setAttributeNodeNS((Attr JavaDoc)undeployDoc.importNode(attribute, true));
229          }
230
231          // external services are just a matter of us
232
NodeList JavaDoc children = root.getElementsByTagName("ext-service");
233          for (int count = 0; count < children.getLength(); count++)
234          {
235             Element JavaDoc actNode = (Element JavaDoc)children.item(count);
236             undeployExternalWebService(actNode);
237          }
238
239          // all service and handler entries are copied in the
240
// undeployment document
241
copyChildren(undeployDoc, root, "service", undeploy);
242
243          // axis should be able to do that on its own
244
children = root.getElementsByTagName("service");
245          for (int count = 0; count < children.getLength(); count++)
246          {
247             Element JavaDoc actNode = (Element JavaDoc)children.item(count);
248             String JavaDoc serviceName = actNode.getAttribute("name");
249             ServiceLifecycle JavaDoc slc = (ServiceLifecycle JavaDoc)axisServer.
250                     getApplicationSession().get(serviceName);
251             if (slc != null)
252             {
253                slc.destroy();
254                axisServer.getApplicationSession().remove(serviceName);
255             }
256          }
257
258          copyChildren(undeployDoc, root, "handler", undeploy);
259          copyChildren(undeployDoc, root, "typemapping", undeploy);
260          copyChildren(undeployDoc, root, "beanmapping", undeploy);
261
262          // put command into document
263
undeployDoc.appendChild(undeploy);
264
265          // and call the administrator
266
new WSDDUndeployment(undeploy).undeployFromRegistry(((XMLResourceProvider)axisServer.getConfig()).getDeployment());
267          // and call the administrator
268
new WSDDUndeployment(undeploy).undeployFromRegistry(clientConfiguration.getDeployment());
269          axisServer.refreshGlobalOptions();
270          axisServer.saveConfiguration();
271       }
272       catch (Exception JavaDoc e)
273       {
274          throw new DeploymentException(Constants.COULD_NOT_UNDEPLOY, e);
275       }
276    }
277
278    /**
279     * deploy an external web service reference
280     */

281
282    protected synchronized void deployExternalWebService(Element JavaDoc deployElement)
283            throws DeploymentException
284    {
285
286       try
287       {
288          if (initialContext == null)
289          {
290             initialContext = new InitialContext JavaDoc();
291          }
292
293          NamedNodeMap JavaDoc attributes = deployElement.getAttributes();
294
295          String JavaDoc jndiName = attributes.getNamedItem("jndiName").getNodeValue();
296          String JavaDoc serviceClassName =
297                  attributes.getNamedItem("serviceImplClass").getNodeValue();
298          Object JavaDoc factory =
299                  new AttacheableService(serviceClassName,
300                          serviceName.getCanonicalName());
301          initialContext.bind(jndiName, factory);
302       }
303       catch (NamingException JavaDoc e)
304       {
305          throw new DeploymentException("Could not deploy item " + deployElement,
306                  e);
307       }
308    }
309
310    /** undeploys an external web service reference */
311    protected synchronized void undeployExternalWebService(Element JavaDoc deployElement)
312    {
313       try
314       {
315          if (initialContext == null)
316          {
317             initialContext = new InitialContext JavaDoc();
318          }
319
320          NamedNodeMap JavaDoc attributes = deployElement.getAttributes();
321
322          String JavaDoc jndiName = attributes.getNamedItem("jndiName").getNodeValue();
323
324          if (jndiName != null)
325          {
326             initialContext.unbind(jndiName);
327          }
328       }
329       catch (NamingException JavaDoc e)
330       {
331          // what?
332
log.warn("Could not undeploy " + deployElement,
333                  e);
334       }
335    }
336
337    //----------------------------------------------------------------------------
338
// 'service' interface
339
//----------------------------------------------------------------------------
340

341    /**
342     * start service means
343     * - initialise axis engine
344     * - register Axis servlet in WebContainer
345     * - contact the maindeployer
346     */

347    protected void startService() throws Exception JavaDoc
348    {
349
350       DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
351       dbf.setNamespaceAware(true);
352       // this will (along JAXP in conjunction with
353
// validation+namespace-awareness) enable xml schema checking.
354
// Will currently fail because some J2EE1.4/W3C schemas
355
// are still lacking.
356
//dbf.setAttribute
357
// ("http://java.sun.com/xml/jaxp/properties/schemaLanguage","http://www.w3.org/2001/XMLSchema");
358
dbf.setValidating(validateDTDs);
359
360       documentBuilder = dbf.newDocumentBuilder();
361       documentBuilder.setEntityResolver(new JBossEntityResolver());
362
363       // find the global config file in classpath
364
URL JavaDoc resource =
365               getClass().getClassLoader().getResource(Constants.AXIS_SERVER_CONFIGURATION_FILE);
366
367       if (resource == null)
368       {
369          log.warn("Could not find: " + Constants.AXIS_SERVER_CONFIGURATION_FILE);
370          throw new Exception JavaDoc(Constants.COULD_NOT_FIND_AXIS_CONFIGURATION_0);
371       }
372
373       serverConfiguration = new XMLResourceProvider(resource, getClass().getClassLoader());
374
375       axisServer = new AxisServer(serverConfiguration);
376
377       // we annotate the server configuration with our serviceName
378
serverConfiguration.getGlobalOptions().put(org.jboss.net.axis.Constants.CONFIGURATION_CONTEXT,
379               serviceName.toString());
380
381       // find the client config file in classpath
382
resource =
383               getClass().getClassLoader().getResource(Constants.AXIS_CLIENT_CONFIGURATION_FILE);
384
385       if (resource == null)
386       {
387          log.warn("Failed to find: " + Constants.AXIS_SERVER_CONFIGURATION_FILE);
388          throw new Exception JavaDoc(Constants.COULD_NOT_FIND_AXIS_CONFIGURATION_0);
389       }
390
391       clientConfiguration = new XMLResourceProvider(resource, getClass().getClassLoader());
392       clientConfiguration.buildDeployment();
393       clientConfiguration.getGlobalOptions().put(org.jboss.net.axis.Constants.CONFIGURATION_CONTEXT,
394               serviceName.toString());
395
396       // make sure that Axis/Discovery wont register any application classloaders for system lookup
397
AxisProperties.getNameDiscoverer();
398       // register our client configuration there
399
Class JavaDoc initializeThisStaticStuff =
400               EngineConfigurationFactoryFinder.class;
401       System.setProperty(EngineConfigurationFactory.SYSTEM_PROPERTY_NAME,
402               JMXEngineConfigurationFactory.class.getName());
403       super.startService();
404    }
405
406    /** what to do to stop axis temporarily --> undeploy the servlet */
407    protected void stopService() throws Exception JavaDoc
408    {
409       super.stopService();
410       axisServer.stop();
411       documentBuilder = null;
412    }
413
414    //----------------------------------------------------------------------------
415
// Deployer interface
416
//----------------------------------------------------------------------------
417

418    /* (non-javadoc)
419     * @see org.jboss.deployment.SubDeployer#accepts(DeploymentInfo)
420     */

421
422    public boolean accepts(DeploymentInfo sdi)
423    {
424       if (sdi.shortName.endsWith("-axis.xml")
425               || sdi.localCl.getResource(Constants.WEB_SERVICE_DESCRIPTOR) != null)
426       {
427          return true;
428       }
429       return false;
430    }
431
432    /* (non-javadoc)
433    * @see org.jboss.deployment.SubDeployerSupport#init(DeploymentInfo)
434    */

435    public void init(DeploymentInfo sdi) throws DeploymentException
436    {
437       super.init(sdi);
438
439       try
440       {
441          if (sdi.document == null)
442          {
443             if (sdi.documentUrl == null)
444             {
445                if (sdi.isXML)
446                {
447                   sdi.documentUrl = sdi.localUrl;
448                }
449                else
450                {
451                   sdi.documentUrl =
452                           sdi.localCl.getResource(Constants.WEB_SERVICE_DESCRIPTOR);
453                }
454             }
455             sdi.document = documentBuilder.parse(sdi.documentUrl.openStream());
456          }
457
458          // Resolve what to watch
459
if (sdi.isDirectory)
460          {
461             sdi.watch = new URL JavaDoc(sdi.url, Constants.WEB_SERVICE_DESCRIPTOR);
462          }
463          else
464          {
465             // We watch the top only, no directory support
466
sdi.watch = sdi.url;
467          }
468       }
469       catch (Exception JavaDoc e)
470       {
471          throw new DeploymentException(e);
472       }
473    }
474
475    /* (non-javadoc)
476    * @see org.jboss.deployment.SubDeployerSupport#create(DeploymentInfo)
477    */

478    public void create(DeploymentInfo sdi) throws DeploymentException
479    {
480       log.debug("create, sdi=" + sdi);
481       super.create(sdi);
482    }
483
484    /* (non-javadoc)
485    * @see org.jboss.deployment.SubDeployerSupport#start(DeploymentInfo)
486    */

487    public void start(DeploymentInfo sdi) throws DeploymentException
488    {
489       log.debug("start, sdi=" + sdi);
490       super.start(sdi);
491
492       start(sdi.toString(), sdi.document, sdi.ucl);
493    }
494
495    /**
496     * start method for usage with immediately known documents
497     * @jmx.managed-operation
498     */

499
500    public void start(String JavaDoc deploymentName,
501                      Document JavaDoc doc,
502                      ClassLoader JavaDoc serviceLoader)
503            throws DeploymentException
504    {
505
506       // the original command
507
Element JavaDoc root = doc.getDocumentElement();
508
509       if (root.getNodeName().equals("deployments"))
510       {
511          NodeList JavaDoc children = root.getChildNodes();
512
513          for (int count = 0; count < children.getLength(); count++)
514          {
515             startAxisDeployment(deploymentName,
516                     (Element JavaDoc)children.item(count), serviceLoader);
517          }
518       }
519       else
520       {
521          startAxisDeployment(deploymentName, root, serviceLoader);
522       }
523    }
524
525    /* (non-javadoc)
526    * @see org.jboss.deployment.SubDeployerSupport#start(DeploymentInfo)
527    */

528    public void stop(DeploymentInfo sdi) throws DeploymentException
529    {
530       log.debug("stop, sdi=" + sdi);
531       super.stop(sdi);
532       stop(sdi.document);
533    }
534
535    /** stop a given deployment by document
536     * @jmx.managed-operation
537     */

538    public void stop(Document JavaDoc doc) throws DeploymentException
539    {
540       // this was the deployment command
541
Element JavaDoc root = (Element JavaDoc)doc.getDocumentElement();
542       if (root.getNodeName().equals("deployments"))
543       {
544          NodeList JavaDoc children = root.getChildNodes();
545          for (int count = 0; count < children.getLength(); count++)
546          {
547             stopAxisDeployment((Element JavaDoc)children.item(count));
548          }
549       }
550       else
551       {
552          stopAxisDeployment(root);
553       }
554    }
555
556    /* (non-javadoc)
557    * @see org.jboss.deployment.SubDeployerSupport#start(DeploymentInfo)
558    */

559    public void destroy(DeploymentInfo sdi) throws DeploymentException
560    {
561       log.debug("destroy, sdi=" + sdi);
562       super.destroy(sdi);
563    }
564
565    /**
566     * return the associated client configuration
567     * @jmx.managed-operation
568     */

569    public EngineConfiguration getClientEngineConfiguration()
570    {
571       return clientConfiguration;
572    }
573
574    /**
575     * return the associated server configuration
576     * @jmx.managed-operation
577     */

578    public EngineConfiguration getServerEngineConfiguration()
579    {
580       return serverConfiguration;
581    }
582
583    /**
584     * return the associated server
585     * @jmx.managed-operation
586     */

587    public AxisServer getAxisServer()
588    {
589       return axisServer;
590    }
591
592 }
593
Popular Tags