1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.util.ant;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.HtmlUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.util.Time;
26  import com.liferay.util.xml.XMLFormatter;
27  
28  import java.io.File;
29  import java.io.IOException;
30  
31  import org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask;
32  import org.apache.axis.tools.ant.wsdl.NamespaceMapping;
33  import org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask;
34  import org.apache.tools.ant.Project;
35  
36  import org.dom4j.DocumentException;
37  
38  /**
39   * <a href="Java2WsddTask.java.html"><b><i>View Source</i></b></a>
40   *
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public class Java2WsddTask {
45  
46      public static String[] generateWsdd(String className, String serviceName)
47          throws IOException {
48  
49          // Create temp directory
50  
51          File tempDir = new File(Time.getTimestamp());
52  
53          tempDir.mkdir();
54  
55          // axis-java2wsdl
56  
57          String wsdlFileName = tempDir + "/service.wsdl";
58  
59          int pos = className.lastIndexOf(".");
60  
61          String packagePath = className.substring(0, pos);
62  
63          String[] packagePaths = StringUtil.split(packagePath, ".");
64  
65          String namespace = "urn:";
66  
67          for (int i = packagePaths.length - 1; i >= 0; i--) {
68              namespace += packagePaths[i];
69  
70              if (i > 0) {
71                  namespace += ".";
72              }
73          }
74  
75          String location = "http://localhost/services/" + serviceName;
76  
77          String mappingPackage = packagePath.substring(
78              0, packagePath.lastIndexOf(".")) + ".ws";
79  
80          Project project = AntUtil.getProject();
81  
82          Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask();
83  
84          NamespaceMapping mapping = new NamespaceMapping();
85  
86          mapping.setNamespace(namespace);
87          mapping.setPackage(mappingPackage);
88  
89          java2Wsdl.setProject(project);
90          java2Wsdl.setClassName(className);
91          java2Wsdl.setOutput(new File(wsdlFileName));
92          java2Wsdl.setLocation(location);
93          java2Wsdl.setNamespace(namespace);
94          java2Wsdl.addMapping(mapping);
95  
96          java2Wsdl.execute();
97  
98          // axis-wsdl2java
99  
100         Wsdl2javaAntTask wsdl2Java = new Wsdl2javaAntTask();
101 
102         wsdl2Java.setProject(project);
103         wsdl2Java.setURL(wsdlFileName);
104         wsdl2Java.setOutput(tempDir);
105         wsdl2Java.setServerSide(true);
106         wsdl2Java.setTestCase(false);
107         wsdl2Java.setVerbose(false);
108 
109         wsdl2Java.execute();
110 
111         // Get content
112 
113         String deployContent = FileUtil.read(
114             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
115                 "/deploy.wsdd");
116 
117         deployContent = StringUtil.replace(
118             deployContent, packagePath + "." + serviceName + "SoapBindingImpl",
119             className);
120 
121         deployContent = _format(deployContent);
122 
123         String undeployContent = FileUtil.read(
124             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
125                 "/undeploy.wsdd");
126 
127         undeployContent = _format(undeployContent);
128 
129         // Delete temp directory
130 
131         DeleteTask.deleteDirectory(tempDir);
132 
133         return new String[] {deployContent, undeployContent};
134     }
135 
136     private static String _format(String content) throws IOException {
137         content = HtmlUtil.stripComments(content);
138 
139         try {
140             content = XMLFormatter.toString(content);
141         }
142         catch (DocumentException de) {
143             de.printStackTrace();
144         }
145 
146         return content;
147     }
148 
149 }