001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util.ant;
022:
023: import com.liferay.portal.kernel.util.StringUtil;
024: import com.liferay.util.FileUtil;
025: import com.liferay.util.Html;
026: import com.liferay.util.Time;
027: import com.liferay.util.xml.XMLFormatter;
028:
029: import java.io.File;
030: import java.io.IOException;
031:
032: import org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask;
033: import org.apache.axis.tools.ant.wsdl.NamespaceMapping;
034: import org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask;
035: import org.apache.tools.ant.Project;
036:
037: import org.dom4j.DocumentException;
038:
039: /**
040: * <a href="Java2WsddTask.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class Java2WsddTask {
046:
047: public static String[] generateWsdd(String className,
048: String serviceName) throws IOException {
049:
050: // Create temp directory
051:
052: File tempDir = new File(Time.getTimestamp());
053:
054: tempDir.mkdir();
055:
056: // axis-java2wsdl
057:
058: String wsdlFileName = tempDir + "/service.wsdl";
059:
060: int pos = className.lastIndexOf(".");
061:
062: String packagePath = className.substring(0, pos);
063:
064: String[] packagePaths = StringUtil.split(packagePath, ".");
065:
066: String namespace = "urn:";
067:
068: for (int i = packagePaths.length - 1; i >= 0; i--) {
069: namespace += packagePaths[i];
070:
071: if (i > 0) {
072: namespace += ".";
073: }
074: }
075:
076: String location = "http://localhost/services/" + serviceName;
077:
078: String mappingPackage = packagePath.substring(0, packagePath
079: .lastIndexOf("."))
080: + ".ws";
081:
082: Project project = AntUtil.getProject();
083:
084: Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask();
085:
086: NamespaceMapping mapping = new NamespaceMapping();
087:
088: mapping.setNamespace(namespace);
089: mapping.setPackage(mappingPackage);
090:
091: java2Wsdl.setProject(project);
092: java2Wsdl.setClassName(className);
093: java2Wsdl.setOutput(new File(wsdlFileName));
094: java2Wsdl.setLocation(location);
095: java2Wsdl.setNamespace(namespace);
096: java2Wsdl.addMapping(mapping);
097:
098: java2Wsdl.execute();
099:
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(tempDir + "/"
116: + StringUtil.replace(packagePath, ".", "/")
117: + "/deploy.wsdd");
118:
119: deployContent = StringUtil.replace(deployContent, packagePath
120: + "." + serviceName + "SoapBindingImpl", className);
121:
122: deployContent = _format(deployContent);
123:
124: String undeployContent = FileUtil.read(tempDir + "/"
125: + StringUtil.replace(packagePath, ".", "/")
126: + "/undeploy.wsdd");
127:
128: undeployContent = _format(undeployContent);
129:
130: // Delete temp directory
131:
132: DeleteTask.deleteDirectory(tempDir);
133:
134: return new String[] { deployContent, undeployContent };
135: }
136:
137: private static String _format(String content) throws IOException {
138: content = Html.stripComments(content);
139:
140: try {
141: content = XMLFormatter.toString(content);
142: } catch (DocumentException de) {
143: de.printStackTrace();
144: }
145:
146: return content;
147: }
148:
149: }
|