1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class Java2WsddTask {
47  
48      public static String[] generateWsdd(String className, String serviceName)
49          throws IOException {
50  
51          // Create temp directory
52  
53          File tempDir = new File(Time.getTimestamp());
54  
55          tempDir.mkdir();
56  
57          // axis-java2wsdl
58  
59          String wsdlFileName = tempDir + "/service.wsdl";
60  
61          int pos = className.lastIndexOf(".");
62  
63          String packagePath = className.substring(0, pos);
64  
65          String[] packagePaths = StringUtil.split(packagePath, ".");
66  
67          String namespace = "urn:";
68  
69          for (int i = packagePaths.length - 1; i >= 0; i--) {
70              namespace += packagePaths[i];
71  
72              if (i > 0) {
73                  namespace += ".";
74              }
75          }
76  
77          String location = "http://localhost/services/" + serviceName;
78  
79          String mappingPackage = packagePath.substring(
80              0, packagePath.lastIndexOf(".")) + ".ws";
81  
82          Project project = AntUtil.getProject();
83  
84          Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask();
85  
86          NamespaceMapping mapping = new NamespaceMapping();
87  
88          mapping.setNamespace(namespace);
89          mapping.setPackage(mappingPackage);
90  
91          java2Wsdl.setProject(project);
92          java2Wsdl.setClassName(className);
93          java2Wsdl.setOutput(new File(wsdlFileName));
94          java2Wsdl.setLocation(location);
95          java2Wsdl.setNamespace(namespace);
96          java2Wsdl.addMapping(mapping);
97  
98          java2Wsdl.execute();
99  
100         // axis-wsdl2java
101 
102         Wsdl2javaAntTask wsdl2Java = new Wsdl2javaAntTask();
103 
104         wsdl2Java.setProject(project);
105         wsdl2Java.setURL(wsdlFileName);
106         wsdl2Java.setOutput(tempDir);
107         wsdl2Java.setServerSide(true);
108         wsdl2Java.setTestCase(false);
109         wsdl2Java.setVerbose(false);
110 
111         wsdl2Java.execute();
112 
113         // Get content
114 
115         String deployContent = FileUtil.read(
116             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
117                 "/deploy.wsdd");
118 
119         deployContent = StringUtil.replace(
120             deployContent, packagePath + "." + serviceName + "SoapBindingImpl",
121             className);
122 
123         deployContent = _format(deployContent);
124 
125         String undeployContent = FileUtil.read(
126             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
127                 "/undeploy.wsdd");
128 
129         undeployContent = _format(undeployContent);
130 
131         // Delete temp directory
132 
133         DeleteTask.deleteDirectory(tempDir);
134 
135         return new String[] {deployContent, undeployContent};
136     }
137 
138     private static String _format(String content) throws IOException {
139         content = HtmlUtil.stripComments(content);
140 
141         try {
142             content = XMLFormatter.toString(content);
143         }
144         catch (DocumentException de) {
145             de.printStackTrace();
146         }
147 
148         return content;
149     }
150 
151 }