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