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.StringPool;
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.portal.util.PropsValues;
025    
026    import java.io.File;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     * @author Zsigmond Rab
031     */
032    public class EARBuilder {
033    
034            public static void main(String[] args) {
035                    InitUtil.initWithSpring();
036    
037                    if (args.length == 2) {
038                            new EARBuilder(args[0], StringUtil.split(args[1]));
039                    }
040                    else {
041                            throw new IllegalArgumentException();
042                    }
043            }
044    
045            public EARBuilder(String originalApplicationXML, String[] pluginFileNames) {
046                    try {
047                            Document document = SAXReaderUtil.read(
048                                    new File(originalApplicationXML));
049    
050                            Element rootElement = document.getRootElement();
051    
052                            for (String pluginFileName : pluginFileNames) {
053                                    Element moduleElement = rootElement.addElement("module");
054    
055                                    Element webElement = moduleElement.addElement("web");
056    
057                                    Element webURIElement = webElement.addElement("web-uri");
058    
059                                    webURIElement.addText(pluginFileName);
060    
061                                    Element contextRootElement = webElement.addElement(
062                                            "context-root");
063    
064                                    String contextRoot = _getContextRoot(pluginFileName);
065    
066                                    contextRootElement.addText(contextRoot);
067                            }
068    
069                            FileUtil.write(
070                                    originalApplicationXML, document.formattedString(), true);
071                    }
072                    catch (Exception e) {
073                            e.printStackTrace();
074                    }
075            }
076    
077            private String _getContextRoot(String pluginFileName) {
078                    String contextRoot = pluginFileName;
079    
080                    int pos = contextRoot.lastIndexOf(".war");
081    
082                    if (pos != -1) {
083                            contextRoot = contextRoot.substring(0, pos);
084                    }
085    
086                    if (contextRoot.equals("liferay-portal")) {
087                            contextRoot = PropsValues.PORTAL_CTX;
088    
089                            if (contextRoot.equals(StringPool.SLASH)) {
090                                    contextRoot = StringPool.BLANK;
091                            }
092                            else if (contextRoot.startsWith(StringPool.SLASH)) {
093                                    contextRoot = contextRoot.substring(1);
094                            }
095                    }
096    
097                    return StringPool.SLASH.concat(contextRoot);
098            }
099    
100    }