KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > webservice > WsCompile


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 package com.sun.enterprise.webservice;
24
25 import java.io.File JavaDoc;
26 import java.io.FileNotFoundException JavaDoc;
27 import java.io.FileOutputStream JavaDoc;
28 import java.io.OutputStream JavaDoc;
29 import java.io.PrintWriter JavaDoc;
30 import java.util.Map JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.HashSet JavaDoc;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import java.util.List JavaDoc;
37 import java.util.Properties JavaDoc;
38
39 import javax.xml.namespace.QName JavaDoc;
40
41 //JAX-RPC SPI
42
import com.sun.xml.rpc.spi.JaxRpcObjectFactory;
43 import com.sun.xml.rpc.spi.tools.CompileTool;
44 import com.sun.xml.rpc.spi.tools.CompileToolDelegate;
45 import com.sun.xml.rpc.spi.tools.HandlerChainInfo;
46 import com.sun.xml.rpc.spi.tools.HandlerInfo;
47 import com.sun.xml.rpc.spi.tools.Configuration;
48 import com.sun.xml.rpc.spi.tools.ModelInfo;
49 import com.sun.xml.rpc.spi.model.Model;
50 import com.sun.xml.rpc.spi.model.Service;
51 import com.sun.xml.rpc.spi.model.Port;
52 import com.sun.xml.rpc.spi.model.ModelProperties;
53
54 import com.sun.enterprise.deployment.WebBundleDescriptor;
55 import com.sun.enterprise.deployment.EjbBundleDescriptor;
56 import com.sun.enterprise.deployment.WebService;
57 import com.sun.enterprise.deployment.WebServicesDescriptor;
58 import com.sun.enterprise.deployment.WebServiceEndpoint;
59 import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
60 import com.sun.enterprise.deployment.WebServiceHandler;
61 import com.sun.enterprise.deployment.NameValuePairDescriptor;
62
63 /**
64  * This implementation of WsCompile should be used only internally
65  * at deployment time (or j2eec). Not meant to be overridden.
66  *
67  * @author Kenneth Saks
68  */

