File: JavaToWSDL.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,276 kB
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (354 lines) | stat: -rw-r--r-- 10,381 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.tools;

// $Id: JavaToWSDL.java 3959 2007-07-20 14:44:19Z heiko.braun@jboss.com $

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.jws.soap.SOAPBinding.ParameterStyle;
import javax.xml.rpc.encoding.TypeMapping;

import org.jboss.logging.Logger;
import org.jboss.ws.Constants;
import org.jboss.ws.WSException;
import org.jboss.ws.core.soap.Style;
import org.jboss.ws.metadata.jaxrpcmapping.JavaWsdlMapping;
import org.jboss.ws.metadata.umdm.UnifiedMetaData;
import org.jboss.ws.metadata.wsdl.WSDLDefinitions;
import org.jboss.ws.tools.Configuration.OperationConfig;
import org.jboss.ws.tools.metadata.ToolsUnifiedMetaDataBuilder;
import org.jboss.ws.tools.wsdl.WSDLWriter;
import org.jboss.wsf.common.DOMUtils;
import org.jboss.wsf.common.DOMWriter;
import org.w3c.dom.Element;

/**
 * Generates a WSDL for a service endpoint.
 *
 * <BR/> This is the main entry point for all Java To WSDL needs.
 * <BR/> Features that can be set are derived from org.jboss.ws.tools.WSToolsConstants
 * <p/>
 * Notable ones are:<br/>
 * @see org.jboss.ws.tools.WSToolsConstants.WSTOOLS_FEATURE_RESTRICT_TO_TARGET_NS
 * @see org.jboss.ws.tools.WSToolsConstants.WSTOOLS_FEATURE_INCLUDE_SCHEMA_IN_WSDL
 * @see org.jboss.ws.tools.WSToolsConstants.WSTOOLS_FEATURE_USE_ANNOTATIONS
 *
 * @author Thomas.Diesler@jboss.org
 * @author Anil.Saldhana@jboss.org
 * @since 24-Jul-2005
 */
public class JavaToWSDL
{
   // provide logging
   private static final Logger log = Logger.getLogger(JavaToWSDL.class);

   // The required wsdl namespace URI
   private String wsdlNamespace;
   // The target namespace
   private String targetNamespace;
   //The type namespace (it can be different from the target namespace)
   private String typeNamespace;
   // The service name
   private String serviceName;
   // The portType name
   private String portTypeName;

   private Style style;

   private ParameterStyle parameterStyle;

   // Features as represented by Constants
   private Map<String, Boolean> features = new HashMap<String, Boolean>();

   // A Map of package/namespace mapping that needs to be passed onto types generator
   private Map<String,String> packageNamespaceMap = new HashMap<String, String>();

   private TypeMapping typeMapping = null;

   private JavaWsdlMapping javaWsdlMapping = null;

   private UnifiedMetaData umd = null;

   private boolean qualifiedElements = false;

   private Map<String, List<OperationConfig>> operationMap = null;

   /** Contruct a java to wsdl generator for a given wsdl version.
    * <p/>
    * WSDL-1.1 namespace URI: http://schemas.xmlsoap.org/wsdl/<br/>
    * WSDL-2.0 namespace URI: http://www.w3.org/2003/11/wsdl
    *
    * @param namespace wsdl namespace URI
    */
   public JavaToWSDL(String namespace)
   {
      if (Constants.NS_WSDL11.equals(namespace) == false)
         throw new IllegalArgumentException("Unsupported wsdl version: " + namespace);

      this.wsdlNamespace = namespace;
   }

   /**
    * Add a feature to this subsystem
    * @see org.jboss.ws.tools.WSToolsConstants
    * @param name
    * @param value
    */
   public void addFeature(String name, boolean value)
   {
      features.put(name, new Boolean(value));
   }

   /**
    * Return a feature if set
    * @see org.jboss.ws.tools.WSToolsConstants
    * @param name
    * @return boolean value representing the feature, if not
    * @throws IllegalStateException  Feature unrecognized
    */
   public boolean getFeature(String name)
   {
      Boolean val = features.get(name);
      if (val == null)
         throw new WSException("Feature value not available: " + name);

      return val.booleanValue();
   }

   /** Get the immutable wsdl namespace URI
    */
   public String getWsdlNamespace()
   {
      return wsdlNamespace;
   }

   /** Get the wsdl target namespace
    */
   public String getTargetNamespace()
   {
      return targetNamespace;
   }

   /** Set the wsdl target namespace
    */
   public void setTargetNamespace(String targetNamespace)
   {
      this.targetNamespace = targetNamespace;
   }

   /** Get the type Namespace */
   public String getTypeNamespace()
   {
      return typeNamespace;
   }

   /** Set the Type Namespace */
   public void setTypeNamespace(String typeNamespace)
   {
      this.typeNamespace = typeNamespace;
   }

   /** Get the wsdl service name
    */
   public String getServiceName()
   {
      return serviceName;
   }

