KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas_ws > wsgen > generator > ews > wsdltoj2ee > writer > J2EEServerDeployWriter


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 /**
17  * JOnAS : Java(TM) OpenSource Application Server
18  * Copyright (C) 2004 Bull S.A.
19  * Contact: jonas-team@objectweb.org
20  *
21  * This library is free software; you can redistribute it and/or
22  * modify it under the terms of the GNU Lesser General Public
23  * License as published by the Free Software Foundation; either
24  * version 2.1 of the License, or any later version.
25  *
26  * This library is distributed in the hope that it will be useful,
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
29  * Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public
32  * License along with this library; if not, write to the Free Software
33  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34  * USA
35  *
36  * --------------------------------------------------------------------------
37  * $Id: J2EEServerDeployWriter.java,v 1.2 2005/05/27 15:01:22 sauthieg Exp $
38  * --------------------------------------------------------------------------
39  */

40 package org.objectweb.jonas_ws.wsgen.generator.ews.wsdltoj2ee.writer;
41
42 import java.io.IOException JavaDoc;
43 import java.io.PrintWriter JavaDoc;
44 import java.util.ArrayList JavaDoc;
45 import java.util.HashSet JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.Map JavaDoc;
49
50 import javax.wsdl.Binding;
51 import javax.wsdl.BindingOperation;
52 import javax.wsdl.Definition;
53 import javax.wsdl.Operation;
54 import javax.wsdl.OperationType;
55 import javax.wsdl.Port;
56 import javax.wsdl.Service;
57 import javax.wsdl.extensions.UnknownExtensibilityElement;
58 import javax.wsdl.extensions.soap.SOAPBinding;
59 import javax.xml.namespace.QName JavaDoc;
60
61 import org.apache.axis.Constants;
62 import org.apache.axis.constants.Scope;
63 import org.apache.axis.constants.Style;
64 import org.apache.axis.constants.Use;
65 import org.apache.axis.deployment.wsdd.WSDDConstants;
66 import org.apache.axis.utils.JavaUtils;
67 import org.apache.axis.utils.Messages;
68 import org.apache.axis.wsdl.symbolTable.BindingEntry;
69 import org.apache.axis.wsdl.symbolTable.Parameters;
70 import org.apache.axis.wsdl.symbolTable.SymbolTable;
71 import org.apache.axis.wsdl.toJava.Emitter;
72 import org.apache.axis.wsdl.toJava.Utils;
73
74 import org.objectweb.jonas_lib.deployment.api.HandlerDesc;
75
76 import org.objectweb.jonas_ws.deployment.api.PortComponentDesc;
77 import org.objectweb.jonas_ws.deployment.api.SSBPortComponentDesc;
78 import org.objectweb.jonas_ws.deployment.api.ServiceDesc;
79
80 /**
81  * This is Wsdl2java's deploy Writer. It writes the server-deploy-XX.wsdd file.
82  * Based on J2eeDeployWriter from Ias
83  * (http://cvs.apache.org/viewcvs.cgi/ws-axis/contrib/ews/src/org/apache/geronimo/ews/ws4j2ee/toWs/ws/J2eeDeployWriter.java?rev=1.13&view=markup)
84  */

