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