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