001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.tools;
016    
017    import com.liferay.portal.kernel.util.FileUtil;
018    import com.liferay.portal.kernel.util.PropsKeys;
019    import com.liferay.portal.kernel.util.PropsUtil;
020    import com.liferay.portal.kernel.util.StringPool;
021    import com.liferay.portal.kernel.util.StringUtil;
022    import com.liferay.portal.kernel.xml.Document;
023    import com.liferay.portal.kernel.xml.Element;
024    import com.liferay.portal.kernel.xml.SAXReaderUtil;
025    import com.liferay.portal.util.InitUtil;
026    
027    import java.io.File;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Zsigmond Rab
032     */
033    public class EARBuilder {
034    
035            public static void main(String[] args) {
036                    InitUtil.initWithSpring();
037    
038                    if (args.length == 2) {
039                            new EARBuilder(args[0], StringUtil.split(args[1]));
040                    }
041                    else {
042                            throw new IllegalArgumentException();
043                    }
044            }
045    
046            public EARBuilder(String originalApplicationXML, String[] pluginFileNames) {
047                    try {
048                            Document document = SAXReaderUtil.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(pluginFileName);
066    
067                                    contextRootElement.addText(contextRoot);
068                            }
069    
070                            FileUtil.write(
071                                    originalApplicationXML, document.formattedString(), true);
072                    }
073                    catch (Exception e) {
074                            e.printStackTrace();
075                    }
076            }
077    
078            private String _getContextRoot(String pluginFileName) {
079                    String contextRoot = pluginFileName;
080    
081                    int pos = contextRoot.lastIndexOf(".war");
082    
083                    if (pos != -1) {
084                            contextRoot = contextRoot.substring(0, pos);
085                    }
086    
087                    if (contextRoot.equals("liferay-portal")) {
088                            contextRoot = PropsUtil.get(PropsKeys.PORTAL_CTX);
089    
090                            if (contextRoot.equals(StringPool.SLASH)) {
091                                    contextRoot = StringPool.BLANK;
092                            }
093                            else if (contextRoot.startsWith(StringPool.SLASH)) {
094                                    contextRoot = contextRoot.substring(1);
095                            }
096                    }
097    
098                    return StringPool.SLASH.concat(contextRoot);
099            }
100    
101    }