001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    import com.liferay.portal.kernel.xml.Document;
021    import com.liferay.portal.kernel.xml.Element;
022    import com.liferay.portal.kernel.xml.SAXReaderUtil;
023    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
024    import com.liferay.util.ant.Java2WsddTask;
025    
026    import java.io.File;
027    
028    import java.util.List;
029    import java.util.Map;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class WSDDBuilder {
035    
036            public static void main(String[] args) throws Exception {
037                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
038    
039                    ToolDependencies.wireBasic();
040    
041                    WSDDBuilder wsddBuilder = new WSDDBuilder();
042    
043                    wsddBuilder._fileName = arguments.get("wsdd.input.file");
044                    wsddBuilder._outputPath = arguments.get("wsdd.output.path");
045                    wsddBuilder._serverConfigFileName = arguments.get(
046                            "wsdd.server.config.file");
047                    wsddBuilder._serviceNamespace = arguments.get("wsdd.service.namespace");
048    
049                    wsddBuilder.build();
050            }
051    
052            public void build() throws Exception {
053                    if (!FileUtil.exists(_serverConfigFileName)) {
054                            ClassLoader classLoader = getClass().getClassLoader();
055    
056                            String serverConfigContent = StringUtil.read(
057                                    classLoader,
058                                    "com/liferay/portal/tools/dependencies/server-config.wsdd");
059    
060                            FileUtil.write(_serverConfigFileName, serverConfigContent);
061                    }
062    
063                    String content = ServiceBuilder.getContent(_fileName);
064    
065                    Document document = SAXReaderUtil.read(content, true);
066    
067                    Element rootElement = document.getRootElement();
068    
069                    String packagePath = rootElement.attributeValue("package-path");
070    
071                    Element portletElement = rootElement.element("portlet");
072                    Element namespaceElement = rootElement.element("namespace");
073    
074                    if (portletElement != null) {
075                            _portletShortName = portletElement.attributeValue("short-name");
076                    }
077                    else {
078                            _portletShortName = namespaceElement.getText();
079                    }
080    
081                    _outputPath +=
082                            StringUtil.replace(packagePath, ".", "/") + "/service/http";
083    
084                    _packagePath = packagePath;
085    
086                    List<Element> entityElements = rootElement.elements("entity");
087    
088                    for (Element entityElement : entityElements) {
089                            String entityName = entityElement.attributeValue("name");
090    
091                            boolean remoteService = GetterUtil.getBoolean(
092                                    entityElement.attributeValue("remote-service"), true);
093    
094                            if (remoteService) {
095                                    _createServiceWSDD(entityName);
096    
097                                    WSDDMerger.merge(
098                                            _outputPath + "/" + entityName + "Service_deploy.wsdd",
099                                            _serverConfigFileName);
100                            }
101                    }
102            }
103    
104            public void setFileName(String fileName) {
105                    _fileName = fileName;
106            }
107    
108            public void setOutputPath(String outputPath) {
109                    _outputPath = outputPath;
110            }
111    
112            public void setServerConfigFileName(String serverConfigFileName) {
113                    _serverConfigFileName = serverConfigFileName;
114            }
115    
116            public void setServiceNamespace(String serviceNamespace) {
117                    _serviceNamespace = serviceNamespace;
118            }
119    
120            private void _createServiceWSDD(String entityName) throws Exception {
121                    String className =
122                            _packagePath + ".service.http." + entityName + "ServiceSoap";
123    
124                    String serviceName = StringUtil.replace(_portletShortName, " ", "_");
125    
126                    if (!_portletShortName.equals("Portal")) {
127                            serviceName = _serviceNamespace + "_" + serviceName;
128                    }
129    
130                    serviceName += ("_" + entityName + "Service");
131    
132                    String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
133    
134                    FileUtil.write(
135                            new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
136                            wsdds[0], true);
137    
138                    FileUtil.write(
139                            new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
140                            wsdds[1], true);
141            }
142    
143            private String _fileName;
144            private String _outputPath;
145            private String _packagePath;
146            private String _portletShortName;
147            private String _serverConfigFileName;
148            private String _serviceNamespace;
149    
150    }