KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > backend > WebServiceDeployer


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.deployment.backend;
25
26 import java.io.File JavaDoc;
27 import java.io.OutputStream JavaDoc;
28 import java.io.BufferedOutputStream JavaDoc;
29
30 import java.util.Iterator JavaDoc;
31 import java.util.Collection JavaDoc;
32 import java.util.HashSet JavaDoc;
33 import java.util.Set JavaDoc;
34
35 import java.net.URL JavaDoc;
36 import javax.servlet.SingleThreadModel JavaDoc;
37 import javax.xml.ws.http.HTTPBinding;
38
39 import com.sun.enterprise.deployment.Application;
40 import com.sun.enterprise.deployment.WebBundleDescriptor;
41 import com.sun.enterprise.deployment.WebService;
42 import com.sun.enterprise.deployment.WebServiceEndpoint;
43 import com.sun.enterprise.deployment.WebComponentDescriptor;
44 import com.sun.enterprise.deployment.deploy.shared.FileArchive;
45 import com.sun.enterprise.deployment.deploy.shared.AbstractArchive;
46 import com.sun.enterprise.deployment.io.WebDeploymentDescriptorFile;
47 import com.sun.enterprise.deployment.util.WebServerInfo;
48 import com.sun.enterprise.deployment.util.VirtualServerInfo;
49
50 import com.sun.enterprise.webservice.WsUtil;
51
52 import com.sun.enterprise.diagnostics.util.FileUtils;
53
54 import com.sun.enterprise.util.i18n.StringManager;
55 /**
56  * This class is responsible for handling all generation/process
57  * that has to happen during web services deployment
58  *
59  * @author Jerome Dochez
60  */