69 public final class WsCompile extends CompileToolDelegate {
70
71     private Collection JavaDoc generatedFiles;
72     private WebService webService;
73     private ServiceReferenceDescriptor serviceRef;
74     private WsUtil wsUtil = new WsUtil();
75     private CompileTool wscompile;
76     private JaxRpcObjectFactory rpcFactory;
77
78     // if set, used to override standard configuration
79
private ModelInfo modelInfo;
80
81     // true if wscompile encountered an error.
82
private boolean error = false;
83
84     public WsCompile(CompileTool compileTool, WebService webServiceDesc) {
85         wscompile = compileTool;
86         webService = webServiceDesc;
87         rpcFactory = JaxRpcObjectFactory.newInstance();
88     }
89
90     public WsCompile(CompileTool compileTool,
91                      ServiceReferenceDescriptor serviceRefDesc) {
92         wscompile = compileTool;
93         serviceRef = serviceRefDesc;
94         rpcFactory = JaxRpcObjectFactory.newInstance();
95     }
96
97     public void setModelInfo(ModelInfo info) {
98         modelInfo = info;
99     }
100
101     public CompileTool getCompileTool() {
102         return wscompile;
103     }
104
105     /**
106      * com.sun.xml.rpc.spi.tools.wscompile.CompileToolDelegate overrides.
107      */

108
109     public Configuration createConfiguration() {
110         Configuration configuration = null;
111         if( modelInfo != null ) {
112             configuration =
113         rpcFactory.createConfiguration(wscompile.getEnvironment());
114             configuration.setModelInfo(modelInfo);
115         }
116         //else, leave it to the jaxrpc implementation to
117
//create Configuration
118

119         return configuration;
120     }
121
122     public void preOnError() {
123         error = true;
124     }
125
126     public void postRegisterProcessorActions() {
127
128         if( !error) {
129
130             if (webService != null) {
131                 setupServiceHandlerChain();
132             }
133
134             // NOTE : Client handler chains are configured at runtime.
135
}
136     }
137
138     public void postRun() {
139         generatedFiles = new HashSet JavaDoc();
140
141         if( !error ) {
142             for(Iterator JavaDoc iter = wscompile.getEnvironment().getGeneratedFiles();
143                 iter.hasNext(); ) {
144                 generatedFiles.add( iter.next() );
145             }
146             if( webService != null ) {
147                 doServicePostProcessing();
148             } else if( serviceRef != null ) {
149                 doClientPostProcessing();
150             }
151         }
152     }
153
154     //
155
// WsCompile methods.
156
//
157

158     public Collection JavaDoc getGeneratedFiles() {
159         return generatedFiles;
160     }
161
162     private void setupServiceHandlerChain() {
163
164         Model model = wscompile.getProcessor().getModel();
165
166         Collection JavaDoc endpoints = webService.getEndpoints();
167         for(Iterator JavaDoc eIter = endpoints.iterator(); eIter.hasNext();) {
168             WebServiceEndpoint nextEndpoint = (WebServiceEndpoint) eIter.next();
169
170             if( !nextEndpoint.hasHandlers() ) {
171                 continue;
172             }
173
174             Port port = wsUtil.getPortFromModel(model,
175                                                 nextEndpoint.getWsdlPort());
176             if( port == null ) {
177                 throw new IllegalStateException JavaDoc("Model port for endpoint " +
178                                                 nextEndpoint.getEndpointName() +
179                                                 " not found");
180             }
181             
182             List JavaDoc handlerChain = nextEndpoint.getHandlers();
183             HandlerChainInfo modelHandlerChain =
184                 port.getServerHCI();
185             List JavaDoc handlerInfoList = new ArrayList JavaDoc();
186
187             // Insert an container handler as the first element.
188
// This is needed to perform method authorization checks.
189
HandlerInfo preHandler = rpcFactory.createHandlerInfo();
190             String JavaDoc handlerClassName = nextEndpoint.implementedByEjbComponent() ?
191                 "com.sun.enterprise.webservice.EjbContainerPreHandler" :
192                 "com.sun.enterprise.webservice.ServletPreHandler";
193             preHandler.setHandlerClassName(handlerClassName);
194             handlerInfoList.add(preHandler);
195
196             // Collect all roles defined on each handler and set them on
197
// handler chain. NOTE : There is a bit of a mismatch here between
198
// 109 and JAXRPC. JAXRPC only defines roles at the handler chain
199
// level, whereas 109 descriptors put roles at the handler level.
200
Collection JavaDoc soapRoles = new HashSet JavaDoc();
201
202             for(Iterator JavaDoc hIter = handlerChain.iterator(); hIter.hasNext();) {
203                 WebServiceHandler nextHandler =
204                     (WebServiceHandler) hIter.next();
205                 HandlerInfo handlerInfo = createHandlerInfo(nextHandler);
206                 handlerInfoList.add(handlerInfo);
207                 soapRoles.addAll(nextHandler.getSoapRoles());
208             }
209
210             // Insert a container handler as the last element in the chain.
211
HandlerInfo postHandler = rpcFactory.createHandlerInfo();
212             handlerClassName = nextEndpoint.implementedByEjbComponent() ?
213                 "com.sun.enterprise.webservice.EjbContainerPostHandler" :
214                 "com.sun.enterprise.webservice.ServletPostHandler";
215             postHandler.setHandlerClassName(handlerClassName);
216             handlerInfoList.add(postHandler);
217
218             // @@@ should probably use addHandler api instead once
219
// == bug is fixed.
220
modelHandlerChain.setHandlersList(handlerInfoList);
221
222             for(Iterator JavaDoc roleIter = soapRoles.iterator(); roleIter.hasNext();) {
223                 modelHandlerChain.addRole((String JavaDoc) roleIter.next());
224             }
225         }
226     }
227
228     private HandlerInfo createHandlerInfo(WebServiceHandler handler) {
229         HandlerInfo handlerInfo = rpcFactory.createHandlerInfo();
230         
231         handlerInfo.setHandlerClassName(handler.getHandlerClass());
232         for(Iterator JavaDoc iter = handler.getSoapHeaders().iterator();
233             iter.hasNext();) {
234             QName JavaDoc next = (QName JavaDoc) iter.next();
235             handlerInfo.addHeaderName(next);
236         }
237
238         Map JavaDoc properties = handlerInfo.getProperties();
239         for(Iterator JavaDoc iter = handler.getInitParams().iterator();
240             iter.hasNext();) {
241             NameValuePairDescriptor next = (NameValuePairDescriptor)
242                 iter.next();
243             properties.put(next.getName(), next.getValue());
244         }
245
246         return handlerInfo;
247     }
248
249     private void doServicePostProcessing() {
250
251         Model model = wscompile.getProcessor().getModel();
252
253         Collection JavaDoc endpoints = webService.getEndpoints();
254
255         for(Iterator JavaDoc iter = endpoints.iterator(); iter.hasNext(); ) {
256             WebServiceEndpoint next = (WebServiceEndpoint) iter.next();
257             Service service = wsUtil.getServiceForPort(model,
258                                                        next.getWsdlPort());
259             if( service == null ) {
260                 service = (Service) model.getServices().next();
261
262                 System.out.println("Warning : Can't find Service for Endpoint '"
263                                    + next.getEndpointName() + "' Port '" +
264                                    next.getWsdlPort() + "'");
265
266                 System.out.println("Defaulting to "+ service.getName());
267             }
268
269             QName JavaDoc serviceName = service.getName();
270
271             next.setServiceNamespaceUri(serviceName.getNamespaceURI());
272             next.setServiceLocalPart(serviceName.getLocalPart());
273
274             Port port = wsUtil.getPortFromModel(model, next.getWsdlPort());
275             if( port == null ) {
276                 String JavaDoc msg = "Can't find model port for endpoint "
277                     + next.getEndpointName() + " with wsdl-port " +
278                     next.getWsdlPort();
279                 throw new IllegalStateException JavaDoc(msg);
280             }
281
282             // If port has a tie class name property, use it. Otherwise,
283
// use naming convention to derive tie class name. If there
284
// are multiple ports per SEI(binding), then the property then
285
// the TIE_CLASS_NAME property will be available. In that case,
286
// a separate tie and stub are generated per port.
287
String JavaDoc tieClassName = (String JavaDoc)
288                 port.getProperty(ModelProperties.PROPERTY_TIE_CLASS_NAME);
289             if( tieClassName == null ) {
290                 tieClassName = next.getServiceEndpointInterface() + "_Tie";
291             }
292             next.setTieClassName(tieClassName);
293
294             if( next.implementedByWebComponent() ) {
295                 wsUtil.updateServletEndpointRuntime(next);
296             } else {
297                 wsUtil.validateEjbEndpoint(next);
298             }
299
300             String JavaDoc endpointAddressUri = next.getEndpointAddressUri();
301             if( endpointAddressUri == null ) {
302                 String JavaDoc msg = "Endpoint address uri must be set for endpoint " +
303                     next.getEndpointName();
304                 throw new IllegalStateException JavaDoc(msg);
305             } else if( endpointAddressUri.indexOf("*") >= 0 ) {
306                 String JavaDoc msg = "Endpoint address uri " + endpointAddressUri +
307                     " for endpoint " + next.getEndpointName() +
308                     " is invalid. It must not contain the '*' character";
309                 throw new IllegalStateException JavaDoc(msg);
310             } else if( endpointAddressUri.endsWith("/") ) {
311                 String JavaDoc msg = "Endpoint address uri " + endpointAddressUri +
312                     " for endpoint " + next.getEndpointName() +
313                     " is invalid. It must not end with '/'";
314                 throw new IllegalStateException JavaDoc(msg);
315             }
316         }
317     }
318
319     private void doClientPostProcessing() {
320         Model model = wscompile.getProcessor().getModel();
321
322         Iterator JavaDoc serviceIter = model.getServices();
323         Service service = null;
324
325         if( serviceRef.hasServiceName() ) {
326             while( serviceIter.hasNext() ) {
327                 Service next = (Service) serviceIter.next();
328                 if( next.getName().equals(serviceRef.getServiceName()) ) {
329                     service = next;
330                     break;
331                 }
332             }
333             if( service == null ) {
334                 throw new IllegalStateException JavaDoc
335                     ("Service " + serviceRef.getServiceName() +
336                      " for service-ref " + serviceRef.getName() + " not found");
337             }
338         } else {
339             if( serviceIter.hasNext() ) {
340                 service = (Service) serviceIter.next();
341                 if( serviceIter.hasNext() ) {
342                     throw new IllegalStateException JavaDoc
343                         ("service ref " + serviceRef.getName() + " must specify"
344                          + " service name since its wsdl declares multiple"
345                          + " services");
346                 }
347                 QName JavaDoc sName = service.getName();
348                 serviceRef.setServiceNamespaceUri(sName.getNamespaceURI());
349                 serviceRef.setServiceLocalPart(sName.getLocalPart());
350             } else {
351                 throw new IllegalStateException JavaDoc
352                     ("service ref " + serviceRef.getName() + " WSDL must " +
353                      "define at least one Service");
354             }
355         }
356
357         // Use naming convention to derive Generated Service
358
// implementation class name.
359
String JavaDoc serviceImpl = service.getJavaIntf().getName() + "_Impl";
360         serviceRef.setServiceImplClassName(serviceImpl);
361
362     }
363
364 }
365
Popular Tags