File: ToolsSchemaConfigReader.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 (378 lines) | stat: -rw-r--r-- 14,210 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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
 * 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.config;

// $Id: ToolsSchemaConfigReader.java 5390 2008-01-02 14:14:14Z mageshbk@jboss.com $

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.jboss.logging.Logger;
import org.jboss.ws.WSException;
import org.jboss.ws.tools.Configuration;
import org.jboss.ws.tools.Configuration.GlobalConfig;
import org.jboss.ws.tools.Configuration.JavaToWSDLConfig;
import org.jboss.ws.tools.Configuration.OperationConfig;
import org.jboss.ws.tools.Configuration.ParameterConfig;
import org.jboss.ws.tools.Configuration.WSDLToJavaConfig;
import org.jboss.xb.binding.JBossXBException;
import org.jboss.xb.binding.ObjectModelFactory;
import org.jboss.xb.binding.Unmarshaller;
import org.jboss.xb.binding.UnmarshallerFactory;
import org.jboss.xb.binding.UnmarshallingContext;
import org.xml.sax.Attributes;

/**
 *  Reads the XML configuration file passed to jbossws
 *  @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
 *  @author Thomas.Diesler@jboss.org
 *  @since   11-Aug-2005
 */
public class ToolsSchemaConfigReader implements ObjectModelFactory
{
   // Tags
   private static final String PARAMETER_TAG = "parameter";
   private static final String WEBSERVICES_TAG = "webservices";
   private static final String MAPPING_TAG = "mapping";
   private static final String PACKAGE_NAMESPACE_TAG = "package-namespace";
   private static final String NAMESPACES_TAG = "namespaces";
   private static final String SERVICE_TAG = "service";
   private static final String GLOBAL_TAG = "global";
   private static final String JAVA_WSDL_TAG = "java-wsdl";
   private static final String WSDL_JAVA_TAG = "wsdl-java";
   private static final String OPERATION_TAG = "operation";

   // Attributes
   private static final String HEADER_ATTRIBUTE = "header";
   private static final String MODE_ATTRIBUTE = "mode";
   private static final String XML_NAME_ATTRIBUTE = "xml-name";
   private static final String TYPE_ATTRIBUTE = "type";
   private static final String NAMESPACE_ATTRIBUTE = "namespace";
   private static final String PACKAGE_ATTRIBUTE = "package";
   private static final String EJB_LINK_ATTRIBUTE = "ejb-link";
   private static final String SERVLET_LINK_ATTRIBUTE = "servlet-link";
   private static final String APPEND_ATTRIBUTE = "append";
   private static final String TYPE_NAMESPACE_ATTRIBUTE = "type-namespace";
   private static final String TARGET_NAMESPACE_ATTRIBUTE = "target-namespace";
   private static final String RETURN_XML_NAME_ATTRIBUTE = "return-xml-name";
   private static final String ONEWAY_ATTRIBUTE = "one-way";
   private static final String PARAMETER_STYLE_ATTRIBUTE = "parameter-style";
   private static final String STYLE_ATTRIBUTE = "style";
   private static final String ENDPOINT_ATTRIBUTE = "endpoint";
   private static final String NAME_ATTRIBUTE = "name";
   private static final String LOCATION_ATTRIBUTE = "location";
   private static final String FILE_ATTRIBUTE = "file";

   // provide logging
   private static final Logger log = Logger.getLogger(ToolsSchemaConfigReader.class);

   public ToolsSchemaConfigReader()
   {
   }

   public Configuration readConfig(String configLocation) throws IOException
   {
      log.trace("Inside readConfig: " + configLocation);
      if (configLocation == null)
         throw new IllegalArgumentException("Config URL passed is null");

      URL configURL = null;
      try
      {
         configURL = new URL(configLocation);
      }
      catch (MalformedURLException e)
      {
         // ignore
      }

      if (configURL == null)
      {
         File configFile = new File(configLocation);
         if (configFile.exists())
            configURL = configFile.toURL();
      }

      if (configURL == null)
      {
         ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
         configURL = ctxLoader.getResource(configLocation);
      }

      if (configURL == null)
         throw new IllegalArgumentException("Cannot load config from: " + configLocation);

      Configuration config = new Configuration();
      InputStream is = configURL.openStream();
      try
      {
         Unmarshaller unmarshaller = UnmarshallerFactory.newInstance().newUnmarshaller();
         unmarshaller.setNamespaceAware(true);
         unmarshaller.setSchemaValidation(true);
         unmarshaller.setValidation(true);
         unmarshaller.unmarshal(is, this, config);
      }
      catch (JBossXBException ex)
      {
         IOException ioex = new IOException("Cannot parse config: " + ex.getMessage());
         ioex.initCause(ex);
         throw ioex;
      }
      finally
      {
         is.close();
      }

      if (config.getJavaToWSDLConfig(false) == null && config.getWSDLToJavaConfig(false) == null)
         throw new WSException("Invalid configuration file, either " + JAVA_WSDL_TAG + ", or " + WSDL_JAVA_TAG + " must be present");

      log.trace("Exit readConfig");
      return config;
   }

