001    /**
002     * Copyright (c) 2000-2010 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    import java.io.IOException;
028    
029    import java.util.Iterator;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     */
035    public class WSDDBuilder {
036    
037            public static void main(String[] args) {
038                    InitUtil.initWithSpring();
039    
040                    if (args.length == 2) {
041                            new WSDDBuilder(args[0], args[1]);
042                    }
043                    else {
044                            throw new IllegalArgumentException();
045                    }
046            }
047    
048            public WSDDBuilder(String fileName, String serverConfigFileName) {
049                    try {
050                            _serverConfigFileName = serverConfigFileName;
051    
052                            if (!FileUtil.exists(_serverConfigFileName)) {
053                                    ClassLoader classLoader = getClass().getClassLoader();
054    
055                                    String serverConfigContent = StringUtil.read(
056                                            classLoader,
057                                            "com/liferay/portal/tools/dependencies/server-config.wsdd");
058    
059                                    FileUtil.write(_serverConfigFileName, serverConfigContent);
060                            }
061    
062                            if (FileUtil.exists("docroot/WEB-INF/src/")) {
063                                    _portalWsdd = false;
064                            }
065    
066                            Document doc = SAXReaderUtil.read(new File(fileName), true);
067    
068                            Element root = doc.getRootElement();
069    
070                            String packagePath = root.attributeValue("package-path");
071    
072                            Element portlet = root.element("portlet");
073                            Element namespace = root.element("namespace");
074    
075                            if (portlet != null) {
076                                    _portletShortName = portlet.attributeValue("short-name");
077                            }
078                            else {
079                                    _portletShortName = namespace.getText();
080                            }
081    
082                            if (_portalWsdd) {
083                                    _outputPath = "src/";
084                            }
085                            else {
086                                    _outputPath = "docroot/WEB-INF/src/";
087                            }
088    
089                            _outputPath +=
090                                    StringUtil.replace(packagePath, ".", "/") + "/service/http";
091    
092                            _packagePath = packagePath;
093    
094                            List<Element> entities = root.elements("entity");
095    
096                            Iterator<Element> itr = entities.iterator();
097    
098                            while (itr.hasNext()) {
099                                    Element entity = itr.next();
100    
101                                    String entityName = entity.attributeValue("name");
102    
103                                    boolean remoteService = GetterUtil.getBoolean(
104                                            entity.attributeValue("remote-service"), true);
105    
106                                    if (remoteService) {
107                                            _createServiceWSDD(entityName);
108    
109                                            WSDDMerger.merge(
110                                                    _outputPath + "/" + entityName + "Service_deploy.wsdd",
111                                                    _serverConfigFileName);
112                                    }
113                            }
114                    }
115                    catch (Exception e) {
116                            e.printStackTrace();
117                    }
118            }
119    
120            private void _createServiceWSDD(String entityName) throws IOException {
121                    String className =
122                            _packagePath + ".service.http." + entityName + "ServiceSoap";
123    
124                    String serviceName = StringUtil.replace(_portletShortName, " ", "_");
125    
126                    if (!_portalWsdd) {
127                            serviceName = "Plugin_" + serviceName;
128                    }
129                    else {
130                            if (!_portletShortName.equals("Portal")) {
131                                    serviceName = "Portlet_" + serviceName;
132                            }
133                    }
134    
135                    serviceName += ("_" + entityName + "Service");
136    
137                    String[] wsdds = Java2WsddTask.generateWsdd(className, serviceName);
138    
139                    FileUtil.write(
140                            new File(_outputPath + "/" + entityName + "Service_deploy.wsdd"),
141                            wsdds[0], true);
142    
143                    FileUtil.write(
144                            new File(_outputPath + "/" + entityName + "Service_undeploy.wsdd"),
145                            wsdds[1], true);
146            }
147    
148            private String _serverConfigFileName;
149            private boolean _portalWsdd = true;
150            private String _portletShortName;
151            private String _outputPath;
152            private String _packagePath;
153    
154    }