001
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
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:
130
131 sb.append("com.liferay.client.soap.portal.model=");
132 sb.append("http:
133
134 sb.append("com.liferay.client.soap.portal.service=");
135 sb.append("http:
136
137 sb.append(soapNamespace);
138 sb.append(".model=");
139 sb.append("http:
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 }