1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.tools;
21  
22  import com.liferay.portal.kernel.util.FileUtil;
23  import com.liferay.portal.kernel.util.GetterUtil;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.util.InitUtil;
29  import com.liferay.util.ant.Java2WsddTask;
30  
31  import java.io.File;
32  import java.io.IOException;
33  
34  import java.util.Iterator;
35  import java.util.List;
36  
37  /**
38   * <a href="WSDDBuilder.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class WSDDBuilder {
44  
45      public static void main(String[] args) {
46          InitUtil.initWithSpring();
47  
48          if (args.length == 2) {
49              new WSDDBuilder(args[0], args[1]);
50          }
51          else {
52              throw new IllegalArgumentException();
53          }
54      }
55  
56      public WSDDBuilder(String fileName, String serverConfigFileName) {
57          try {
58              _serverConfigFileName = serverConfigFileName;
59  
60              Document doc = SAXReaderUtil.read(new File(fileName), true);
61  
62              Element root = doc.getRootElement();
63  
64              String packagePath = root.attributeValue("package-path");
65  
66              Element portlet = root.element("portlet");
67              Element namespace = root.element("namespace");
68  
69              if (portlet != null) {
70                  _portletShortName = portlet.attributeValue("short-name");
71              }
72              else {
73                  _portletShortName = namespace.getText();
74              }
75  
76              _outputPath =
77                  "src/" + StringUtil.replace(packagePath, ".", "/") +
78                      "/service/http";
79  
80              _packagePath = packagePath;
81  
82              List<Element> entities = root.elements("entity");
83  
84              Iterator<Element> itr = entities.iterator();
85  
86              while (itr.hasNext()) {
87                  Element entity = itr.next();
88  
89                  String entityName = entity.attributeValue("name");
90  
91                  boolean remoteService = GetterUtil.getBoolean(
92                      entity.attributeValue("remote-service"), true);
93  
94                  if (remoteService) {
95                      _createServiceWSDD(entityName);
96  
97                      WSDDMerger.merge(
98                          _outputPath + "/" + entityName + "Service_deploy.wsdd",
99                          _serverConfigFileName);
100                 }
101             }
102         }
103         catch (Exception e) {
104             e.printStackTrace();
105         }
106     }
107 
108     private void _createServiceWSDD(String entityName) throws IOException {
109         String className =
110             _packagePath + ".service.http." + entityName + "ServiceSoap";
111 
112         String serviceName = StringUtil.replace(_portletShortName, " ", "_");
113 
114         if (!_portletShortName.equals("Portal")) {
115             serviceName = "Portlet_" + serviceName;
116         }
117 
118         serviceName += ("_" + entityName + "Service");
119 
120         String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
121 
122         FileUtil.write(
123             new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
124             wsdds[0], true);
125 
126         FileUtil.write(
127             new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
128             wsdds[1], true);
129     }
130 
131     private String _serverConfigFileName;
132     private String _portletShortName;
133     private String _outputPath;
134     private String _packagePath;
135 
136 }