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.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    }