001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.ant;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.HtmlUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.util.Time;
021    import com.liferay.util.xml.XMLFormatter;
022    
023    import java.io.File;
024    import java.io.IOException;
025    
026    import org.apache.axis.tools.ant.wsdl.Java2WsdlAntTask;
027    import org.apache.axis.tools.ant.wsdl.NamespaceMapping;
028    import org.apache.axis.tools.ant.wsdl.Wsdl2javaAntTask;
029    import org.apache.tools.ant.Project;
030    
031    import org.dom4j.DocumentException;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class Java2WsddTask {
037    
038            public static String[] generateWsdd(String className, String serviceName)
039                    throws IOException {
040    
041                    // Create temp directory
042    
043                    File tempDir = new File(Time.getTimestamp());
044    
045                    tempDir.mkdir();
046    
047                    // axis-java2wsdl
048    
049                    String wsdlFileName = tempDir + "/service.wsdl";
050    
051                    int pos = className.lastIndexOf(".");
052    
053                    String packagePath = className.substring(0, pos);
054    
055                    String[] packagePaths = StringUtil.split(packagePath, ".");
056    
057                    String namespace = "urn:";
058    
059                    for (int i = packagePaths.length - 1; i >= 0; i--) {
060                            namespace += packagePaths[i];
061    
062                            if (i > 0) {
063                                    namespace += ".";
064                            }
065                    }
066    
067                    String location = "http://localhost/services/" + serviceName;
068    
069                    String mappingPackage = packagePath.substring(
070                            0, packagePath.lastIndexOf(".")) + ".ws";
071    
072                    Project project = AntUtil.getProject();
073    
074                    Java2WsdlAntTask java2Wsdl = new Java2WsdlAntTask();
075    
076                    NamespaceMapping mapping = new NamespaceMapping();
077    
078                    mapping.setNamespace(namespace);
079                    mapping.setPackage(mappingPackage);
080    
081                    java2Wsdl.setProject(project);
082                    java2Wsdl.setClassName(className);
083                    java2Wsdl.setOutput(new File(wsdlFileName));
084                    java2Wsdl.setLocation(location);
085                    java2Wsdl.setNamespace(namespace);
086                    java2Wsdl.addMapping(mapping);
087    
088                    java2Wsdl.execute();
089    
090                    // axis-wsdl2java
091    
092                    Wsdl2javaAntTask wsdl2Java = new Wsdl2javaAntTask();
093    
094                    wsdl2Java.setProject(project);
095                    wsdl2Java.setURL(wsdlFileName);
096                    wsdl2Java.setOutput(tempDir);
097                    wsdl2Java.setServerSide(true);
098                    wsdl2Java.setTestCase(false);
099                    wsdl2Java.setVerbose(false);
100    
101                    wsdl2Java.execute();
102    
103                    // Get content
104    
105                    String deployContent = FileUtil.read(
106                            tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
107                                    "/deploy.wsdd");
108    
109                    deployContent = StringUtil.replace(
110                            deployContent, packagePath + "." + serviceName + "SoapBindingImpl",
111                            className);
112    
113                    deployContent = _format(deployContent);
114    
115                    String undeployContent = FileUtil.read(
116                            tempDir + "/" + StringUtil.replace(packagePath, ".", "/") +
117                                    "/undeploy.wsdd");
118    
119                    undeployContent = _format(undeployContent);
120    
121                    // Delete temp directory
122    
123                    DeleteTask.deleteDirectory(tempDir);
124    
125                    return new String[] {deployContent, undeployContent};
126            }
127    
128            private static String _format(String content) throws IOException {
129                    content = HtmlUtil.stripComments(content);
130    
131                    try {
132                            content = XMLFormatter.toString(content);
133                    }
134                    catch (DocumentException de) {
135                            de.printStackTrace();
136                    }
137    
138                    content = StringUtil.replace(content, "\"/>", "\" />");
139    
140                    return content;
141            }
142    
143    }