001
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.CharPool;
020 import com.liferay.portal.kernel.util.FileUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.PropsKeys;
023 import com.liferay.portal.kernel.util.ServerDetector;
024 import com.liferay.portal.kernel.util.StreamUtil;
025 import com.liferay.portal.kernel.util.StringUtil;
026 import com.liferay.portal.kernel.util.SystemProperties;
027 import com.liferay.portal.kernel.util.Validator;
028 import com.liferay.portal.util.PrefsPropsUtil;
029 import com.liferay.portal.util.PropsValues;
030 import com.liferay.util.ant.CopyTask;
031 import com.liferay.util.ant.DeleteTask;
032
033 import java.io.File;
034 import java.io.FileOutputStream;
035 import java.io.IOException;
036 import java.io.InputStream;
037
038 import java.util.HashMap;
039 import java.util.Map;
040
041 import org.apache.commons.io.FileUtils;
042
043
046 public class DeployUtil {
047
048 public static void copyDependencyXml(
049 String fileName, String targetDir, String targetFileName,
050 Map<String, String> filterMap, boolean overwrite)
051 throws Exception {
052
053 File file = new File(getResourcePath(fileName));
054 File targetFile = new File(targetDir, targetFileName);
055
056 if (!targetFile.exists()) {
057 CopyTask.copyFile(
058 file, new File(targetDir), targetFileName, filterMap, overwrite,
059 true);
060 }
061 }
062
063 public static String getAutoDeployDestDir() throws Exception {
064 String destDir = PrefsPropsUtil.getString(
065 PropsKeys.AUTO_DEPLOY_DEST_DIR, PropsValues.AUTO_DEPLOY_DEST_DIR);
066
067 if (Validator.isNull(destDir)) {
068 destDir = getAutoDeployServerDestDir();
069 }
070
071 return destDir;
072 }
073
074 public static String getAutoDeployServerDestDir() throws Exception {
075 String destDir = null;
076
077 String serverId = GetterUtil.getString(ServerDetector.getServerId());
078
079 if (serverId.equals(ServerDetector.TOMCAT_ID)) {
080 destDir = PrefsPropsUtil.getString(
081 PropsKeys.AUTO_DEPLOY_TOMCAT_DEST_DIR,
082 PropsValues.AUTO_DEPLOY_TOMCAT_DEST_DIR);
083 }
084 else {
085 destDir = PrefsPropsUtil.getString(
086 "auto.deploy." + serverId + ".dest.dir");
087 }
088
089 if (Validator.isNull(destDir)) {
090 destDir = PrefsPropsUtil.getString(
091 PropsKeys.AUTO_DEPLOY_DEFAULT_DEST_DIR,
092 PropsValues.AUTO_DEPLOY_DEFAULT_DEST_DIR);
093 }
094
095 destDir = StringUtil.replace(
096 destDir, CharPool.BACK_SLASH, CharPool.SLASH);
097
098 return destDir;
099 }
100
101 public static String getResourcePath(String resource)
102 throws Exception {
103
104 return _instance._getResourcePath(resource);
105 }
106
107 public static void redeployJetty(String context) throws Exception {
108 String contextsDirName = System.getProperty("jetty.home") + "/contexts";
109
110 File contextXml = new File(contextsDirName + "/" + context + ".xml");
111
112 if (contextXml.exists()) {
113 FileUtils.touch(contextXml);
114 }
115 else {
116 Map<String, String> filterMap = new HashMap<String, String>();
117
118 filterMap.put("context", context);
119
120 copyDependencyXml(
121 "jetty-context-configure.xml", contextsDirName,
122 context + ".xml", filterMap, true);
123 }
124 }
125
126 public static void redeployTomcat(String context) throws Exception {
127 File webXml = new File(getAutoDeployDestDir(), "/WEB-INF/web.xml");
128
129 FileUtils.touch(webXml);
130 }
131
132 public static void undeploy(String appServerType, File deployDir)
133 throws Exception {
134
135 boolean undeployEnabled = PrefsPropsUtil.getBoolean(
136 PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
137
138 if (!undeployEnabled) {
139 return;
140 }
141
142 if (!appServerType.equals(ServerDetector.JBOSS_ID) &&
143 !appServerType.equals(ServerDetector.JETTY_ID) &&
144 !appServerType.equals(ServerDetector.TOMCAT_ID)) {
145
146 return;
147 }
148
149 if (!deployDir.exists()) {
150 return;
151 }
152
153 File webXml = new File(deployDir + "/WEB-INF/web.xml");
154
155 if (!webXml.exists()) {
156 return;
157 }
158
159 if (_log.isInfoEnabled()) {
160 _log.info("Undeploy " + deployDir);
161 }
162
163 FileUtil.delete(deployDir + "/WEB-INF/web.xml");
164
165 DeleteTask.deleteDirectory(deployDir);
166
167 if (appServerType.equals(ServerDetector.JETTY_ID)) {
168 FileUtil.delete(
169 System.getProperty("jetty.home") + "/contexts/" +
170 deployDir.getName() + ".xml");
171 }
172
173 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
174 File deployedFile = new File(
175 deployDir.getParent(), deployDir.getName() + ".deployed");
176
177 FileUtil.delete(deployedFile);
178 }
179
180 int undeployInterval = PrefsPropsUtil.getInteger(
181 PropsKeys.HOT_UNDEPLOY_INTERVAL,
182 PropsValues.HOT_UNDEPLOY_INTERVAL);
183
184 if (_log.isInfoEnabled()) {
185 _log.info(
186 "Wait " + undeployInterval +
187 " ms to allow the plugin time to fully undeploy");
188 }
189
190 if (undeployInterval > 0) {
191 Thread.sleep(undeployInterval);
192 }
193 }
194
195 private DeployUtil() {
196 }
197
198 private String _getResourcePath(String resource) throws IOException {
199 InputStream is = getClass().getResourceAsStream(
200 "dependencies/" + resource);
201
202 if (is == null) {
203 return null;
204 }
205
206 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
207
208 File file = new File(
209 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
210 resource);
211
212
213 File parentFile = file.getParentFile();
214
215 if (parentFile != null) {
216 parentFile.mkdirs();
217 }
218
219 StreamUtil.transfer(is, new FileOutputStream(file));
220
221
222 return FileUtil.getAbsolutePath(file);
223 }
224
225 private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
226
227 private static DeployUtil _instance = new DeployUtil();
228
229 }