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(
065                                                    mappingFile, serviceElement, serviceName);
066                                    }
067    
068                                    if (serviceName.startsWith("Plugin_") ||
069                                            serviceName.startsWith("Portal_") ||
070                                            serviceName.startsWith("Portlet_")) {
071    
072                                            Wsdl2JavaTask.generateJava(
073                                                    url + "/" + serviceName + "?wsdl", outputDir,
074                                                    mappingFile);
075                                    }
076                            }
077                    }
078                    catch (Exception e) {
079                            e.printStackTrace();
080                    }
081    
082                    File testNamespace = new File(outputDir + "/com/liferay/portal");
083    
084                    if (testNamespace.exists()) {
085                            throw new RuntimeException(
086                                    "Please update " + mappingFile + " to namespace " +
087                                            "com.liferay.portal to com.liferay.client.soap.portal");
088                    }
089            }
090    
091            private void _writePluginMappingFile(
092                            String mappingFile, Element serviceElement, String serviceName)
093                    throws Exception {
094    
095                    String wsdlTargetNamespace = null;
096    
097                    List<Element> parameterElements = serviceElement.elements("parameter");
098    
099                    for (Element parameterElement : parameterElements) {
100                            String parameterName = parameterElement.attributeValue("name");
101    
102                            if (parameterName.equals("wsdlTargetNamespace")) {
103                                    wsdlTargetNamespace = parameterElement.attributeValue("value");
104    
105                                    break;
106                            }
107                    }
108    
109                    int pos = wsdlTargetNamespace.indexOf(".service.");
110    
111                    String soapNamespace = wsdlTargetNamespace.substring(pos + 9);
112    
113                    String[] soapNamespaceArray = StringUtil.split(
114                            soapNamespace, CharPool.PERIOD);
115    
116                    ArrayUtil.reverse(soapNamespaceArray);
117    
118                    soapNamespace = StringUtil.merge(soapNamespaceArray, StringPool.PERIOD);
119    
120                    pos = soapNamespace.lastIndexOf(StringPool.PERIOD);
121    
122                    soapNamespace =
123                            soapNamespace.substring(0, pos) + ".client.soap" +
124                                    soapNamespace.substring(pos);
125    
126                    StringBundler sb = new StringBundler(12);
127    
128                    sb.append("com.liferay.client.soap.portal.kernel.util=");
129                    sb.append("http://util.kernel.portal.liferay.com\n");
130    
131                    sb.append("com.liferay.client.soap.portal.model=");
132                    sb.append("http://model.portal.liferay.com\n");
133    
134                    sb.append("com.liferay.client.soap.portal.service=");
135                    sb.append("http://service.portal.liferay.com\n");
136    
137                    sb.append(soapNamespace);
138                    sb.append(".model=");
139                    sb.append("http://model.knowledgebase.liferay.com\n");
140    
141                    sb.append(soapNamespace);
142                    sb.append(".service.http=");
143                    sb.append("urn:http.service.knowledgebase.liferay.com\n");
144    
145                    FileUtil.write(mappingFile, sb.toString());
146            }
147    
148    }