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