1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.ArrayUtil;
18  import com.liferay.portal.kernel.util.FileUtil;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.StringUtil;
22  import com.liferay.portal.kernel.xml.Document;
23  import com.liferay.portal.kernel.xml.Element;
24  import com.liferay.portal.kernel.xml.SAXReaderUtil;
25  import com.liferay.portal.util.InitUtil;
26  import com.liferay.util.ant.Wsdl2JavaTask;
27  
28  import java.io.File;
29  
30  import java.util.Iterator;
31  import java.util.List;
32  
33  /**
34   * <a href="PortalClientBuilder.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class PortalClientBuilder {
39  
40      public static void main(String[] args) {
41          InitUtil.initWithSpring();
42  
43          if (args.length == 4) {
44              new PortalClientBuilder(args[0], args[1], args[2], args[3]);
45          }
46          else {
47              throw new IllegalArgumentException();
48          }
49      }
50  
51      public PortalClientBuilder(
52          String fileName, String outputDir, String mappingFile, String url) {
53  
54          try {
55              Document document = SAXReaderUtil.read(new File(fileName));
56  
57              Element rootElement = document.getRootElement();
58  
59              Iterator<Element> itr = rootElement.elements("service").iterator();
60  
61              while (itr.hasNext()) {
62                  Element serviceElement = itr.next();
63  
64                  String serviceName = serviceElement.attributeValue("name");
65  
66                  if (serviceName.startsWith("Plugin_") &&
67                      !FileUtil.exists(mappingFile)) {
68  
69                      _writePluginMappingFile(
70                          mappingFile, serviceElement, serviceName);
71                  }
72  
73                  if (serviceName.startsWith("Plugin_") ||
74                      serviceName.startsWith("Portal_") ||
75                      serviceName.startsWith("Portlet_")) {
76  
77                      Wsdl2JavaTask.generateJava(
78                          url + "/" +  serviceName + "?wsdl", outputDir,
79                          mappingFile);
80                  }
81              }
82          }
83          catch (Exception e) {
84              e.printStackTrace();
85          }
86  
87          File testNamespace = new File(outputDir + "/com/liferay/portal");
88  
89          if (testNamespace.exists()) {
90              throw new RuntimeException(
91                  "Please update " + mappingFile + " to namespace " +
92                      "com.liferay.portal to com.liferay.client.soap.portal");
93          }
94      }
95  
96      private void _writePluginMappingFile(
97              String mappingFile, Element serviceElement, String serviceName)
98          throws Exception {
99  
100         String wsdlTargetNamespace = null;
101 
102         List<Element> parameterElements = serviceElement.elements("parameter");
103 
104         for (Element parameterElement : parameterElements) {
105             String parameterName = parameterElement.attributeValue("name");
106 
107             if (parameterName.equals("wsdlTargetNamespace")) {
108                 wsdlTargetNamespace = parameterElement.attributeValue("value");
109 
110                 break;
111             }
112         }
113 
114         int pos = wsdlTargetNamespace.indexOf(".service.");
115 
116         String soapNamespace = wsdlTargetNamespace.substring(pos + 9);
117 
118         String[] soapNamespaceArray = StringUtil.split(
119             soapNamespace, StringPool.PERIOD);
120 
121         ArrayUtil.reverse(soapNamespaceArray);
122 
123         soapNamespace = StringUtil.merge(soapNamespaceArray, StringPool.PERIOD);
124 
125         pos = soapNamespace.lastIndexOf(StringPool.PERIOD);
126 
127         soapNamespace =
128             soapNamespace.substring(0, pos) + ".client.soap" +
129                 soapNamespace.substring(pos);
130 
131         StringBundler sb = new StringBundler(12);
132 
133         sb.append("com.liferay.client.soap.portal.kernel.util=");
134         sb.append("http://util.kernel.portal.liferay.com\n");
135 
136         sb.append("com.liferay.client.soap.portal.model=");
137         sb.append("http://model.portal.liferay.com\n");
138 
139         sb.append("com.liferay.client.soap.portal.service=");
140         sb.append("http://service.portal.liferay.com\n");
141 
142         sb.append(soapNamespace);
143         sb.append(".model=");
144         sb.append("http://model.knowledgebase.liferay.com\n");
145 
146         sb.append(soapNamespace);
147         sb.append(".service.http=");
148         sb.append("urn:http.service.knowledgebase.liferay.com\n");
149 
150         FileUtil.write(mappingFile, sb.toString());
151     }
152 
153 }