001    /**
002     * Copyright (c) 2000-2012 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.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    /**
032     * @author Igor Spasic
033     * @author Brian Wing Shun Chan
034     */
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://www.liferay.com\n");
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            public void deploy(File dir) throws SandboxDeployException {
089                    try {
090                            if (!isEnabled(dir)) {
091                                    return;
092                            }
093    
094                            String dirName = dir.getName();
095    
096                            if (_log.isInfoEnabled()) {
097                                    _log.info("Deploying " + dirName);
098                            }
099    
100                            String pluginName = getPluginName(dirName);
101    
102                            createPluginPackageProperties(dir, pluginName);
103    
104                            PluginPackage pluginPackage = _deployer.readPluginPackage(dir);
105    
106                            clonePlugin(dir, pluginPackage);
107    
108                            String displayName = getDisplayName(dirName);
109    
110                            _deployer.processPluginPackageProperties(
111                                    dir, displayName, pluginPackage);
112    
113                            _deployer.copyJars(dir, pluginPackage);
114                            _deployer.copyProperties(dir, pluginPackage);
115                            _deployer.copyTlds(dir, pluginPackage);
116                            _deployer.copyXmls(dir, displayName, pluginPackage);
117    
118                            _deployer.updateWebXml(
119                                    new File(dir, "WEB-INF/web.xml"), dir, displayName,
120                                    pluginPackage);
121    
122                            createContextXml(dir);
123                    }
124                    catch (Exception e) {
125                            throw new SandboxDeployException(e);
126                    }
127            }
128    
129            public String getDisplayName(String dirName) {
130                    String displayName = dirName.substring(
131                            0, dirName.length() - (_pluginType.length() + 1));
132    
133                    StringBundler sb = new StringBundler(5);
134    
135                    sb.append(displayName);
136                    sb.append(SANDBOX_MARKER);
137                    sb.append(_pluginType);
138    
139                    return sb.toString();
140            }
141    
142            public String getPluginName(String dirName) {
143                    String pluginName = dirName.substring(
144                            0, dirName.length() - (_pluginType.length() + 1));
145    
146                    return TextFormatter.format(pluginName, TextFormatter.J);
147            }
148    
149            public boolean isEnabled(File dir) {
150                    if (_engineHostDir == null) {
151                            return false;
152                    }
153    
154                    String dirName = dir.getName();
155    
156                    if (!dirName.endsWith(StringPool.DASH.concat(_pluginType))) {
157                            return false;
158                    }
159    
160                    return true;
161            }
162    
163            public void undeploy(File dir) throws SandboxDeployException {
164                    try {
165                            if (!isEnabled(dir)) {
166                                    return;
167                            }
168    
169                            String dirName = dir.getName();
170    
171                            if (_log.isInfoEnabled()) {
172                                    _log.info("Undeploying " + dirName);
173                            }
174    
175                            deleteContextXml(dir);
176                    }
177                    catch (Exception e) {
178                            throw new SandboxDeployException(e);
179                    }
180            }
181    
182            protected abstract void clonePlugin(File dir, PluginPackage pluginPackage)
183                    throws Exception;
184    
185            protected File getEngineHostDir() {
186                    if (!ServerDetector.isTomcat()) {
187                            return null;
188                    }
189    
190                    String dirName = System.getenv("CATALINA_BASE") + "/conf";
191    
192                    String[] fileNames = FileUtil.find(dirName, "**/ROOT.xml", null);
193    
194                    if (fileNames.length == 0) {
195                            _log.error("Unable to locate ROOT.xml under CATALINA_BASE/conf");
196    
197                            return null;
198                    }
199    
200                    File file = new File(fileNames[0]);
201    
202                    return file.getParentFile();
203            }
204    
205            protected abstract String getPluginType();
206    
207            private static Log _log = LogFactoryUtil.getLog(BaseSandboxHandler.class);
208    
209            private Deployer _deployer;
210            private File _engineHostDir;
211            private String _pluginType;
212    
213    }