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