1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.tools;
16  
17  import com.liferay.portal.kernel.util.FileUtil;
18  import com.liferay.portal.kernel.util.GetterUtil;
19  import com.liferay.portal.kernel.util.StringUtil;
20  import com.liferay.portal.kernel.xml.Document;
21  import com.liferay.portal.kernel.xml.Element;
22  import com.liferay.portal.kernel.xml.SAXReaderUtil;
23  import com.liferay.portal.util.InitUtil;
24  import com.liferay.util.ant.Java2WsddTask;
25  
26  import java.io.File;
27  import java.io.IOException;
28  
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /**
33   * <a href="WSDDBuilder.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class WSDDBuilder {
38  
39      public static void main(String[] args) {
40          InitUtil.initWithSpring();
41  
42          if (args.length == 2) {
43              new WSDDBuilder(args[0], args[1]);
44          }
45          else {
46              throw new IllegalArgumentException();
47          }
48      }
49  
50      public WSDDBuilder(String fileName, String serverConfigFileName) {
51          try {
52              _serverConfigFileName = serverConfigFileName;
53  
54              if (!FileUtil.exists(_serverConfigFileName)) {
55                  ClassLoader classLoader = getClass().getClassLoader();
56  
57                  String serverConfigContent = StringUtil.read(
58                      classLoader,
59                      "com/liferay/portal/tools/dependencies/server-config.wsdd");
60  
61                  FileUtil.write(_serverConfigFileName, serverConfigContent);
62              }
63  
64              if (FileUtil.exists("docroot/WEB-INF/src/")) {
65                  _portalWsdd = false;
66              }
67  
68              Document doc = SAXReaderUtil.read(new File(fileName), true);
69  
70              Element root = doc.getRootElement();
71  
72              String packagePath = root.attributeValue("package-path");
73  
74              Element portlet = root.element("portlet");
75              Element namespace = root.element("namespace");
76  
77              if (portlet != null) {
78                  _portletShortName = portlet.attributeValue("short-name");
79              }
80              else {
81                  _portletShortName = namespace.getText();
82              }
83  
84              if (_portalWsdd) {
85                  _outputPath = "src/";
86              }
87              else {
88                  _outputPath = "docroot/WEB-INF/src/";
89              }
90  
91              _outputPath +=
92                  StringUtil.replace(packagePath, ".", "/") + "/service/http";
93  
94              _packagePath = packagePath;
95  
96              List<Element> entities = root.elements("entity");
97  
98              Iterator<Element> itr = entities.iterator();
99  
100             while (itr.hasNext()) {
101                 Element entity = itr.next();
102 
103                 String entityName = entity.attributeValue("name");
104 
105                 boolean remoteService = GetterUtil.getBoolean(
106                     entity.attributeValue("remote-service"), true);
107 
108                 if (remoteService) {
109                     _createServiceWSDD(entityName);
110 
111                     WSDDMerger.merge(
112                         _outputPath + "/" + entityName + "Service_deploy.wsdd",
113                         _serverConfigFileName);
114                 }
115             }
116         }
117         catch (Exception e) {
118             e.printStackTrace();
119         }
120     }
121 
122     private void _createServiceWSDD(String entityName) throws IOException {
123         String className =
124             _packagePath + ".service.http." + entityName + "ServiceSoap";
125 
126         String serviceName = StringUtil.replace(_portletShortName, " ", "_");
127 
128         if (!_portalWsdd) {
129             serviceName = "Plugin_" + serviceName;
130         }
131         else {
132             if (!_portletShortName.equals("Portal")) {
133                 serviceName = "Portlet_" + serviceName;
134             }
135         }
136 
137         serviceName += ("_" + entityName + "Service");
138 
139         String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
140 
141         FileUtil.write(
142             new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
143             wsdds[0], true);
144 
145         FileUtil.write(
146             new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
147             wsdds[1], true);
148     }
149 
150     private String _serverConfigFileName;
151     private boolean _portalWsdd = true;
152     private String _portletShortName;
153     private String _outputPath;
154     private String _packagePath;
155 
156 }