KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > webservice > ServiceDeployerJSE


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: ServiceDeployerJSE.java,v 1.12.2.7 2005/06/16 09:53:06 tdiesler Exp $
9
package org.jboss.webservice;
10
11 // $Id: ServiceDeployerJSE.java,v 1.12.2.7 2005/06/16 09:53:06 tdiesler Exp $
12

13 import org.dom4j.Document;
14 import org.dom4j.io.OutputFormat;
15 import org.dom4j.io.SAXReader;
16 import org.dom4j.io.XMLWriter;
17 import org.jboss.deployment.DeploymentException;
18 import org.jboss.deployment.DeploymentInfo;
19 import org.jboss.logging.Logger;
20 import org.jboss.webservice.metadata.PortComponentMetaData;
21 import org.jboss.webservice.metadata.WebserviceDescriptionMetaData;
22 import org.jboss.webservice.metadata.WebservicesMetaData;
23 import org.jboss.webservice.server.ServiceEndpointServletJSE;
24 import org.jboss.util.xml.JBossEntityResolver;
25
26 import javax.management.ObjectName JavaDoc;
27 import java.io.File JavaDoc;
28 import java.io.FileOutputStream JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.net.URL JavaDoc;
31
32 /**
33  * A deployer service that manages WS4EE compliant Web-Services for WAR
34  *
35  * @author Thomas.Diesler@jboss.org
36  * @jmx.mbean name="jboss.ws4ee:service=ServiceDeployerJSE"
37  * description="Webservice WAR deployer"
38  * extends="org.jboss.webservice.ServiceDeployerMBean"
39  * @since 15-April-2004
40  */

41 public class ServiceDeployerJSE extends ServiceDeployer implements ServiceDeployerJSEMBean
42 {
43    // provide logging
44
private final Logger log = Logger.getLogger(ServiceDeployer.class);
45
46    // service name of the EJB deployer
47
private ObjectName JavaDoc warDeployer;
48
49    /**
50     * Set the service name of the WAR deployer
51     *
52     * @jmx.managed-attribute
53     */

54    public void setWARDeployer(ObjectName JavaDoc deployerName)
55    {
56       this.warDeployer = deployerName;
57    }
58
59    /**
60     * Register this service as NotificationListener to the WARDeployer
61     */

62    protected void startService() throws Exception JavaDoc
63    {
64       super.startService();
65       registerNotificationListener(warDeployer);
66    }
67
68    /**
69     * Unregister this service as NotificationListener from the WARDeployer
70     */

71    protected void stopService() throws Exception JavaDoc
72    {
73       unregisterNotificationListener(warDeployer);
74       super.stopService();
75    }
76
77    /**
78     * Get the resource name of the webservices.xml descriptor.
79     */

80    protected URL JavaDoc getWebservicesDescriptor(DeploymentInfo di)
81    {
82       if (di.localCl != null)
83          return di.localCl.findResource("WEB-INF/webservices.xml");
84       else
85          return null;
86    }
87
88    /** Is called when the parent deployer sends the CREATE_NOTIFICATION.
89     *
90     * This implemantation modifies the servlet entries in web.xml
91     */

92    protected void createWebservice(DeploymentInfo di) throws DeploymentException
93    {
94       // parse webservices.xml and put it in the local registry.
95
super.createWebservice(di);
96
97       WebservicesMetaData webservices = (WebservicesMetaData)webservicesMap.get(di.url);
98       modifyWebXML(di, webservices);
99    }
100
101    /** Modify the deployed web.xml
102     */

103    private void modifyWebXML(DeploymentInfo di, WebservicesMetaData webservices)
104            throws DeploymentException
105    {
106       if (webservices == null)
107          throw new DeploymentException("webservices.xml not registerd");
108
109       File JavaDoc warFile = new File JavaDoc(di.localUrl.getFile());
110       if (warFile.isDirectory() == false)
111          throw new DeploymentException("Expected a war directory: " + di.localUrl);
112
113       File JavaDoc webXML = new File JavaDoc(di.localUrl.getFile() + "/WEB-INF/web.xml");
114       if (webXML.isFile() == false)
115          throw new DeploymentException("Cannot find web.xml: " + webXML);
116
117       FileOutputStream JavaDoc fos = null;
118       try
119       {
120          SAXReader saxReader = new SAXReader();
121          saxReader.setEntityResolver(new JBossEntityResolver());
122
123          Document doc = saxReader.read(webXML);
124
125          if (modifyWebXMLDocument(doc, di, webservices))
126          {
127             // After redeployment there might be a stale copy of the original web.xml.org, we delete it
128
File JavaDoc orgWebXML = new File JavaDoc(webXML.getCanonicalPath() + ".org");
129             orgWebXML.delete();
130
131             // Rename the web.xml
132
if (webXML.renameTo(orgWebXML) == false)
133                throw new DeploymentException("Cannot rename web.xml: " + orgWebXML);
134
135             OutputFormat format = OutputFormat.createPrettyPrint();
136             fos = new FileOutputStream JavaDoc(webXML);
137             XMLWriter writer = new XMLWriter(fos, format);
138             writer.write(doc);
139          }
140       }
141       catch (DeploymentException e)
142       {
143          throw e;
144       }
145       catch (Exception JavaDoc e)
146       {
147          throw new DeploymentException(e);
148       }
149       finally
150       {
151          if (fos != null)
152          {
153             try
154             {
155                fos.close();
156             }
157             catch (IOException JavaDoc e)
158             {
159                log.warn("Unexpected IOException on file close", e);
160             }
161          }
162       }
163    }
164
165    /** Modify the web.xml document.
166     *
167     * The web.xml containes the service implementation class in the <servlet-class> element, this should be
168     * replaced by a propper servlet.
169     */

170    private boolean modifyWebXMLDocument(Document doc, DeploymentInfo di, WebservicesMetaData webservices) throws DeploymentException
171    {
172       boolean modified = false;
173
174       WebserviceDescriptionMetaData[] wsDescriptions = webservices.getWebserviceDescriptions();
175       for (int i = 0; i < wsDescriptions.length; i++)
176       {
177          WebserviceDescriptionMetaData wsDescription = wsDescriptions[i];
178          PortComponentMetaData[] portComponents = wsDescription.getPortComponents();
179          for (int j = 0; j < portComponents.length; j++)
180          {
181             PortComponentMetaData pcMetaData = portComponents[j];
182             PortComponentInfo pcInfo = new PortComponentInfo(di, pcMetaData);
183
184             // get the servlet link
185
String JavaDoc servletLink = pcMetaData.getServletLink();
186             if (servletLink == null)
187                throw new DeploymentException("Cannot find servlet-link in port-component: " + pcMetaData.getPortComponentName());
188
189             // modify the servlet-class
190
if (modifyServletConfig(doc, servletLink, pcInfo))
191                modified = true;
192          }
193       }
194
195       return modified;
196    }
197
198    /** Override to return the name of the service endpoint servlet */
199    protected String JavaDoc getServiceEndpointServletName()
200    {
201       return ServiceEndpointServletJSE.class.getName();
202    }
203 }
204
Popular Tags