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.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.kernel.xml.SAXReaderUtil;
23  import com.liferay.portal.util.InitUtil;
24  import com.liferay.util.ant.Java2WsddTask;
25  
26  import java.io.File;
27  import java.io.IOException;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="WSDDBuilder.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class WSDDBuilder {
38  
39      public static void main(String[] args) {
40          InitUtil.initWithSpring();
41  
42          if (args.length == 2) {
43              new WSDDBuilder(args[0], args[1]);
44          }
45          else {
46              throw new IllegalArgumentException();
47          }
48      }
49  
50      public WSDDBuilder(String fileName, String serverConfigFileName) {
51          try {
52              _serverConfigFileName = serverConfigFileName;
53  
54              if (!FileUtil.exists(_serverConfigFileName)) {
55                  ClassLoader classLoader = getClass().getClassLoader();
56  
57                  String serverConfigContent = StringUtil.read(
58                      classLoader,
59                      "com/liferay/portal/tools/dependencies/server-config.wsdd");
60  
61                  FileUtil.write(_serverConfigFileName, serverConfigContent);
62              }
63  
64              if (FileUtil.exists("docroot/WEB-INF/src/")) {
65                  _portalWsdd = false;
66              }
67  
68              Document doc = SAXReaderUtil.read(new File(fileName), true);
69  
70              Element root = doc.getRootElement();
71  
72              String packagePath = root.attributeValue("package-path");
73  
74              Element portlet = root.element("portlet");
75              Element namespace = root.element("namespace");
76  
77              if (portlet != null) {
78                  _portletShortName = portlet.attributeValue("short-name");
79              }
80              else {
81                  _portletShortName = namespace.getText();
82              }
83  
84              if (_portalWsdd) {
85                  _outputPath = "src/";
86              }
87              else {
88                  _outputPath = "docroot/WEB-INF/src/";
89              }
90  
91              _outputPath +=
92                  StringUtil.replace(packagePath, ".", "/") + "/service/http";
93  
94              _packagePath = packagePath;
95  
96              List<Element> entities = root.elements("entity");
97  
98              Iterator<Element> itr = entities.iterator();
99  
100             while (itr.hasNext()) {
101                 Element entity = itr.next();
102 
103                 String entityName = entity.attributeValue("name");
104 
105                 boolean remoteService = GetterUtil.getBoolean(
106                     entity.attributeValue("remote-service"), true);
107 
108                 if (remoteService) {
109                     _createServiceWSDD(entityName);
110 
111                     WSDDMerger.merge(
112                         _outputPath + "/" + entityName + "Service_deploy.wsdd",
113                         _serverConfigFileName);
114                 }
115             }
116         }
117         catch (Exception e) {
118             e.printStackTrace();
119         }
120     }
121 
122     private void _createServiceWSDD(String entityName) throws IOException {
123         String className =
124             _packagePath + ".service.http." + entityName + "ServiceSoap";
125 
126         String serviceName = StringUtil.replace(_portletShortName, " ", "_");
127 
128         if (_portalWsdd && !_portletShortName.equals("Portal")) {
129             serviceName = "Portlet_" + serviceName;
130         }
131 
132         serviceName += ("_" + entityName + "Service");
133 
134         String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
135 
136         FileUtil.write(
137             new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
138             wsdds[0], true);
139 
140         FileUtil.write(
141             new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
142             wsdds[1], true);
143     }
144 
145     private String _serverConfigFileName;
146     private boolean _portalWsdd = true;
147     private String _portletShortName;
148     private String _outputPath;
149     private String _packagePath;
150 
151 }