61 public class WebServiceDeployer {
62     
63     private static StringManager localStrings =
64         StringManager.getManager( WebServiceDeployer.class );
65     
66     private DeploymentRequest request;
67         
68     /** Creates a new instance of WebServiceDeployer */
69     public WebServiceDeployer(DeploymentRequest request) {
70         this.request = request;
71     }
72     
73     /**
74      * We need to replace the servlet-class for each servlet endpoint
75      * with a pre-written servlet that has access to the container and
76      * the JAXRPC runtime. The original servlet-class was already saved
77      * in the runtime information by j2eec.
78      *
79      * Also, generate the final WSDL for each web service by filling in
80      * the endpoint address field, and publish to client if necessary.
81      *
82      */

83     public void doWebServiceDeployment(Application app, File JavaDoc appDir) throws Exception JavaDoc {
84
85         ClassLoader JavaDoc loader = app.getClassLoader();
86         Collection JavaDoc webBundles = new HashSet JavaDoc();
87         Collection JavaDoc webServices = new HashSet JavaDoc();
88
89         // First collect all web applications and web service descriptors.
90
webBundles.addAll( app.getWebBundleDescriptors() );
91         webServices.addAll( app.getWebServiceDescriptors() );
92         
93         // swap the deployment descriptors context-root with the one
94
// provided in the deployment request.
95
if (request.getContextRoot()!=null) {
96             if (app.isVirtual()) {
97                 ((WebBundleDescriptor) webBundles.iterator().next()).setContextRoot(request.getContextRoot());
98             }
99         }
100         
101         // Swap the application written servlet implementation class for
102
// one provided by the container. The original class is stored
103
// as runtime information since it will be used as the servant at
104
// dispatch time.
105
for(Iterator JavaDoc<WebBundleDescriptor> iter = webBundles.iterator(); iter.hasNext(); ) {
106             doWebServiceDeployment(iter.next(), appDir);
107         }
108             
109         // Generate final wsdls for all web services and store them in
110
// the application repository directory.
111
for(Iterator JavaDoc<WebService> iter = webServices.iterator(); iter.hasNext(); ) {
112             WsUtil wsUtil = new WsUtil();
113             WebService next = iter.next();
114
115             // Endpoint with HTTP bindings need not have WSDLs; In which case
116
// there is no need for WSDL publishing
117
if( (next.getWsdlFileUrl() == null) &&
118                  (next.getMappingFileUri() == null) ) {
119                 for(WebServiceEndpoint wsep : next.getEndpoints()) {
120                     if(!(HTTPBinding.HTTP_BINDING.equals(wsep.getProtocolBinding()))) {
121                         throw new Exception JavaDoc(
122                             localStrings.getStringWithDefault(
123                             "enterprise.webservice.noWsdlError",
124                             "Service {0} has an endpoint with non-HTTP binding but there is no WSDL; Deployment cannot proceed",
125                             new Object JavaDoc[] {next.getName()}));
126                     }
127                     wsep.composeFinalWsdlUrl(wsUtil.getWebServerInfo(request).getWebServerRootURL(wsep.isSecure()));
128                 }
129                 continue;
130             }
131
132             URL JavaDoc clientPublishLocation = next.getClientPublishUrl();
133
134             // Even if deployer specified a wsdl file
135
// publish location, server can't assume it can access that
136
// file system. Plus, it's cleaner to depend on a file stored
137
// within the application repository rather than one directly
138
// exposed to the deployer. Name of final wsdl is derived based
139
// on the location of its associated module. This prevents us
140
// from having write the module to disk in order to store the
141
// modified runtime info.
142
URL JavaDoc url = next.getWsdlFileUrl();
143             
144             // Create the generated WSDL in the generated directory; for that create the directories first
145
File JavaDoc genXmlDir = request.getGeneratedXMLDirectory();
146             if(request.isApplication()) {
147                 // Add module name to the generated xml dir for apps
148
String JavaDoc subDirName = next.getBundleDescriptor().getModuleDescriptor().getArchiveUri();
149                 genXmlDir = new File JavaDoc(genXmlDir, subDirName.replaceAll("\\.", "_"));
150             }
151             File JavaDoc genWsdlFile = null;
152             
153             if (!next.hasWsdlFile()) {
154                 // no wsdl file was specified at deployment or it was an http location
155
// we must have downloaded it or created one when
156
// deploying into the generated directory directly. pick it up from there,
157
// but generate it into a temp
158
genWsdlFile = new File JavaDoc(url.toURI());
159                 genWsdlFile = File.createTempFile("gen_","", genWsdlFile.getParentFile());
160             } else {
161                 String JavaDoc wsdlFileDir = next.getWsdlFileUri().substring(0, next.getWsdlFileUri().lastIndexOf('/'));
162                 (new File JavaDoc(genXmlDir, wsdlFileDir)).mkdirs();
163                 genWsdlFile = new File JavaDoc(genXmlDir, next.getWsdlFileUri());
164             }
165             wsUtil.generateFinalWsdl(url, next, wsUtil.getWebServerInfo(request), genWsdlFile);
166             
167             if (!next.hasWsdlFile()) {
168                 // Two renaming operations followed by a delete
169
// are required because, on windows, a File.delete and
170
// a File.renameTo are not foolproof
171
File JavaDoc finalName = new File JavaDoc(url.toURI());
172                 File JavaDoc tmpName = new File JavaDoc(genWsdlFile.getAbsolutePath() + ".TMP");
173                 // Rename wsgen generated / downloaded WSDL to .TMP
174
boolean renameDone = finalName.renameTo(tmpName);
175                 if(!renameDone) {
176                     // On windows rename operation fails occassionaly;
177
// so use the iostream way to do the rename
178
FileUtils.moveFile(finalName.getAbsolutePath(), tmpName.getAbsolutePath());
179                 }
180                 // Rename soap:address fixed WSDL to wsgen generated WSDL
181
renameDone = genWsdlFile.renameTo(finalName);
182                 if(!renameDone) {
183                     // On windows rename operation fails occassionaly;
184
// so use the iostream way to do the rename
185
FileUtils.moveFile(genWsdlFile.getAbsolutePath(), finalName.getAbsolutePath());
186                 }
187                 // Remove the original WSDL file
188
tmpName.delete();
189             }
190         }
191     }
192     
193     /**
194      * We need to replace the servlet-class for each servlet endpoint
195      * with a pre-written servlet that has access to the container and
196      * the JAXRPC runtime. The original servlet-class was already saved
197      * in the runtime information by j2eec.
198      *
199      * Also, generate the final WSDL for each web service by filling in
200      * the endpoint address field, and publish to client if necessary.
201      *
202      */

203     public void doWebServiceDeployment(WebBundleDescriptor web, File JavaDoc outDir) throws Exception JavaDoc {
204         
205         Collection JavaDoc endpoints = web.getWebServices().getEndpoints();
206         ClassLoader JavaDoc loader = web.getClassLoader();
207         
208         boolean writeModule = !endpoints.isEmpty();
209         
210         for(Iterator JavaDoc endpointIter = endpoints.iterator();
211             endpointIter.hasNext();) {
212             
213             WebServiceEndpoint nextEndpoint = (WebServiceEndpoint)
214             endpointIter.next();
215             WebComponentDescriptor webComp = nextEndpoint.getWebComponentImpl();
216             String JavaDoc servletImplClass = nextEndpoint.getServletImplClass();
217             if( !nextEndpoint.hasServletImplClass() ) {
218                 throw new Exception JavaDoc(
219                 localStrings.getStringWithDefault(
220                 "enterprise.deployment.backend.cannot_find_servlet",
221                 "Runtime settings error. Cannot find servlet-impl-class for endpoint {0} ",
222                 new Object JavaDoc[] {nextEndpoint.getEndpointName()}));
223             }
224             
225             if( !nextEndpoint.getWebService().hasFilePublishing() ) {
226                 // @@@ add security attributes as well????
227
String JavaDoc publishingUri = nextEndpoint.getPublishingUri();
228                 String JavaDoc publishingUrlPattern =
229                 (publishingUri.charAt(0) == '/') ?
230                 publishingUri : "/" + publishingUri + "/*";
231                 webComp.addUrlPattern(publishingUrlPattern);
232             }
233             
234             Class JavaDoc servletImplClazz = loader.loadClass(servletImplClass);
235             String JavaDoc containerServlet;
236             // XXX - TODO - Do we need to handle Single Thread Servlet for 2.0 ?
237
// For versions above 1.1, use JAXWSServlet
238
if("1.1".compareTo(web.getWebServices().getSpecVersion())<0) {
239                 containerServlet = "com.sun.enterprise.webservice.JAXWSServlet";
240             } else {
241                 containerServlet =
242                 SingleThreadModel JavaDoc.class.isAssignableFrom(servletImplClazz) ?
243                 "com.sun.enterprise.webservice.SingleThreadJAXRPCServlet" :
244                     "com.sun.enterprise.webservice.JAXRPCServlet";
245             }
246             webComp.setWebComponentImplementation(containerServlet);
247         }
248     }
249 }
250
Popular Tags