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