View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
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  package org.springframework.ws.config;
18  
19  import java.util.List;
20  
21  import org.springframework.beans.factory.config.BeanDefinition;
22  import org.springframework.beans.factory.support.AbstractBeanDefinition;
23  import org.springframework.beans.factory.support.BeanDefinitionBuilder;
24  import org.springframework.beans.factory.support.ManagedList;
25  import org.springframework.beans.factory.support.RootBeanDefinition;
26  import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser;
27  import org.springframework.beans.factory.xml.ParserContext;
28  import org.springframework.util.ClassUtils;
29  import org.springframework.util.StringUtils;
30  import org.springframework.util.xml.DomUtils;
31  import org.springframework.ws.wsdl.wsdl11.DefaultWsdl11Definition;
32  import org.springframework.xml.xsd.SimpleXsdSchema;
33  import org.springframework.xml.xsd.commons.CommonsXsdSchemaCollection;
34  
35  import org.w3c.dom.Element;
36  
37  /**
38   * Parser for the {@code <sws:dynamic-wsdl/>} element.
39   *
40   * @author Arjen Poutsma
41   * @since 2.0
42   */
43  class DynamicWsdlBeanDefinitionParser extends AbstractBeanDefinitionParser {
44  
45      private static final boolean commonsSchemaPresent = ClassUtils.isPresent("org.apache.ws.commons.schema.XmlSchema",
46              DynamicWsdlBeanDefinitionParser.class.getClassLoader());
47  
48      @Override
49      protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
50          Object source = parserContext.extractSource(element);
51  
52          BeanDefinitionBuilder wsdlBuilder = BeanDefinitionBuilder.rootBeanDefinition(DefaultWsdl11Definition.class);
53          wsdlBuilder.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
54          wsdlBuilder.getRawBeanDefinition().setSource(source);
55  
56          addProperty(element, wsdlBuilder, "portTypeName");
57          addProperty(element, wsdlBuilder, "targetNamespace");
58          addProperty(element, wsdlBuilder, "requestSuffix");
59          addProperty(element, wsdlBuilder, "responseSuffix");
60          addProperty(element, wsdlBuilder, "faultSuffix");
61          addProperty(element, wsdlBuilder, "createSoap11Binding");
62          addProperty(element, wsdlBuilder, "createSoap12Binding");
63          addProperty(element, wsdlBuilder, "transportUri");
64          addProperty(element, wsdlBuilder, "locationUri");
65          addProperty(element, wsdlBuilder, "serviceName");
66  
67          List<Element> schemas = DomUtils.getChildElementsByTagName(element, "xsd");
68          if (commonsSchemaPresent) {
69              RootBeanDefinition collectionDef = createBeanDefinition(CommonsXsdSchemaCollection.class, source);
70              collectionDef.getPropertyValues().addPropertyValue("inline", "true");
71              ManagedList<String> xsds = new ManagedList<String>();
72              xsds.setSource(source);
73              for (Element schema : schemas) {
74                  xsds.add(schema.getAttribute("location"));
75              }
76              collectionDef.getPropertyValues().addPropertyValue("xsds", xsds);
77              String collectionName = parserContext.getReaderContext().registerWithGeneratedName(collectionDef);
78              wsdlBuilder.addPropertyReference("schemaCollection", collectionName);
79          }
80          else {
81              if (schemas.size() > 1) {
82                  throw new IllegalArgumentException(
83                          "Multiple <xsd/> elements requires Commons XMLSchema." +
84                                  "Please put Commons XMLSchema on the classpath.");
85              }
86              RootBeanDefinition schemaDef = createBeanDefinition(SimpleXsdSchema.class, source);
87              Element schema = schemas.iterator().next();
88              schemaDef.getPropertyValues().addPropertyValue("xsd", schema.getAttribute("location"));
89              String schemaName = parserContext.getReaderContext().registerWithGeneratedName(schemaDef);
90              wsdlBuilder.addPropertyReference("schema", schemaName);
91          }
92          return wsdlBuilder.getBeanDefinition();
93      }
94  
95      private void addProperty(Element element, BeanDefinitionBuilder builder, String propertyName) {
96          String propertyValue = element.getAttribute(propertyName);
97          if (StringUtils.hasText(propertyValue)) {
98              builder.addPropertyValue(propertyName, propertyValue);
99          }
100     }
101 
102     private RootBeanDefinition createBeanDefinition(Class<?> beanClass, Object source) {
103         RootBeanDefinition beanDefinition = new RootBeanDefinition(beanClass);
104         beanDefinition.setSource(source);
105         beanDefinition.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
106         return beanDefinition;
107     }
108 
109 }