001    /**
002     * Copyright (c) 2000-2010 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;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.FileUtil;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.ServerDetector;
023    import com.liferay.portal.kernel.util.StreamUtil;
024    import com.liferay.portal.kernel.util.StringPool;
025    import com.liferay.portal.kernel.util.StringUtil;
026    import com.liferay.portal.kernel.util.Validator;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    import com.liferay.portal.util.PropsValues;
029    import com.liferay.util.SystemProperties;
030    import com.liferay.util.ant.DeleteTask;
031    
032    import java.io.File;
033    import java.io.FileOutputStream;
034    import java.io.IOException;
035    import java.io.InputStream;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class DeployUtil {
041    
042            public static String getAutoDeployDestDir() throws Exception {
043                    String destDir = PrefsPropsUtil.getString(
044                            PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
045    
046                    if (Validator.isNull(destDir)) {
047                            destDir = getAutoDeployServerDestDir();
048                    }
049    
050                    return destDir;
051            }
052    
053            public static String getAutoDeployServerDestDir() throws Exception {
054                    String destDir = null;
055    
056                    String serverId = GetterUtil.getString(ServerDetector.getServerId());
057    
058                    if (serverId.equals(ServerDetector.TOMCAT_ID)) {
059                            destDir = PrefsPropsUtil.getString(
060                                    PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
061                                    PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
062                    }
063                    else {
064                            destDir = PrefsPropsUtil.getString(
065                                    "auto.deploy." + serverId + ".dest.dir");
066                    }
067    
068                    if (Validator.isNull(destDir)) {
069                            destDir = PrefsPropsUtil.getString(
070                                    PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
071                                    PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
072                    }
073    
074                    destDir = StringUtil.replace(
075                            destDir, StringPool.BACK_SLASH, StringPool.SLASH);
076    
077                    return destDir;
078            }
079    
080            public static String getResourcePath(String resource)
081                    throws Exception {
082    
083                    return _instance._getResourcePath(resource);
084            }
085    
086            public static void undeploy(String appServerType, File deployDir)
087                    throws Exception {
088    
089                    boolean undeployEnabled = PrefsPropsUtil.getBoolean(
090                            PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
091    
092                    if (!undeployEnabled) {
093                            return;
094                    }
095    
096                    if (!appServerType.startsWith(ServerDetector.JBOSS_ID) &&
097                            !appServerType.equals(ServerDetector.TOMCAT_ID)) {
098    
099                            return;
100                    }
101    
102                    File webXml = new File(deployDir + "/WEB-INF/web.xml");
103    
104                    if (!webXml.exists()) {
105                            return;
106                    }
107    
108                    if (_log.isInfoEnabled()) {
109                            _log.info("Undeploy " + deployDir);
110                    }
111    
112                    FileUtil.delete(deployDir + "/WEB-INF/web.xml");
113    
114                    DeleteTask.deleteDirectory(deployDir);
115    
116                    int undeployInterval = PrefsPropsUtil.getInteger(
117                            PropsKeys.HOT_UNDEPLOY_INTERVAL,
118                            PropsValues.HOT_UNDEPLOY_INTERVAL);
119    
120                    if (_log.isInfoEnabled()) {
121                            _log.info(
122                                    "Wait " + undeployInterval +
123                                            " ms to allow the plugin time to fully undeploy");
124                    }
125    
126                    if (undeployInterval > 0) {
127                            Thread.sleep(undeployInterval);
128                    }
129            }
130    
131            private DeployUtil() {
132            }
133    
134            private String _getResourcePath(String resource) throws IOException {
135                    InputStream is = getClass().getResourceAsStream(
136                            "dependencies/" + resource);
137    
138                    if (is == null) {
139                            return null;
140                    }
141    
142                    String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
143    
144                    File file = new File(
145                            tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
146                                    resource);
147    
148                    //if (!file.exists() || resource.startsWith("ext-")) {
149                            File parentFile = file.getParentFile();
150    
151                            if (parentFile != null) {
152                                    parentFile.mkdirs();
153                            }
154    
155                            StreamUtil.transfer(is, new FileOutputStream(file));
156                    //}
157    
158                    return FileUtil.getAbsolutePath(file);
159            }
160    
161            private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
162    
163            private static DeployUtil _instance = new DeployUtil();
164    
165    }