1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.tools;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.util.PortalUtil;
28  import com.liferay.util.FileUtil;
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  import org.dom4j.Document;
38  import org.dom4j.Element;
39  
40  /**
41   * <a href="WSDDBuilder.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   *
45   */
46  public class WSDDBuilder {
47  
48      public static void main(String[] args) {
49          if (args.length == 2) {
50              new WSDDBuilder(args[0], args[1]);
51          }
52          else {
53              throw new IllegalArgumentException();
54          }
55      }
56  
57      public WSDDBuilder(String fileName, String serverConfigFileName) {
58          try {
59              _serverConfigFileName = serverConfigFileName;
60  
61              Document doc = PortalUtil.readDocumentFromFile(
62                  new File(fileName), true);
63  
64              Element root = doc.getRootElement();
65  
66              String packagePath = root.attributeValue("package-path");
67  
68              Element portlet = root.element("portlet");
69              Element namespace = root.element("namespace");
70  
71              if (portlet != null) {
72                  _portletShortName = portlet.attributeValue("short-name");
73              }
74              else {
75                  _portletShortName = namespace.getText();
76              }
77  
78              _outputPath =
79                  "src/" + StringUtil.replace(packagePath, ".", "/") +
80                      "/service/http";
81  
82              _packagePath = packagePath;
83  
84              List entities = root.elements("entity");
85  
86              Iterator itr = entities.iterator();
87  
88              while (itr.hasNext()) {
89                  Element entity = (Element)itr.next();
90  
91                  String entityName = entity.attributeValue("name");
92  
93                  boolean remoteService = GetterUtil.getBoolean(
94                      entity.attributeValue("remote-service"), true);
95  
96                  if (remoteService) {
97                      _createServiceWSDD(entityName);
98  
99                      WSDDMerger.merge(
100                         _outputPath + "/" + entityName + "Service_deploy.wsdd",
101                         _serverConfigFileName);
102                 }
103             }
104         }
105         catch (Exception e) {
106             e.printStackTrace();
107         }
108     }
109 
110     private void _createServiceWSDD(String entityName) throws IOException {
111         String className =
112             _packagePath + ".service.http." + entityName + "ServiceSoap";
113 
114         String serviceName = StringUtil.replace(_portletShortName, " ", "_");
115 
116         if (!_portletShortName.equals("Portal")) {
117             serviceName = "Portlet_" + serviceName;
118         }
119 
120         serviceName += ("_" + entityName + "Service");
121 
122         String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
123 
124         FileUtil.write(
125             new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
126             wsdds[0], true);
127 
128         FileUtil.write(
129             new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
130             wsdds[1], true);
131     }
132 
133     private String _serverConfigFileName;
134     private String _portletShortName;
135     private String _outputPath;
136     private String _packagePath;
137 
138 }