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