001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.deploy.sandbox;
016    
017    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployException;
018    import com.liferay.portal.kernel.deploy.sandbox.SandboxDeployListener;
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.CharPool;
023    import com.liferay.portal.kernel.util.FileUtil;
024    import com.liferay.portal.kernel.util.ServerDetector;
025    import com.liferay.portal.kernel.util.StringBundler;
026    import com.liferay.portal.kernel.util.StringPool;
027    import com.liferay.portal.kernel.util.StringUtil;
028    import com.liferay.portal.tools.deploy.ThemeDeployer;
029    import com.liferay.portal.util.PortalUtil;
030    import com.liferay.util.ant.CopyTask;
031    
032    import java.io.File;
033    import java.io.IOException;
034    
035    import java.util.ArrayList;
036    
037    /**
038     * @author Igor Spasic
039     * @author Brian Wing Shun Chan
040     */
041    public class ThemeSandboxDeployListener
042            extends ThemeDeployer implements SandboxDeployListener {
043    
044            public ThemeSandboxDeployListener() {
045                    if (!ServerDetector.isTomcat()) {
046                            return;
047                    }
048    
049                    _engineHostDir = _getEngineHostDir();
050    
051                    if (_engineHostDir == null) {
052                            return;
053                    }
054    
055                    appServerType = ServerDetector.getServerId();
056    
057                    String portalWebDir = PortalUtil.getPortalWebDir();
058    
059                    themeTaglibDTD = portalWebDir + "/WEB-INF/tld/liferay-theme.tld";
060                    uiTaglibDTD = portalWebDir + "/WEB-INF/tld/liferay-util.tld";
061    
062                    jars = new ArrayList<String>();
063    
064                    String portalLibDir = PortalUtil.getPortalLibDir();
065    
066                    jars.add(portalLibDir + "/commons-logging.jar");
067                    jars.add(portalLibDir + "/log4j.jar");
068                    jars.add(portalLibDir + "/util-java.jar");
069                    jars.add(portalLibDir + "/util-taglib.jar");
070            }
071    
072            public void deploy(File dir) throws SandboxDeployException {
073                    try {
074                            if (!_isEnabled(dir)) {
075                                    return;
076                            }
077    
078                            String dirName = dir.getName();
079    
080                            if (_log.isInfoEnabled()) {
081                                    _log.info("Deploying " + dirName);
082                            }
083    
084                            _copyTheme(dir);
085    
086                            String themeName = _createThemeName(dirName);
087    
088                            _createPluginPackageProperties(dir, themeName);
089    
090                            PluginPackage pluginPackage = readPluginPackage(dir);
091    
092                            String displayName = _createDisplayName(dirName);
093    
094                            processPluginPackageProperties(dir, displayName, pluginPackage);
095    
096                            copyJars(dir, pluginPackage);
097                            copyProperties(dir, pluginPackage);
098                            copyTlds(dir, pluginPackage);
099                            copyXmls(dir, displayName, pluginPackage);
100    
101                            updateWebXml(
102                                    new File(dir, "WEB-INF/web.xml"), dir, displayName,
103                                    pluginPackage);
104    
105                            _createContextXml(dir);
106                    }
107                    catch (Exception e) {
108                            throw new SandboxDeployException(e);
109                    }
110            }
111    
112            public void undeploy(File dir) throws SandboxDeployException {
113                    try {
114                            if (!_isEnabled(dir)) {
115                                    return;
116                            }
117    
118                            String dirName = dir.getName();
119    
120                            if (_log.isInfoEnabled()) {
121                                    _log.info("Undeploying " + dirName);
122                            }
123    
124                            _deleteContextXml(dir);
125                    }
126                    catch (Exception e) {
127                            throw new SandboxDeployException(e);
128                    }
129            }
130    
131            protected String getDisplayName(File srcFile) {
132                    String displayName = super.getDisplayName(srcFile);
133    
134                    return _createDisplayName(displayName);
135            }
136    
137            private void _copyTheme(File dir) {
138                    String portalWebDir = PortalUtil.getPortalWebDir();
139    
140                    CopyTask.copyDirectory(
141                            new File(portalWebDir, "html/themes/classic"), dir, null,
142                            "/_diffs/**", true, true);
143            }
144    
145            private void _createContextXml(File dir) throws IOException {
146                    String displayName = _createDisplayName(dir.getName());
147    
148                    File contextXml = new File(_engineHostDir, displayName + ".xml");
149    
150                    StringBundler sb = new StringBundler();
151    
152                    sb.append("<?xml version=\"1.0\"?>\n");
153    
154                    sb.append("<Context crossContext=\"true\" docBase=\"");
155                    sb.append(dir.getAbsolutePath());
156                    sb.append("\" ");
157                    sb.append("path=\"");
158                    sb.append(displayName);
159                    sb.append("\" />");
160    
161                    FileUtil.write(contextXml, sb.toString());
162            }
163    
164            private String _createDisplayName(String dirName) {
165                    String displayName = dirName.substring(0, dirName.length() - 6);
166    
167                    StringBundler sb = new StringBundler(4);
168    
169                    sb.append(displayName);
170                    sb.append(CharPool.DASH);
171                    sb.append(SANDBOX_MARKER);
172                    sb.append("-theme");
173    
174                    return sb.toString();
175            }
176    
177            private void _createPluginPackageProperties(File dir, String themeName)
178                    throws IOException {
179    
180                    StringBundler sb = new StringBundler(10);
181    
182                    sb.append("name=" + themeName + "\n");
183                    sb.append("module-group-id=liferay\n");
184                    sb.append("module-incremental-version=1\n");
185                    sb.append("tags=\n");
186                    sb.append("short-description=\n");
187                    sb.append("change-log=\n");
188                    sb.append("page-url=http://www.liferay.com\n");
189                    sb.append("author=Liferay, Inc.\n");
190                    sb.append("licenses=LGPL\n");
191                    sb.append("speed-filters-enabled=false\n");
192    
193                    FileUtil.write(
194                            dir + "/WEB-INF/liferay-plugin-package.properties", sb.toString());
195            }
196    
197            private String _createThemeName(String dirName) {
198                    String themeName = dirName.substring(0, dirName.length() - 6);
199    
200                    themeName = StringUtil.replace(
201                            themeName, new String[] {StringPool.UNDERLINE, StringPool.MINUS},
202                            new String[] {StringPool.SPACE, StringPool.SPACE});
203    
204                    return StringUtil.upperCaseFirstLetter(themeName);
205            }
206    
207            private void _deleteContextXml(File dir) {
208                    String displayName = _createDisplayName(dir.getName());
209    
210                    FileUtil.delete(_engineHostDir + "/" + displayName + ".xml");
211            }
212    
213            private File _getEngineHostDir() {
214                    String dirName = System.getenv("CATALINA_BASE") + "/conf";
215    
216                    String[] fileNames = FileUtil.find(dirName, "**/ROOT.xml", null);
217    
218                    if (fileNames.length == 0) {
219                            _log.error("Unable to locate ROOT.xml under CATALINA_BASE/conf");
220    
221                            return null;
222                    }
223    
224                    File file = new File(dirName, fileNames[0]);
225    
226                    return file.getParentFile();
227            }
228    
229            private boolean _isEnabled(File dir) {
230                    if (_engineHostDir == null) {
231                            return false;
232                    }
233    
234                    String dirName = dir.getName();
235    
236                    if (!dirName.endsWith("-theme")) {
237                            return false;
238                    }
239    
240                    return true;
241            }
242    
243            private static Log _log = LogFactoryUtil.getLog(
244                    ThemeSandboxDeployListener.class);
245    
246            private File _engineHostDir;
247    
248    }