1 7 8 package org.jboss.webservice; 10 11 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 ; 32 import java.io.File ; 33 import java.io.FileWriter ; 34 import java.io.IOException ; 35 import java.net.URL ; 36 import java.util.ArrayList ; 37 38 48 public class ServiceDeployerEJB extends ServiceDeployer 49 implements ServiceDeployerEJBMBean 50 { 51 private final Logger log = Logger.getLogger(ServiceDeployer.class); 53 54 public static final String EJB_ENDPOINT_WEBAPP_URL = "ejb-endpoint-webapp-url"; 56 57 private ObjectName ejbDeployer; 59 private ObjectName mainDeployer; 61 62 67 public void setEJBDeployer(ObjectName deployerName) 68 { 69 this.ejbDeployer = deployerName; 70 } 71 72 77 public void setMainDeployer(ObjectName deployerName) 78 { 79 this.mainDeployer = deployerName; 80 } 81 82 85 protected void startService() throws Exception 86 { 87 super.startService(); 88 registerNotificationListener(ejbDeployer); 89 } 90 91 94 protected void stopService() throws Exception 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 tmpWar = null; 110 try 111 { 112 File tmpdir = new File (config.getServerTempDir().getCanonicalPath() + "/deploy"); 113 114 String deploymentName = di.getCanonicalName().replace('/', '-') + "-ws"; 115 tmpWar = File.createTempFile(deploymentName, ".war", tmpdir); 116 tmpWar.delete(); 117 File webInf = new File (tmpWar, "WEB-INF"); 118 webInf.mkdirs(); 119 120 File webXml = new File (webInf, "web.xml"); 121 FileWriter fw = new FileWriter (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 jbossWebXml = new File (webInf, "jboss-web.xml"); 129 fw = new FileWriter (jbossWebXml); 130 writer = new XMLWriter(fw, fmt); 131 writer.write(jbossDoc); 132 writer.close(); 133 fw.close(); 134 } 135 catch (IOException e) 136 { 137 throw new DeploymentException("Failed to create webservice.war", e); 138 } 139 140 try 142 { 143 URL warURL = tmpWar.toURL(); 144 Object [] args = {warURL}; 145 String [] sig = {"java.net.URL"}; 146 server.invoke(mainDeployer, "deploy", args, sig); 147 di.context.put(EJB_ENDPOINT_WEBAPP_URL, warURL); 148 } 149 catch (Exception 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 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 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 196 ArrayList urlPatters = new ArrayList (); 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 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 urlPattern = (pcMetaData != null ? pcMetaData.getURLPattern() : "/*"); 215 if (urlPatters.contains(urlPattern)) 216 throw new IllegalArgumentException ("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 authMethod = null; 225 226 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 portName = port.getPortComponentName(); 235 String ejbLink = port.getEjbLink(); 236 BeanMetaData smd = (BeanMetaData)applMetaData.getBeanByEjbName(ejbLink); 237 EjbPortComponentMetaData portMetaData = smd.getPortComponent(); 238 if (portMetaData != null) 239 { 240 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 uri = portMetaData.getURLPattern(); 260 pattern.addText(uri); 261 if (portMetaData.getAuthMethod() != null) 263 { 264 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 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 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 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 319 Document jbossDoc = factory.createDocument(); 320 Element jbossWeb = jbossDoc.addElement("jboss-web"); 321 322 String securityDomain = amd.getSecurityDomain(); 323 if (securityDomain != null) 324 { 325 Element secDomain = jbossWeb.addElement("security-domain"); 326 secDomain.addText(securityDomain); 327 } 328 String 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 try 338 { 339 URL warURL = (URL )di.context.get(EJB_ENDPOINT_WEBAPP_URL); 340 if (warURL != null) 341 { 342 Object [] args = {warURL}; 343 String [] sig = {"java.net.URL"}; 344 server.invoke(mainDeployer, "undeploy", args, sig); 345 } 346 347 } 348 catch (Exception e) 349 { 350 log.error("Failed to undeploy webservice.war", e); 351 } 352 353 super.undeployWebservices(di, webservices); 354 } 355 356 359 protected URL 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 370 protected String getServiceEndpointServletName() 371 { 372 return ServiceEndpointServletEJB.class.getName(); 373 } 374 } 375 | Popular Tags |