1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.util.ant;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.HtmlUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.util.Time;
21  import com.liferay.util.xml.XMLFormatter;
22  
23  import java.io.File;
24  import java.io.IOException;
25  
26  import org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask;
27  import org.apache.axis.tools.ant.wsdl.NamespaceMapping;
28  import org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask;
29  import org.apache.tools.ant.Project;
30  
31  import org.dom4j.DocumentException;
32  
33  /**
34   * <a href="Java2WsddTask.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class Java2WsddTask {
39  
40      public static String[] generateWsdd(String className, String serviceName)
41          throws IOException {
42  
43          // Create temp directory
44  
45          File tempDir = new File(Time.getTimestamp());
46  
47          tempDir.mkdir();
48  
49          // axis-java2wsdl
50  
51          String wsdlFileName = tempDir + "/service.wsdl";
52  
53          int pos = className.lastIndexOf(".");
54  
55          String packagePath = className.substring(0, pos);
56  
57          String[] packagePaths = StringUtil.split(packagePath, ".");
58  
59          String namespace = "urn:";
60  
61          for (int i = packagePaths.length - 1; i >= 0; i--) {
62              namespace += packagePaths[i];
63  
64              if (i > 0) {
65                  namespace += ".";
66              }
67          }
68  
69          String location = "http://localhost/services/" + serviceName;
70  
71          String mappingPackage = packagePath.substring(
72              0, packagePath.lastIndexOf(".")) + ".ws";
73  
74          Project project = AntUtil.getProject();
75  
76          Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask();
77  
78          NamespaceMapping mapping = new NamespaceMapping();
79  
80          mapping.setNamespace(namespace);
81          mapping.setPackage(mappingPackage);
82  
83          java2Wsdl.setProject(project);
84          java2Wsdl.setClassName(className);
85          java2Wsdl.setOutput(new File(wsdlFileName));
86          java2Wsdl.setLocation(location);
87          java2Wsdl.setNamespace(namespace);
88          java2Wsdl.addMapping(mapping);
89  
90          java2Wsdl.execute();
91  
92          // axis-wsdl2java
93  
94          Wsdl2javaAntTask wsdl2Java = new Wsdl2javaAntTask();
95  
96          wsdl2Java.setProject(project);
97          wsdl2Java.setURL(wsdlFileName);
98          wsdl2Java.setOutput(tempDir);
99          wsdl2Java.setServerSide(true);
100         wsdl2Java.setTestCase(false);
101         wsdl2Java.setVerbose(false);
102 
103         wsdl2Java.execute();
104 
105         // Get content
106 
107         String deployContent = FileUtil.read(
108             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
109                 "/deploy.wsdd");
110 
111         deployContent = StringUtil.replace(
112             deployContent, packagePath + "." + serviceName + "SoapBindingImpl",
113             className);
114 
115         deployContent = _format(deployContent);
116 
117         String undeployContent = FileUtil.read(
118             tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
119                 "/undeploy.wsdd");
120 
121         undeployContent = _format(undeployContent);
122 
123         // Delete temp directory
124 
125         DeleteTask.deleteDirectory(tempDir);
126 
127         return new String[] {deployContent, undeployContent};
128     }
129 
130     private static String _format(String content) throws IOException {
131         content = HtmlUtil.stripComments(content);
132 
133         try {
134             content = XMLFormatter.toString(content);
135         }
136         catch (DocumentException de) {
137             de.printStackTrace();
138         }
139 
140         content = StringUtil.replace(content, "\"/>", "\" />");
141 
142         return content;
143     }
144 
145 }