001    /**
002     * Copyright (c) 2000-2012 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.portal.tools;
016    
017    import com.liferay.portal.kernel.util.ArrayUtil;
018    import com.liferay.portal.kernel.util.CharPool;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    import com.liferay.portal.kernel.xml.Document;
024    import com.liferay.portal.kernel.xml.Element;
025    import com.liferay.portal.kernel.xml.SAXReaderUtil;
026    import com.liferay.portal.util.InitUtil;
027    import com.liferay.util.ant.Wsdl2JavaTask;
028    
029    import java.io.File;
030    
031    import java.util.List;
032    
033    /**
034     * @author Brian Wing Shun Chan
035     */
036    public class PortalClientBuilder {
037    
038            public static void main(String[] args) {
039                    InitUtil.initWithSpring();
040    
041                    if (args.length == 4) {
042                            new PortalClientBuilder(args[0], args[1], args[2], args[3]);
043                    }
044                    else {
045                            throw new IllegalArgumentException();
046                    }
047            }
048    
049            public PortalClientBuilder(
050                    String fileName, String outputDir, String mappingFile, String url) {
051    
052                    try {
053                            Document document = SAXReaderUtil.read(new File(fileName));
054    
055                            Element rootElement = document.getRootElement();
056    
057                            List<Element> serviceElements = rootElement.elements("service");
058    
059                            for (Element serviceElement : serviceElements) {
060                                    String serviceName = serviceElement.attributeValue("name");
061    
062                                    if (serviceName.startsWith("Plugin_") &&
063                                            !FileUtil.exists(mappingFile)) {
064    
065                                            _writePluginMappingFile(
066                                                    mappingFile, serviceElement, serviceName);
067                                    }
068    
069                                    if (serviceName.startsWith("Plugin_") ||
070                                            serviceName.startsWith("Portal_") ||
071                                            serviceName.startsWith("Portlet_")) {
072    
073                                            Wsdl2JavaTask.generateJava(
074                                                    url + "/" + serviceName + "?wsdl", outputDir,
075                                                    mappingFile);
076                                    }
077                            }
078                    }
079                    catch (Exception e) {
080                            e.printStackTrace();
081                    }
082    
083                    File testNamespace = new File(outputDir + "/com/liferay/portal");
084    
085                    if (testNamespace.exists()) {
086                            throw new RuntimeException(
087                                    "Please update " + mappingFile + " to namespace " +
088                                            "com.liferay.portal to com.liferay.client.soap.portal");
089                    }
090            }
091    
092            private void _writePluginMappingFile(
093                            String mappingFile, Element serviceElement, String serviceName)
094                    throws Exception {
095    
096                    String wsdlTargetNamespace = null;
097    
098                    List<Element> parameterElements = serviceElement.elements("parameter");
099    
100                    for (Element parameterElement : parameterElements) {
101                            String parameterName = parameterElement.attributeValue("name");
102    
103                            if (parameterName.equals("wsdlTargetNamespace")) {
104                                    wsdlTargetNamespace = parameterElement.attributeValue("value");
105    
106                                    break;
107                            }
108                    }
109    
110                    int pos = wsdlTargetNamespace.indexOf(".service.");
111    
112                    String soapNamespace = wsdlTargetNamespace.substring(pos + 9);
113    
114                    String[] soapNamespaceArray = StringUtil.split(
115                            soapNamespace, CharPool.PERIOD);
116    
117                    ArrayUtil.reverse(soapNamespaceArray);
118    
119                    soapNamespace = StringUtil.merge(soapNamespaceArray, StringPool.PERIOD);
120    
121                    pos = soapNamespace.lastIndexOf(StringPool.PERIOD);
122    
123                    soapNamespace =
124                            soapNamespace.substring(0, pos) + ".client.soap" +
125                                    soapNamespace.substring(pos);
126    
127                    StringBundler sb = new StringBundler(12);
128    
129                    sb.append("com.liferay.client.soap.portal.kernel.util=");
130                    sb.append("http://util.kernel.portal.liferay.com\n");
131    
132                    sb.append("com.liferay.client.soap.portal.model=");
133                    sb.append("http://model.portal.liferay.com\n");
134    
135                    sb.append("com.liferay.client.soap.portal.service=");
136                    sb.append("http://service.portal.liferay.com\n");
137    
138                    sb.append(soapNamespace);
139                    sb.append(".model=");
140                    sb.append("http://model.knowledgebase.liferay.com\n");
141    
142                    sb.append(soapNamespace);
143                    sb.append(".service.http=");
144                    sb.append("urn:http.service.knowledgebase.liferay.com\n");
145    
146                    FileUtil.write(mappingFile, sb.toString());
147            }
148    
149    }