001    /**
002     * Copyright (c) 2000-2012 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.util.InitUtil;
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                    InitUtil.initWithSpring();
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                    Document document = SAXReaderUtil.read(new File(_fileName), true);
064    
065                    Element rootElement = document.getRootElement();
066    
067                    String packagePath = rootElement.attributeValue("package-path");
068    
069                    Element portletElement = rootElement.element("portlet");
070                    Element namespaceElement = rootElement.element("namespace");
071    
072                    if (portletElement != null) {
073                            _portletShortName = portletElement.attributeValue("short-name");
074                    }
075                    else {
076                            _portletShortName = namespaceElement.getText();
077                    }
078    
079                    _outputPath +=
080                            StringUtil.replace(packagePath, ".", "/") + "/service/http";
081    
082                    _packagePath = packagePath;
083    
084                    List<Element> entityElements = rootElement.elements("entity");
085    
086                    for (Element entityElement : entityElements) {
087                            String entityName = entityElement.attributeValue("name");
088    
089                            boolean remoteService = GetterUtil.getBoolean(
090                                    entityElement.attributeValue("remote-service"), true);
091    
092                            if (remoteService) {
093                                    _createServiceWSDD(entityName);
094    
095                                    WSDDMerger.merge(
096                                            _outputPath + "/" + entityName + "Service_deploy.wsdd",
097                                            _serverConfigFileName);
098                            }
099                    }
100            }
101    
102            public void setFileName(String fileName) {
103                    _fileName = fileName;
104            }
105    
106            public void setOutputPath(String outputPath) {
107                    _outputPath = outputPath;
108            }
109    
110            public void setServerConfigFileName(String serverConfigFileName) {
111                    _serverConfigFileName = serverConfigFileName;
112            }
113    
114            public void setServiceNamespace(String serviceNamespace) {
115                    _serviceNamespace = serviceNamespace;
116            }
117    
118            private void _createServiceWSDD(String entityName) throws Exception {
119                    String className =
120                            _packagePath + ".service.http." + entityName + "ServiceSoap";
121    
122                    String serviceName = StringUtil.replace(_portletShortName, " ", "_");
123    
124                    if (!_portletShortName.equals("Portal")) {
125                            serviceName = _serviceNamespace + "_" + serviceName;
126                    }
127    
128                    serviceName += ("_" + entityName + "Service");
129    
130                    String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
131    
132                    FileUtil.write(
133                            new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
134                            wsdds[0], true);
135    
136                    FileUtil.write(
137                            new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
138                            wsdds[1], true);
139            }
140    
141            private String _fileName;
142            private String _outputPath;
143            private String _packagePath;
144            private String _portletShortName;
145            private String _serverConfigFileName;
146            private String _serviceNamespace;
147    
148    }