   public Object newRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName, Attributes attrs)
   {
      if (root instanceof Configuration)
         return root;
      else return null;
   }

   /**
    * This method is called by the object model factory and returns the root of the object graph.
    */
   public Object newChild(Configuration config, UnmarshallingContext navigator, String namespaceURI, String localName, Attributes attrs)
   {
      log.trace("Inside newChild:localName=" + localName);
      if (JAVA_WSDL_TAG.equals(localName))
      {
         JavaToWSDLConfig j2wsdlc = config.getJavaToWSDLConfig(true);
         return j2wsdlc;
      }
      else if (WSDL_JAVA_TAG.equals(localName))
      {
         WSDLToJavaConfig wsdl2jc = config.getWSDLToJavaConfig(true);
         wsdl2jc.wsdlLocation = attrs.getValue(LOCATION_ATTRIBUTE);
         String paramStyle = attrs.getValue(PARAMETER_STYLE_ATTRIBUTE);
         if (paramStyle != null)
            wsdl2jc.parameterStyle = paramStyle;

         return wsdl2jc;
      }
      else if (GLOBAL_TAG.equals(localName))
      {
         GlobalConfig globalc = config.getGlobalConfig(true);
         return globalc;
      }
      return config;
   }

   /**
    * This method is called by the object model factory and returns the root of the object graph.
    */
   public Object newChild(JavaToWSDLConfig j2wsdlc, UnmarshallingContext navigator, String namespaceURI, String localName, Attributes attrs)
   {
      String errorStr = "Problem parsing tag:" + JAVA_WSDL_TAG;

      if (SERVICE_TAG.equals(localName))
      {
         j2wsdlc.serviceName = attrs.getValue(NAME_ATTRIBUTE);
         j2wsdlc.endpointName = attrs.getValue(ENDPOINT_ATTRIBUTE);
         j2wsdlc.wsdlStyle = getOptionalAttribute(attrs, STYLE_ATTRIBUTE, "document");
         j2wsdlc.parameterStyle = getOptionalAttribute(attrs, PARAMETER_STYLE_ATTRIBUTE, "wrapped");
      }
      else if (OPERATION_TAG.equals(localName))
      {
         OperationConfig operation = j2wsdlc.createOperationConfig();
         operation.name = attrs.getValue(NAME_ATTRIBUTE);
         String oneWay = attrs.getValue(ONEWAY_ATTRIBUTE);
         operation.isOneWay = "true".equals(oneWay) || "1".equals(oneWay);
         String returnXmlName = attrs.getValue(RETURN_XML_NAME_ATTRIBUTE);
         if (returnXmlName != null)
            operation.returnXmlName = navigator.resolveQName(returnXmlName);

         return operation;
      }
      else if (NAMESPACES_TAG.equals(localName))
      {
         errorStr += NAMESPACES_TAG;
         j2wsdlc.targetNamespace = getNamespace(navigator, TARGET_NAMESPACE_ATTRIBUTE, errorStr, attrs);
         j2wsdlc.typeNamespace = getNamespace(navigator, TYPE_NAMESPACE_ATTRIBUTE, errorStr, attrs);
         if (j2wsdlc.typeNamespace == null)
            j2wsdlc.typeNamespace = j2wsdlc.targetNamespace;
      }
      else if (MAPPING_TAG.equals(localName))
      {
         j2wsdlc.mappingFileNeeded = true;
         j2wsdlc.mappingFileName = getOptionalAttribute(attrs, FILE_ATTRIBUTE, "jaxrpc-mapping.xml");
      }
      else if (WEBSERVICES_TAG.equals(localName))
      {
         j2wsdlc.wsxmlFileNeeded = true;
         j2wsdlc.servletLink = getOptionalAttribute(attrs, SERVLET_LINK_ATTRIBUTE, null);
         j2wsdlc.ejbLink = getOptionalAttribute(attrs, EJB_LINK_ATTRIBUTE, null);
         if (j2wsdlc.ejbLink == null && j2wsdlc.servletLink == null)
            throw new WSException("Either servletLink or ejbLink should be specified");
         String wsxmlFileAppend = attrs.getValue(APPEND_ATTRIBUTE);
         j2wsdlc.wsxmlFileAppend = "true".equals(wsxmlFileAppend) || "1".equals(wsxmlFileAppend);
      }
      return j2wsdlc;
   }

   /**
    * This method is called by the object model factory and returns the root of the object graph.
    */
   public Object newChild(GlobalConfig globalc, UnmarshallingContext navigator, String namespaceURI, String localName, Attributes attrs)
   {
      if (PACKAGE_NAMESPACE_TAG.equals(localName))
      {
         String pkgname = attrs.getValue(PACKAGE_ATTRIBUTE);
         String ns = attrs.getValue(NAMESPACE_ATTRIBUTE);
         globalc.packageNamespaceMap.put(ns, pkgname);
      }
      return globalc;
   }

   /**
    * This method is called by the object model factory and returns the root of the object graph.
    */
   public Object newChild(WSDLToJavaConfig wsdl2jc, UnmarshallingContext navigator, String namespaceURI, String localName, Attributes attrs)
   {
      if (MAPPING_TAG.equals(localName))
      {
         wsdl2jc.mappingFileNeeded = true;
         wsdl2jc.mappingFileName = getOptionalAttribute(attrs, FILE_ATTRIBUTE, "jaxrpc-mapping.xml");
      }
      else if (WEBSERVICES_TAG.equals(localName))
      {
         wsdl2jc.wsxmlFileNeeded = true;
         wsdl2jc.servletLink = getOptionalAttribute(attrs, SERVLET_LINK_ATTRIBUTE, null);
         wsdl2jc.ejbLink = getOptionalAttribute(attrs, EJB_LINK_ATTRIBUTE, null);
         if (wsdl2jc.ejbLink == null && wsdl2jc.servletLink == null)
            throw new WSException("Either servletLink or ejbLink should be specified");
      }
      return wsdl2jc;
   }

   /**
    * This method is called by the object model factory and returns the root of the object graph.
    */
   public Object newChild(OperationConfig op, UnmarshallingContext navigator, String namespaceURI, String localName, Attributes attrs)
   {
      if (PARAMETER_TAG.equals(localName))
      {
         ParameterConfig parameter = op.createParameterConfig();
         parameter.javaType = attrs.getValue(TYPE_ATTRIBUTE);
         String xmlName = attrs.getValue(XML_NAME_ATTRIBUTE);
         if (xmlName != null)
            parameter.xmlName = navigator.resolveQName(xmlName);
         parameter.mode = attrs.getValue(MODE_ATTRIBUTE);
         String header = attrs.getValue(HEADER_ATTRIBUTE);
         if (header != null)
            parameter.header = "true".equals(header) || "1".equals(header);

         return parameter;
      }

      return null;
   }

   /**
    */
   public Object addChild(Configuration config, WSDLToJavaConfig wsdl2jc, UnmarshallingContext navigator, String namespaceURI, String localName)
   {
      config.setWSDLToJavaConfig(wsdl2jc);
      return config;
   }

   /**
    */
   public Object addChild(Configuration config, GlobalConfig global, UnmarshallingContext navigator, String namespaceURI, String localName)
   {
      config.setGlobalConfig(global);
      return config;
   }

   /**
    */
   public Object addChild(Configuration config, JavaToWSDLConfig j2wc, UnmarshallingContext navigator, String namespaceURI, String localName)
   {
      config.setJavaToWSDLConfig(j2wc);
      return config;
   }

   /**
    */
   public Object addChild(JavaToWSDLConfig j2wc, OperationConfig opc, UnmarshallingContext navigator, String namespaceURI, String localName)
   {
      List<OperationConfig> list = j2wc.operations.get(opc.name);
      if (list == null)
      {
         list = new ArrayList<OperationConfig>();
         list.add(opc);
         j2wc.operations.put(opc.name, list);
      }
      else
      {
         list.add(opc);
      }

      return j2wc;
   }

   public Object addChild(OperationConfig opc, ParameterConfig pc, UnmarshallingContext navigator, String namespaceURI, String localName)
   {
      opc.params.add(pc);
      return opc;
   }

   public Object completeRoot(Object root, UnmarshallingContext ctx, String namespaceURI, String localName)
   {
      return root;
   }

   //PRIVATE METHODS
   private String getOptionalAttribute(Attributes attrs, String attribName, String defaultValue)
   {
      String value = attrs.getValue(attribName);
      if (value == null)
         return defaultValue;

      return value;
   }

   private String getNamespace(UnmarshallingContext navigator, String attribName, String errorStr, Attributes attrs)
   {
      try
      {
         return attrs.getValue(attribName);
      }
      catch (RuntimeException e)
      {
         throw new WSException(errorStr + " attribute=" + attribName);
      }
   }
}