   /** Set the wsdl service name
    */
   public void setServiceName(String serviceName)
   {
      this.serviceName = serviceName;
   }

   /** Get the wsdl service endpoint name
    */
   public String getPortTypeName()
   {
      return portTypeName;
   }

   /** Set the wsdl service PortType Name
    */
   public void setPortTypeName(String endpointName)
   {
      this.portTypeName = endpointName;
   }

   /**
    * During the WSDL generation process, a typeMapping will be
    * created that maps xml types -> java types
    *
    * @return  typeMapping
    * @exception IllegalStateException If typeMapping has not been generated
    */
   public TypeMapping getTypeMapping()
   {
      if(typeMapping == null)
         throw new WSException("TypeMapping has not been generated");
      return typeMapping;
   }

   public Style getStyle()
   {
      return style;
   }

   public void setStyle(Style style)
   {
      this.style = style;
   }

   public ParameterStyle getParameterStyle()
   {
      return parameterStyle;
   }

   public void setParameterStyle(ParameterStyle parameterStyle)
   {
      this.parameterStyle = parameterStyle;
   }

   /**
    * Users can customize a java package->xml namespace map
    * that will be used in the Java to WSDL process.
    * <br/>The package representing the endpoint will always be mapped
    * to the target namespace, irrespective of an attempt to
    * customize that in the map. If you desire to change that, then
    * think about changing just the type namespace as the types are
    * generated using the typenamespace.
    *
    * @param pn  The Map
    */
   public void setPackageNamespaceMap(Map<String,String> pn)
   {
      this.packageNamespaceMap = pn;
   }

   public void setOperationMap(Map<String, List<OperationConfig>> operationMap)
   {
      this.operationMap = operationMap;
   }

   /**
    * Clients of Tools can build a UnifiedMetaData externally
    * and pass it to the Java To WSDL subsystem [Optional]
    *
    * @param um
    */
   public void setUnifiedMetaData(UnifiedMetaData um)
   {
      this.umd = um;
   }

   public UnifiedMetaData getUnifiedMetaData()
   {
      return umd;
   }

   public void setUmd(UnifiedMetaData umd)
   {
      this.umd = umd;
   }

   public boolean isQualifiedElements()
   {
      return qualifiedElements;
   }

   public void setQualifiedElements(boolean qualifiedElements)
   {
      this.qualifiedElements = qualifiedElements;
   }

   public JavaWsdlMapping getJavaWsdlMapping()
   {
      return javaWsdlMapping;
   }

   /** Generate the common WSDL definition for a given endpoint
    */
   public WSDLDefinitions generate(Class endpoint)
   {
      if(log.isDebugEnabled()) log.debug("generate [endpoint=" + endpoint.getName() + ",tnsURI=" + targetNamespace + ",service=" + serviceName
            + ",portType=" + portTypeName + "]");

      if( umd == null)
      {
         umd = new ToolsUnifiedMetaDataBuilder(endpoint, targetNamespace,
              typeNamespace,  serviceName, style, parameterStyle, operationMap).getUnifiedMetaData();
      }

      if (typeNamespace != null)
         packageNamespaceMap.put(endpoint.getPackage().getName(), typeNamespace);

      WSDLDefinitions wsdlDefinitions = null;
      try
      {
         if (Constants.NS_WSDL11.equals(wsdlNamespace))
         {
            JavaToWSDL11 javaWSDL11 = new JavaToWSDL11();
            javaWSDL11.addFeatures(features);
            javaWSDL11.setPackageNamespaceMap(packageNamespaceMap);
            javaWSDL11.addFeatures(features);
            if( umd != null )
               javaWSDL11.setUnifiedMetaData(umd);
            javaWSDL11.setQualifiedElements(qualifiedElements);

            wsdlDefinitions = javaWSDL11.generate(endpoint);
            typeMapping = javaWSDL11.getTypeMapping();
            javaWsdlMapping = javaWSDL11.getJavaWsdlMapping();
         }
         if (wsdlDefinitions == null)
            throw new WSException("Cannot generate WSDL definitions");

         // Debug the generated wsdl
         StringWriter sw = new StringWriter();
         new WSDLWriter(wsdlDefinitions).write(sw, Constants.DEFAULT_XML_CHARSET);
         if(log.isDebugEnabled()) log.debug("Generated WSDL:\n" + sw.toString());

         // Debug the generated mapping file
         String jaxrpcMappingStr = null;
         if (javaWsdlMapping != null)
         {
            Element root = DOMUtils.parse(javaWsdlMapping.serialize());
            jaxrpcMappingStr = DOMWriter.printNode(root, true);
         }
         if(log.isDebugEnabled()) log.debug("Generated Mapping:\n" + jaxrpcMappingStr);
      }
      catch (RuntimeException rte)
      {
         throw rte;
      }
      catch (Exception e)
      {
         log.error("Cannot generate WSDL",e);
         throw new WSException("Cannot generate wsdl from: " + endpoint);
      }
      return wsdlDefinitions;
   }
}