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