001
014
015 package com.liferay.portal.deploy.sandbox;
016
017 import com.liferay.portal.kernel.deploy.Deployer;
018 import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.plugin.PluginPackage;
022 import com.liferay.portal.kernel.util.FileUtil;
023 import com.liferay.portal.kernel.util.ServerDetector;
024 import com.liferay.portal.kernel.util.StringBundler;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.TextFormatter;
027
028 import java.io.File;
029 import java.io.IOException;
030
031
035 public abstract class BaseSandboxHandler implements SandboxHandler {
036
037 public BaseSandboxHandler(Deployer deployer) {
038 _deployer = deployer;
039 _engineHostDir = getEngineHostDir();
040 _pluginType = getPluginType();
041 }
042
043 public void createContextXml(File dir) throws IOException {
044 String displayName = getDisplayName(dir.getName());
045
046 File contextXml = new File(_engineHostDir, displayName + ".xml");
047
048 StringBundler sb = new StringBundler(6);
049
050 sb.append("<?xml version=\"1.0\"?>\n");
051 sb.append("<Context crossContext=\"true\" docBase=\"");
052 sb.append(dir.getAbsolutePath());
053 sb.append("\" path=\"");
054 sb.append(displayName);
055 sb.append("\" />");
056
057 FileUtil.write(contextXml, sb.toString());
058 }
059
060 public void createPluginPackageProperties(File dir, String pluginName)
061 throws IOException {
062
063 StringBundler sb = new StringBundler(12);
064
065 sb.append("name=");
066 sb.append(pluginName);
067 sb.append("\n");
068 sb.append("module-group-id=liferay\n");
069 sb.append("module-incremental-version=1\n");
070 sb.append("tags=\n");
071 sb.append("short-description=\n");
072 sb.append("change-log=\n");
073 sb.append("page-url=http:
074 sb.append("author=Liferay, Inc.\n");
075 sb.append("licenses=LGPL\n");
076 sb.append("speed-filters-enabled=false\n");
077
078 FileUtil.write(
079 dir + "/WEB-INF/liferay-plugin-package.properties", sb.toString());
080 }
081
082 public void deleteContextXml(File dir) {
083 String displayName = getDisplayName(dir.getName());
084
085 FileUtil.delete(_engineHostDir + "/" + displayName + ".xml");
086 }
087
088 @Override
089 public void deploy(File dir) throws SandboxDeployException {
090 try {
091 if (!isEnabled(dir)) {
092 return;
093 }
094
095 String dirName = dir.getName();
096
097 if (_log.isInfoEnabled()) {
098 _log.info("Deploying " + dirName);
099 }
100
101 String pluginName = getPluginName(dirName);
102
103 createPluginPackageProperties(dir, pluginName);
104
105 PluginPackage pluginPackage = _deployer.readPluginPackage(dir);
106
107 clonePlugin(dir, pluginPackage);
108
109 String displayName = getDisplayName(dirName);
110
111 _deployer.processPluginPackageProperties(
112 dir, displayName, pluginPackage);
113
114 _deployer.copyJars(dir, pluginPackage);
115 _deployer.copyProperties(dir, pluginPackage);
116 _deployer.copyTlds(dir, pluginPackage);
117 _deployer.copyXmls(dir, displayName, pluginPackage);
118
119 _deployer.updateWebXml(
120 new File(dir, "WEB-INF/web.xml"), dir, displayName,
121 pluginPackage);
122
123 createContextXml(dir);
124 }
125 catch (Exception e) {
126 throw new SandboxDeployException(e);
127 }
128 }
129
130 @Override
131 public String getDisplayName(String dirName) {
132 String displayName = dirName.substring(
133 0, dirName.length() - (_pluginType.length() + 1));
134
135 return displayName.concat(SANDBOX_MARKER).concat(_pluginType);
136 }
137
138 public String getPluginName(String dirName) {
139 String pluginName = dirName.substring(
140 0, dirName.length() - (_pluginType.length() + 1));
141
142 return TextFormatter.format(pluginName, TextFormatter.J);
143 }
144
145 public boolean isEnabled(File dir) {
146 if (_engineHostDir == null) {
147 return false;
148 }
149
150 String dirName = dir.getName();
151
152 if (!dirName.endsWith(StringPool.DASH.concat(_pluginType))) {
153 return false;
154 }
155
156 return true;
157 }
158
159 @Override
160 public void undeploy(File dir) throws SandboxDeployException {
161 try {
162 if (!isEnabled(dir)) {
163 return;
164 }
165
166 String dirName = dir.getName();
167
168 if (_log.isInfoEnabled()) {
169 _log.info("Undeploying " + dirName);
170 }
171
172 deleteContextXml(dir);
173 }
174 catch (Exception e) {
175 throw new SandboxDeployException(e);
176 }
177 }
178
179 protected abstract void clonePlugin(File dir, PluginPackage pluginPackage)
180 throws Exception;
181
182 protected File getEngineHostDir() {
183 if (!ServerDetector.isTomcat()) {
184 return null;
185 }
186
187 String dirName = System.getProperty("catalina.base") + "/conf";
188
189 String[] fileNames = FileUtil.find(dirName, "**/ROOT.xml", null);
190
191 if (fileNames.length == 0) {
192 if (_log.isWarnEnabled()) {
193 _log.warn("Unable to locate ROOT.xml under CATALINA_BASE/conf");
194 }
195
196 return null;
197 }
198
199 File file = new File(fileNames[0]);
200
201 return file.getParentFile();
202 }
203
204 protected abstract String getPluginType();
205
206 private static final Log _log = LogFactoryUtil.getLog(
207 BaseSandboxHandler.class);
208
209 private final Deployer _deployer;
210 private File _engineHostDir;
211 private final String _pluginType;
212
213 }