1   /**
2    * Copyright (c) 2000-2009 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.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   */
46  public class WSDDBuilder {
47  
48      public static void main(String[] args) {
49          InitUtil.initWithSpring();
50  
51          if (args.length == 2) {
52              new WSDDBuilder(args[0], args[1]);
53          }
54          else {
55              throw new IllegalArgumentException();
56          }
57      }
58  
59      public WSDDBuilder(String fileName, String serverConfigFileName) {
60          try {
61              _serverConfigFileName = serverConfigFileName;
62  
63              Document doc = SAXReaderUtil.read(new File(fileName), true);
64  
65              Element root = doc.getRootElement();
66  
67              String packagePath = root.attributeValue("package-path");
68  
69              Element portlet = root.element("portlet");
70              Element namespace = root.element("namespace");
71  
72              if (portlet != null) {
73                  _portletShortName = portlet.attributeValue("short-name");
74              }
75              else {
76                  _portletShortName = namespace.getText();
77              }
78  
79              _outputPath =
80                  "src/" + StringUtil.replace(packagePath, ".", "/") +
81                      "/service/http";
82  
83              _packagePath = packagePath;
84  
85              List<Element> entities = root.elements("entity");
86  
87              Iterator<Element> itr = entities.iterator();
88  
89              while (itr.hasNext()) {
90                  Element entity = itr.next();
91  
92                  String entityName = entity.attributeValue("name");
93  
94                  boolean remoteService = GetterUtil.getBoolean(
95                      entity.attributeValue("remote-service"), true);
96  
97                  if (remoteService) {
98                      _createServiceWSDD(entityName);
99  
100                     WSDDMerger.merge(
101                         _outputPath + "/" + entityName + "Service_deploy.wsdd",
102                         _serverConfigFileName);
103                 }
104             }
105         }
106         catch (Exception e) {
107             e.printStackTrace();
108         }
109     }
110 
111     private void _createServiceWSDD(String entityName) throws IOException {
112         String className =
113             _packagePath + ".service.http." + entityName + "ServiceSoap";
114 
115         String serviceName = StringUtil.replace(_portletShortName, " ", "_");
116 
117         if (!_portletShortName.equals("Portal")) {
118             serviceName = "Portlet_" + serviceName;
119         }
120 
121         serviceName += ("_" + entityName + "Service");
122 
123         String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
124 
125         FileUtil.write(
126             new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
127             wsdds[0], true);
128 
129         FileUtil.write(
130             new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
131             wsdds[1], true);
132     }
133 
134     private String _serverConfigFileName;
135     private String _portletShortName;
136     private String _outputPath;
137     private String _packagePath;
138 
139 }