1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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.FileUtil;
26  import com.liferay.portal.kernel.util.GetterUtil;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.util.InitUtil;
32  import com.liferay.util.ant.Java2WsddTask;
33  
34  import java.io.File;
35  import java.io.IOException;
36  
37  import java.util.Iterator;
38  import java.util.List;
39  
40  /**
41   * <a href="WSDDBuilder.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author Brian Wing Shun Chan
44   */
45  public class WSDDBuilder {
46  
47      public static void main(String[] args) {
48          InitUtil.initWithSpring();
49  
50          if (args.length == 2) {
51              new WSDDBuilder(args[0], args[1]);
52          }
53          else {
54              throw new IllegalArgumentException();
55          }
56      }
57  
58      public WSDDBuilder(String fileName, String serverConfigFileName) {
59          try {
60              _serverConfigFileName = serverConfigFileName;
61  
62              Document doc = SAXReaderUtil.read(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<Element> entities = root.elements("entity");
85  
86              Iterator<Element> itr = entities.iterator();
87  
88              while (itr.hasNext()) {
89                  Element entity = 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 }