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