KickJava   Java API By Example, From Geeks To Geeks.

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


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

13 import org.dom4j.Document;
14 import org.dom4j.DocumentFactory;
15 import org.dom4j.Element;
16 import org.dom4j.io.OutputFormat;
17 import org.dom4j.io.XMLWriter;
18 import org.jboss.deployment.DeploymentException;
19 import org.jboss.deployment.DeploymentInfo;
20 import org.jboss.logging.Logger;
21 import org.jboss.metadata.ApplicationMetaData;
22 import org.jboss.metadata.BeanMetaData;
23 import org.jboss.metadata.EjbPortComponentMetaData;
24 import org.jboss.system.server.ServerConfig;
25 import org.jboss.system.server.ServerConfigLocator;
26 import org.jboss.webservice.metadata.PortComponentMetaData;
27 import org.jboss.webservice.metadata.WebserviceDescriptionMetaData;
28 import org.jboss.webservice.metadata.WebservicesMetaData;
29 import org.jboss.webservice.server.ServiceEndpointServletEJB;
30
31 import javax.management.ObjectName JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.IOException JavaDoc;
35 import java.net.URL JavaDoc;
36 import java.util.ArrayList JavaDoc;
37
38 /**
39  * A deployer service that manages WS4EE compliant Web-Services for EJB Endpoints
40  *
41  * @author Thomas.Diesler@jboss.org
42  * @author Scott.Stark@jboss.org
43  * @jmx.mbean name="jboss.ws4ee:service=ServiceDeployerEJB"
44  * description="Webservice EJB deployer"
45  * extends="org.jboss.webservice.ServiceDeployerMBean"
46  * @since 15-April-2004
47  */

