001    /**
002     * Copyright (c) 2000-2013 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.UnsecureSAXReaderUtil;
023    import com.liferay.portal.tools.servicebuilder.ServiceBuilder;
024    import com.liferay.portal.util.InitUtil;
025    import com.liferay.util.ant.Java2WsddTask;
026    
027    import java.io.File;
028    
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class WSDDBuilder {
036    
037            public static void main(String[] args) throws Exception {
038                    Map<String, String> arguments = ArgumentsUtil.parseArguments(args);
039    
040                    InitUtil.initWithSpring();
041    
042                    WSDDBuilder wsddBuilder = new WSDDBuilder();
043    
044                    wsddBuilder._fileName = arguments.get("wsdd.input.file");
045                    wsddBuilder._outputPath = arguments.get("wsdd.output.path");
046                    wsddBuilder._serverConfigFileName = arguments.get(
047                            "wsdd.server.config.file");
048                    wsddBuilder._serviceNamespace = arguments.get("wsdd.service.namespace");
049    
050                    wsddBuilder.build();
051            }
052    
053            public void build() throws Exception {
054                    if (!FileUtil.exists(_serverConfigFileName)) {
055                            ClassLoader classLoader = getClass().getClassLoader();
056    
057                            String serverConfigContent = StringUtil.read(
058                                    classLoader,
059                                    "com/liferay/portal/tools/dependencies/server-config.wsdd");
060    
061                            FileUtil.write(_serverConfigFileName, serverConfigContent);
062                    }
063    
064                    String content = ServiceBuilder.getContent(_fileName);
065    
066                    Document document = UnsecureSAXReaderUtil.read(content, true);
067    
068                    Element rootElement = document.getRootElement();
069    
070                    String packagePath = rootElement.attributeValue("package-path");
071    
072                    Element portletElement = rootElement.element("portlet");
073                    Element namespaceElement = rootElement.element("namespace");
074    
075                    if (portletElement != null) {
076                            _portletShortName = portletElement.attributeValue("short-name");
077                    }
078                    else {
079                            _portletShortName = namespaceElement.getText();
080                    }
081    
082                    _outputPath +=
083                            StringUtil.replace(packagePath, ".", "/") + "/service/http";
084    
085                    _packagePath = packagePath;
086    
087                    List<Element> entityElements = rootElement.elements("entity");
088    
089                    for (Element entityElement : entityElements) {
090                            String entityName = entityElement.attributeValue("name");
091    
092                            boolean remoteService = GetterUtil.getBoolean(
093                                    entityElement.attributeValue("remote-service"), true);
094    
095                            if (remoteService) {
096                                    _createServiceWSDD(entityName);
097    
098                                    WSDDMerger.merge(
099                                            _outputPath + "/" + entityName + "Service_deploy.wsdd",
100                                            _serverConfigFileName);
101                            }
102                    }
103            }
104    
105            public void setFileName(String fileName) {
106                    _fileName = fileName;
107            }
108    
109            public void setOutputPath(String outputPath) {
110                    _outputPath = outputPath;
111            }
112    
113            public void setServerConfigFileName(String serverConfigFileName) {
114                    _serverConfigFileName = serverConfigFileName;
115            }
116    
117            public void setServiceNamespace(String serviceNamespace) {
118                    _serviceNamespace = serviceNamespace;
119            }
120    
121            private void _createServiceWSDD(String entityName) throws Exception {
122                    String className =
123                            _packagePath + ".service.http." + entityName + "ServiceSoap";
124    
125                    String serviceName = StringUtil.replace(_portletShortName, " ", "_");
126    
127                    if (!_portletShortName.equals("Portal")) {
128                            serviceName = _serviceNamespace + "_" + serviceName;
129                    }
130    
131                    serviceName += ("_" + entityName + "Service");
132    
133                    String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
134    
135                    FileUtil.write(
136                            new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
137                            wsdds[0], true);
138    
139                    FileUtil.write(
140                            new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
141                            wsdds[1], true);
142            }
143    
144            private String _fileName;
145            private String _outputPath;
146            private String _packagePath;
147            private String _portletShortName;
148            private String _serverConfigFileName;
149            private String _serviceNamespace;
150    
151    }