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.wsdl.wsdl11;
18  
19  import javax.wsdl.Definition;
20  import javax.wsdl.WSDLException;
21  import javax.wsdl.factory.WSDLFactory;
22  import javax.wsdl.xml.WSDLWriter;
23  import javax.xml.transform.Source;
24  import javax.xml.transform.dom.DOMSource;
25  
26  import org.springframework.util.Assert;
27  import org.springframework.util.StringUtils;
28  import org.springframework.ws.wsdl.WsdlDefinitionException;
29  
30  import org.w3c.dom.Document;
31  
32  /**
33   * Implementation of the <code>Wsdl11Definition</code> based on WSDL4J. A [email protected] javax.wsdl.Definition} can be given as
34   * as constructor argument, or set using a property.
35   *
36   * @author Arjen Poutsma
37   * @see #Wsdl4jDefinition(javax.wsdl.Definition)
38   * @see #setDefinition(javax.wsdl.Definition)
39   * @since 1.0.0
40   */
41  public class Wsdl4jDefinition implements Wsdl11Definition {
42  
43      private Definition definition;
44  
45      /** Cached DOM version of the definition */
46      private Document document;
47  
48      /** WSDL4J is not thread safe, hence the need for a monitor. */
49      private final Object monitor = new Object();
50  
51      /**
52       * Constructs a new, empty <code>Wsdl4jDefinition</code>.
53       *
54       * @see #setDefinition(javax.wsdl.Definition)
55       */
56      public Wsdl4jDefinition() {
57      }
58  
59      /**
60       * Constructs a new <code>Wsdl4jDefinition</code> based on the given <code>Definition</code>.
61       *
62       * @param definition the WSDL4J definition
63       */
64      public Wsdl4jDefinition(Definition definition) {
65          setDefinition(definition);
66      }
67  
68      /** Returns the WSDL4J <code>Definition</code>. */
69      public Definition getDefinition() {
70          synchronized (monitor) {
71              return definition;
72          }
73      }
74  
75      /** Set the WSDL4J <code>Definition</code>. */
76      public void setDefinition(Definition definition) {
77          synchronized (monitor) {
78              this.definition = definition;
79              this.document = null;
80          }
81      }
82  
83      public Source getSource() {
84          synchronized (monitor) {
85              Assert.notNull(definition, "definition must not be null");
86              if (document == null) {
87                  try {
88                      WSDLFactory wsdlFactory = WSDLFactory.newInstance();
89                      WSDLWriter wsdlWriter = wsdlFactory.newWSDLWriter();
90                      document = wsdlWriter.getDocument(definition);
91                  }
92                  catch (WSDLException ex) {
93                      throw new WsdlDefinitionException(ex.getMessage(), ex);
94                  }
95              }
96          }
97          return new DOMSource(document);
98      }
99  
100     public String toString() {
101         StringBuilder builder = new StringBuilder("Wsdl4jDefinition");
102         if (definition != null && StringUtils.hasLength(definition.getTargetNamespace())) {
103             builder.append('{');
104             builder.append(definition.getTargetNamespace());
105             builder.append('}');
106         }
107         return builder.toString();
108     }
109 }