85 public class J2EEServerDeployWriter extends JOnASDeployWriter {
86
87     /**
88      * WSDD Extension prefix
89      */

90     private static final String JavaDoc WSDD_PREFIX = "deploy-server-";
91
92     /**
93      * Axis parameter name for SE interface name
94      */

95     private static final String JavaDoc SERVICE_ENDPOINT_INTERFACE_NAME = "serviceEndpointInterfaceName";
96
97     /**
98      * Axis parameter name for SE jndi name
99      */

100     private static final String JavaDoc SERVICE_ENDPOINT_JNDI_NAME = "serviceEndpointJndiName";
101
102     /**
103      * Constructor.
104      * @param emitter J2EEEmitter
105      * @param definition Definition
106      * @param symbolTable SymbolTable
107      */

108     public J2EEServerDeployWriter(Emitter emitter, Definition definition, SymbolTable symbolTable) {
109         super(emitter, definition, symbolTable);
110     } // ctor
111

112     /**
113      * Write out deployment and undeployment instructions for each WSDL service
114      * @param pw PrintWriter
115      * @throws IOException when services cannot be created
116      */

117     protected void writeDeployServices(PrintWriter JavaDoc pw) throws IOException JavaDoc {
118
119         // deploy the ports on each service
120
Map JavaDoc serviceMap = getDefinition().getServices();
121         ServiceDesc desc = getJonasWSContext().getServiceDesc();
122
123         for (Iterator JavaDoc mapIterator = serviceMap.values().iterator(); mapIterator.hasNext();) {
124             Service myService = (Service) mapIterator.next();
125
126             pw.println();
127             pw.println(" <!-- " + Messages.getMessage("wsdlService00", myService.getQName().getLocalPart()) + " -->");
128             pw.println();
129
130             for (Iterator JavaDoc portIterator = myService.getPorts().values().iterator(); portIterator.hasNext();) {
131                 Port myPort = (Port) portIterator.next();
132                 BindingEntry bEntry = getSymbolTable().getBindingEntry(myPort.getBinding().getQName());
133
134                 // If this isn't an SOAP binding, skip it
135
if (bEntry.getBindingType() != BindingEntry.TYPE_SOAP) {
136                     continue;
137                 }
138
139                 PortComponentDesc portDesc = findPortComponentDesc(desc, myPort);
140
141                 if (portDesc != null) {
142                     // write ports described by the current ServiceDesc
143
writeDeployPort(pw, myService, bEntry, portDesc);
144                 }
145             }
146         }
147     } // writeDeployServices
148

149     /**
150      * @param desc JOnAS Service Description
151      * @param myPort wsdl:port
152      * @return Returns the JOnAS PortComponentDesc associated with the given wsdl:port
153      */

154     private static PortComponentDesc findPortComponentDesc(ServiceDesc desc, Port myPort) {
155
156         PortComponentDesc port = null;
157         List JavaDoc ports = desc.getPortComponents();
158         for (Iterator JavaDoc i = ports.iterator(); i.hasNext() && port == null;) {
159             PortComponentDesc pcd = (PortComponentDesc) i.next();
160             if (pcd.getQName().getLocalPart().equals(myPort.getName())) {
161                 port = pcd;
162             }
163         }
164         return port;
165     }
166
167     /**
168      * Write out deployment and undeployment instructions for given WSDL port
169      * @param pw PrintWriter
170      * @param service wsdl:service
171      * @param bEntry BindingEntry
172      * @param portComponentDesc JOnAS Port representation
173      * @throws IOException IOException
174      */

175     protected void writeDeployPort(PrintWriter JavaDoc pw, Service service, BindingEntry bEntry,
176             PortComponentDesc portComponentDesc) throws IOException JavaDoc {
177
178         String JavaDoc serviceName = portComponentDesc.getServiceName();
179         boolean hasLiteral = bEntry.hasLiteral();
180         boolean hasMIME = Utils.hasMIME(bEntry);
181         String JavaDoc prefix = WSDDConstants.NS_PREFIX_WSDD_JAVA;
182         String JavaDoc styleStr = "";
183         Iterator JavaDoc iterator = bEntry.getBinding().getExtensibilityElements().iterator();
184
185         // iterate throught extensibilityElements of given Port's Binding
186
while (iterator.hasNext()) {
187             Object JavaDoc obj = iterator.next();
188
189             if (obj instanceof SOAPBinding) {
190                 use = Use.ENCODED;
191             } else if (obj instanceof UnknownExtensibilityElement) {
192
193                 // TODO After WSDL4J supports soap12, change this code
194
UnknownExtensibilityElement unkElement = (UnknownExtensibilityElement) obj;
195                 QName JavaDoc name = unkElement.getElementType();
196
197                 if (name.getNamespaceURI().equals(Constants.URI_WSDL12_SOAP) && name.getLocalPart().equals("binding")) {
198                     use = Use.ENCODED;
199                 }
200             }
201         }
202
203         if (getSymbolTable().isWrapped()) {
204             styleStr = " style=\"" + Style.WRAPPED + "\"";
205             use = Use.LITERAL;
206         } else {
207             styleStr = " style=\"" + bEntry.getBindingStyle().getName() + "\"";
208
209             if (hasLiteral) {
210                 use = Use.LITERAL;
211             }
212         }
213
214         String JavaDoc useStr = " use=\"" + use + "\"";
215
216         if (portComponentDesc.hasBeanImpl()) {
217             pw.println(" <service name=\"" + serviceName + "\" provider=\"" + prefix + ":JOnASEJB\"" + styleStr + useStr + ">");
218
219             SSBPortComponentDesc ssbPCD = (SSBPortComponentDesc) portComponentDesc;
220
221             pw.println(" <parameter name=\"" + SERVICE_ENDPOINT_INTERFACE_NAME + "\" value=\""
222                     + ssbPCD.getServiceEndpointInterface().getName() + "\"/>");
223             pw.println(" <parameter name=\"" + SERVICE_ENDPOINT_JNDI_NAME + "\" value=\""
224                     + ssbPCD.getSessionStatelessDesc().getJndiServiceEndpointName() + "\"/>");
225
226         } else {
227             pw.println(" <service name=\"" + serviceName + "\" provider=\"" + prefix + ":RPC\"" + styleStr + useStr + ">");
228             pw.println(" <parameter name=\"className\" value=\"" + portComponentDesc.getSIBClassname() + "\"/>");
229
230         }
231
232         pw.println(" <parameter name=\"wsdlTargetNamespace\" value=\"" + service.getQName().getNamespaceURI()
233                 + "\"/>");
234         pw.println(" <parameter name=\"wsdlServiceElement\" value=\"" + service.getQName().getLocalPart()
235                         + "\"/>");
236         pw.println(" <parameter name=\"wsdlServicePort\" value=\"" + serviceName + "\"/>");
237
238         // MIME attachments don't work with multiref, so turn it off.
239
if (hasMIME) {
240             pw.println(" <parameter name=\"sendMultiRefs\" value=\"false\"/>");
241         }
242
243         writeDeployBinding(pw, bEntry);
244         writeDeployTypes(pw, bEntry.getBinding(), hasLiteral, hasMIME, use);
245
246         List JavaDoc handlers = portComponentDesc.getHandlers();
247         if (!handlers.isEmpty()) {
248             pw.println(" <handlerInfoChain>");
249             for (Iterator JavaDoc i = handlers.iterator(); i.hasNext();) {
250                 writeHandler(pw, (HandlerDesc) i.next());
251             }
252             pw.println(" </handlerInfoChain>");
253         }
254         pw.println(" </service>");
255     }
256
257     /**
258      * Write out deployment instructions for given WSDL binding
259      * @param pw PrintWriter
260      * @param bEntry BindingEntry
261      * @throws IOException IOException
262      */

263     protected void writeDeployBinding(PrintWriter JavaDoc pw, BindingEntry bEntry) throws IOException JavaDoc {
264
265         Binding binding = bEntry.getBinding();
266
267         pw.println(" <parameter name=\"wsdlPortType\" value=\"" + binding.getPortType().getQName().getLocalPart()
268                 + "\"/>");
269         // force JAXRPC 1.1 Type Mapping
270
pw.println(" <parameter name=\"typeMappingVersion\" value=\"" + emitter.getTypeMappingVersion() + "\"/>");
271
272         HashSet JavaDoc allowedMethods = new HashSet JavaDoc();
273
274         if (!emitter.isSkeletonWanted()) {
275             Iterator JavaDoc operationsIterator = binding.getBindingOperations().iterator();
276
277             for (; operationsIterator.hasNext();) {
278                 BindingOperation bindingOper = (BindingOperation) operationsIterator.next();
279                 Operation operation = bindingOper.getOperation();
280                 OperationType type = operation.getStyle();
281                 String JavaDoc javaOperName = JavaUtils.xmlNameToJava(operation.getName());
282
283                 // These operation types are not supported. The signature
284
// will be a string stating that fact.
285
if ((type == OperationType.NOTIFICATION) || (type == OperationType.SOLICIT_RESPONSE)) {
286                     continue;
287                 }
288
289                 allowedMethods.add(javaOperName);
290
291                 // We pass "" as the namespace argument because we're just
292
// interested in the return type for now.
293
Parameters params = getSymbolTable().getOperationParameters(operation, "", bEntry);
294
295                 if (params != null) {
296
297                     // Get the operation QName
298
QName JavaDoc elementQName = Utils.getOperationQName(bindingOper, bEntry, getSymbolTable());
299
300                     // Get the operation's return QName and type
301
QName JavaDoc returnQName = null;
302                     QName JavaDoc returnType = null;
303
304                     if (params.returnParam != null) {
305                         returnQName = params.returnParam.getQName();
306                         returnType = Utils.getXSIType(params.returnParam);
307                     }
308
309                     // Get the operations faults
310
Map JavaDoc faultMap = bEntry.getFaults();
311                     ArrayList JavaDoc faults = null;
312
313                     if (faultMap != null) {
314                         faults = (ArrayList JavaDoc) faultMap.get(bindingOper);
315                     }
316
317                     String JavaDoc soapAction = Utils.getOperationSOAPAction(bindingOper);
318
319                     // Write the operation metadata
320
writeOperation(pw, javaOperName, elementQName, returnQName, returnType, params, faults, soapAction);
321                 }
322             }
323         }
324
325         pw.print(" <parameter name=\"allowedMethods\" value=\"");
326
327         if (allowedMethods.isEmpty()) {
328             pw.println("*\"/>");
329         } else {
330             boolean first = true;
331
332             for (Iterator JavaDoc i = allowedMethods.iterator(); i.hasNext();) {
333                 String JavaDoc method = (String JavaDoc) i.next();
334
335                 if (first) {
336                     pw.print(method);
337
338                     first = false;
339                 } else {
340                     pw.print(" " + method);
341                 }
342             }
343
344             pw.println("\"/>");
345         }
346
347         Scope scope = emitter.getScope();
348
349         if (scope != null) {
350             pw.println(" <parameter name=\"scope\" value=\"" + scope.getName() + "\"/>");
351         }
352     }
353
354     /**
355      * @param pw PrintWriter
356      * @param handler the server Handler to write
357      */

358     protected void writeHandler(PrintWriter JavaDoc pw, HandlerDesc handler) {
359         pw.println(" <handlerInfo classname=\"" + handler.getHandlerClassName() + "\">");
360         Map JavaDoc params = handler.getInitParams();
361         for (Iterator JavaDoc i = params.keySet().iterator(); i.hasNext();) {
362             String JavaDoc pName = (String JavaDoc) i.next();
363             pw.println(" <parameter name=\"" + pName + "\" value=\""
364                     + params.get(pName) + "\"/>");
365         }
366         List JavaDoc headers = handler.getSOAPHeaders();
367         for (Iterator JavaDoc i = headers.iterator(); i.hasNext();) {
368             QName JavaDoc sh = (QName JavaDoc) i.next();
369             pw.println(" <header xmlns:ns=\"" + sh.getNamespaceURI() + "\" qname=\"ns:"
370                     + sh.getLocalPart() + "\"/>");
371         }
372         pw.println(" </handlerInfo>");
373         List JavaDoc roles = handler.getSOAPRoles();
374         for (Iterator JavaDoc i = roles.iterator(); i.hasNext();) {
375             String JavaDoc role = (String JavaDoc) i.next();
376             pw.println(" <role soapActorName=\"" + role + "\"/>");
377         }
378     }
379
380     /**
381      * @return Returns "deploy-server-"
382      */

383     protected String JavaDoc getPrefix() {
384         return WSDD_PREFIX;
385     }
386
387 }
388
Popular Tags