48 public class ServiceDeployerEJB extends ServiceDeployer
49         implements ServiceDeployerEJBMBean
50 {
51    // provide logging
52
private final Logger log = Logger.getLogger(ServiceDeployer.class);
53
54    // The key in the deployment info context for url the generated webapp has been deployed to
55
public static final String JavaDoc EJB_ENDPOINT_WEBAPP_URL = "ejb-endpoint-webapp-url";
56
57    // service name of the EJB deployer
58
private ObjectName JavaDoc ejbDeployer;
59    // service name of the EJB deployer
60
private ObjectName JavaDoc mainDeployer;
61
62    /**
63     * Set the service name of the EJB deployer
64     *
65     * @jmx.managed-attribute
66     */

67    public void setEJBDeployer(ObjectName JavaDoc deployerName)
68    {
69       this.ejbDeployer = deployerName;
70    }
71
72    /**
73     * Set the service name of the Main deployer
74     *
75     * @jmx.managed-attribute
76     */

77    public void setMainDeployer(ObjectName JavaDoc deployerName)
78    {
79       this.mainDeployer = deployerName;
80    }
81
82    /**
83     * Register this service as NotificationListener to the EJBDeployer
84     */

85    protected void startService() throws Exception JavaDoc
86    {
87       super.startService();
88       registerNotificationListener(ejbDeployer);
89    }
90
91    /**
92     * Unregister this service as NotificationListener from the EJBDeployer
93     */

94    protected void stopService() throws Exception JavaDoc
95    {
96       unregisterNotificationListener(ejbDeployer);
97       super.stopService();
98    }
99
100    protected void deployWebservices(DeploymentInfo di, WebservicesMetaData webservices)
101            throws DeploymentException
102    {
103       super.deployWebservices(di, webservices);
104
105       Document webDoc = createWebXML(di, webservices);
106       Document jbossDoc = createJBossWebXML(di);
107
108       ServerConfig config = ServerConfigLocator.locate();
109       File JavaDoc tmpWar = null;
110       try
111       {
112          File JavaDoc tmpdir = new File JavaDoc(config.getServerTempDir().getCanonicalPath() + "/deploy");
113
114          String JavaDoc deploymentName = di.getCanonicalName().replace('/', '-') + "-ws";
115          tmpWar = File.createTempFile(deploymentName, ".war", tmpdir);
116          tmpWar.delete();
117          File JavaDoc webInf = new File JavaDoc(tmpWar, "WEB-INF");
118          webInf.mkdirs();
119
120          File JavaDoc webXml = new File JavaDoc(webInf, "web.xml");
121          FileWriter JavaDoc fw = new FileWriter JavaDoc(webXml);
122          OutputFormat fmt = OutputFormat.createPrettyPrint();
123          XMLWriter writer = new XMLWriter(fw, fmt);
124          writer.write(webDoc);
125          writer.close();
126          fw.close();
127
128          File JavaDoc jbossWebXml = new File JavaDoc(webInf, "jboss-web.xml");
129          fw = new FileWriter JavaDoc(jbossWebXml);
130          writer = new XMLWriter(fw, fmt);
131          writer.write(jbossDoc);
132          writer.close();
133          fw.close();
134       }
135       catch (IOException JavaDoc e)
136       {
137          throw new DeploymentException("Failed to create webservice.war", e);
138       }
139
140       // Deploy the webservice.war
141
try
142       {
143          URL JavaDoc warURL = tmpWar.toURL();
144          Object JavaDoc[] args = {warURL};
145          String JavaDoc[] sig = {"java.net.URL"};
146          server.invoke(mainDeployer, "deploy", args, sig);
147          di.context.put(EJB_ENDPOINT_WEBAPP_URL, warURL);
148       }
149       catch (Exception JavaDoc e)
150       {
151          throw new DeploymentException("Failed to deploy webservice.war", e);
152       }
153    }
154
155    private Document createWebXML(DeploymentInfo di, WebservicesMetaData webservices)
156            throws DeploymentException
157    {
158       ApplicationMetaData applMetaData = (ApplicationMetaData)di.metaData;
159       DocumentFactory factory = DocumentFactory.getInstance();
160
161       Document webDoc = factory.createDocument();
162       Element webApp = webDoc.addElement("web-app");
163
164       /*
165       <servlet>
166         <servlet-name>
167         <servlet-class>
168       </servlet>
169       */

170       WebserviceDescriptionMetaData[] wsDescriptions = webservices.getWebserviceDescriptions();
171       for (int i = 0; i < wsDescriptions.length; i++)
172       {
173          WebserviceDescriptionMetaData wsDescription = wsDescriptions[i];
174          PortComponentMetaData[] portComponents = wsDescription.getPortComponents();
175          for (int j = 0; j < portComponents.length; j++)
176          {
177             PortComponentMetaData pc = portComponents[j];
178             String JavaDoc ejbLink = pc.getEjbLink();
179             if (ejbLink == null)
180                throw new DeploymentException("Cannot find ejb-link in port-component: " + pc.getPortComponentName());
181
182             Element servlet = webApp.addElement("servlet");
183             Element servletName = servlet.addElement("servlet-name");
184             servletName.addText(ejbLink);
185             Element servletClass = servlet.addElement("servlet-class");
186             servletClass.addText(ServiceEndpointServletEJB.class.getName());
187          }
188       }
189
190       /*
191       <servlet-mapping>
192         <servlet-name>
193         <url-pattern>
194       </servlet-mapping>
195       */

196       ArrayList JavaDoc urlPatters = new ArrayList JavaDoc();
197       for (int i = 0; i < wsDescriptions.length; i++)
198       {
199          WebserviceDescriptionMetaData wsDescription = wsDescriptions[i];
200          PortComponentMetaData[] portComponents = wsDescription.getPortComponents();
201          for (int j = 0; j < portComponents.length; j++)
202          {
203             PortComponentMetaData pc = portComponents[j];
204             String JavaDoc ejbLink = pc.getEjbLink();
205
206             Element servletMapping = webApp.addElement("servlet-mapping");
207             Element servletName = servletMapping.addElement("servlet-name");
208             servletName.addText(ejbLink);
209             Element urlPatternElement = servletMapping.addElement("url-pattern");
210
211             BeanMetaData ejbMetaData = (BeanMetaData)applMetaData.getBeanByEjbName(ejbLink);
212             EjbPortComponentMetaData pcMetaData = ejbMetaData.getPortComponent();
213
214             String JavaDoc urlPattern = (pcMetaData != null ? pcMetaData.getURLPattern() : "/*");
215             if (urlPatters.contains(urlPattern))
216                throw new IllegalArgumentException JavaDoc("Cannot use the same url-pattern with different endpoints, " +
217                        "check your <port-component-uri> in jboss.xml");
218
219             urlPatternElement.addText(urlPattern);
220             urlPatters.add(urlPattern);
221          }
222       }
223
224       String JavaDoc authMethod = null;
225
226       // Add web-app/security-constraint for each port component
227
for (int i = 0; i < wsDescriptions.length; i++)
228       {
229          WebserviceDescriptionMetaData wdmd = wsDescriptions[i];
230          PortComponentMetaData[] ports = wdmd.getPortComponents();
231          for (int j = 0; j < ports.length; j++)
232          {
233             PortComponentMetaData port = ports[j];
234             String JavaDoc portName = port.getPortComponentName();
235             String JavaDoc ejbLink = port.getEjbLink();
236             BeanMetaData smd = (BeanMetaData)applMetaData.getBeanByEjbName(ejbLink);
237             EjbPortComponentMetaData portMetaData = smd.getPortComponent();
238             if (portMetaData != null)
239             {
240                /*
241                <security-constraint>
242                  <web-resource-collection>
243                    <web-resource-name>TestUnAuthPort</web-resource-name>
244                    <url-pattern>/HSTestRoot/TestUnAuth/*</url-pattern>
245                  </web-resource-collection>
246                  <auth-constraint>
247                    <role-name>*</role-name>
248                  </auth-constraint>
249                  <user-data-constraint>
250                    <transport-guarantee>NONE</transport-guarantee>
251                  </user-data-constraint>
252                </security-constraint>
253                */

254                Element securityConstraint = webApp.addElement("security-constraint");
255                Element wrc = securityConstraint.addElement("web-resource-collection");
256                Element wrName = wrc.addElement("web-resource-name");
257                wrName.addText(portName);
258                Element pattern = wrc.addElement("url-pattern");
259                String JavaDoc uri = portMetaData.getURLPattern();
260                pattern.addText(uri);
261                // Optional auth-constraint
262
if (portMetaData.getAuthMethod() != null)
263                {
264                   // Only the first auth-method gives the war login-config/auth-method
265
if (authMethod == null)
266                      authMethod = portMetaData.getAuthMethod();
267                   Element authConstraint = securityConstraint.addElement("auth-constraint");
268                   Element roleName = authConstraint.addElement("role-name");
269                   roleName.addText("*");
270                }
271                // Optional user-data-constraint
272
if (portMetaData.getTransportGuarantee() != null)
273                {
274                   Element userData = securityConstraint.addElement("user-data-constraint");
275                   Element transport = userData.addElement("transport-guarantee");
276                   transport.addText(portMetaData.getTransportGuarantee());
277                }
278             }
279          }
280       }
281
282       // Optional login-config/auth-method
283
if (authMethod != null)
284       {
285          Element loginConfig = webApp.addElement("login-config");
286          Element method = loginConfig.addElement("auth-method");
287          method.addText(authMethod);
288          Element realm = loginConfig.addElement("realm-name");
289          realm.addText("EJBServiceEndpointServlet Realm");
290       }
291
292       for (int i = 0; i < wsDescriptions.length; i++)
293       {
294          WebserviceDescriptionMetaData wsDescription = wsDescriptions[i];
295          PortComponentMetaData[] portComponents = wsDescription.getPortComponents();
296          for (int j = 0; j < portComponents.length; j++)
297          {
298             PortComponentMetaData pcMetaData = portComponents[j];
299             String JavaDoc ejbLink = pcMetaData.getEjbLink();
300             PortComponentInfo pcInfo = new PortComponentInfo(di, pcMetaData);
301             modifyServletConfig(webDoc, ejbLink, pcInfo);
302          }
303       }
304
305       return webDoc;
306    }
307
308    private Document createJBossWebXML(DeploymentInfo di)
309    {
310       ApplicationMetaData amd = (ApplicationMetaData)di.metaData;
311       DocumentFactory factory = DocumentFactory.getInstance();
312
313       /* Create a jboss-web
314       <jboss-web>
315          <security-domain>java:/jaas/cts</security-domain>
316          <context-root>/ws4ee/ejbN/</context-root>
317       </jboss-web>
318       */

319       Document jbossDoc = factory.createDocument();
320       Element jbossWeb = jbossDoc.addElement("jboss-web");
321
322       String JavaDoc securityDomain = amd.getSecurityDomain();
323       if (securityDomain != null)
324       {
325          Element secDomain = jbossWeb.addElement("security-domain");
326          secDomain.addText(securityDomain);
327       }
328       String JavaDoc contextRoot = amd.getWebServiceContextRoot();
329       Element root = jbossWeb.addElement("context-root");
330       root.addText(contextRoot);
331       return jbossDoc;
332    }
333
334    protected void undeployWebservices(DeploymentInfo di, WebservicesMetaData webservices)
335    {
336       // Undeploy the webservice.war
337
try
338       {
339          URL JavaDoc warURL = (URL JavaDoc)di.context.get(EJB_ENDPOINT_WEBAPP_URL);
340          if (warURL != null)
341          {
342             Object JavaDoc[] args = {warURL};
343             String JavaDoc[] sig = {"java.net.URL"};
344             server.invoke(mainDeployer, "undeploy", args, sig);
345          }
346
347       }
348       catch (Exception JavaDoc e)
349       {
350          log.error("Failed to undeploy webservice.war", e);
351       }
352
353       super.undeployWebservices(di, webservices);
354    }
355
356    /**
357     * Get the resource name of the webservices.xml descriptor.
358     */

359    protected URL JavaDoc getWebservicesDescriptor(DeploymentInfo di)
360    {
361       if (di.localCl != null)
362          return di.localCl.findResource("META-INF/webservices.xml");
363       else
364          return null;
365    }
366
367    /**
368     * Override to return the name of the service endpoint servlet
369     */

370    protected String JavaDoc getServiceEndpointServletName()
371    {
372       return ServiceEndpointServletEJB.class.getName();
373    }
374 }
375
Popular Tags