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(10);
066
067 sb.append("name=" + pluginName + "\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 StringBundler sb = new StringBundler(5);
136
137 sb.append(displayName);
138 sb.append(SANDBOX_MARKER);
139 sb.append(_pluginType);
140
141 return sb.toString();
142 }
143
144 public String getPluginName(String dirName) {
145 String pluginName = dirName.substring(
146 0, dirName.length() - (_pluginType.length() + 1));
147
148 return TextFormatter.format(pluginName, TextFormatter.J);
149 }
150
151 public boolean isEnabled(File dir) {
152 if (_engineHostDir == null) {
153 return false;
154 }
155
156 String dirName = dir.getName();
157
158 if (!dirName.endsWith(StringPool.DASH.concat(_pluginType))) {
159 return false;
160 }
161
162 return true;
163 }
164
165 @Override
166 public void undeploy(File dir) throws SandboxDeployException {
167 try {
168 if (!isEnabled(dir)) {
169 return;
170 }
171
172 String dirName = dir.getName();
173
174 if (_log.isInfoEnabled()) {
175 _log.info("Undeploying " + dirName);
176 }
177
178 deleteContextXml(dir);
179 }
180 catch (Exception e) {
181 throw new SandboxDeployException(e);
182 }
183 }
184
185 protected abstract void clonePlugin(File dir, PluginPackage pluginPackage)
186 throws Exception;
187
188 protected File getEngineHostDir() {
189 if (!ServerDetector.isTomcat()) {
190 return null;
191 }
192
193 String dirName = System.getProperty("catalina.base") + "/conf";
194
195 String[] fileNames = FileUtil.find(dirName, "**/ROOT.xml", null);
196
197 if (fileNames.length == 0) {
198 if (_log.isWarnEnabled()) {
199 _log.warn("Unable to locate ROOT.xml under CATALINA_BASE/conf");
200 }
201
202 return null;
203 }
204
205 File file = new File(fileNames[0]);
206
207 return file.getParentFile();
208 }
209
210 protected abstract String getPluginType();
211
212 private static Log _log = LogFactoryUtil.getLog(BaseSandboxHandler.class);
213
214 private Deployer _deployer;
215 private File _engineHostDir;
216 private String _pluginType;
217
218 }