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.GLASSFISH_ID) &&
143 !appServerType.equals(ServerDetector.JBOSS_ID) &&
144 !appServerType.equals(ServerDetector.JETTY_ID) &&
145 !appServerType.equals(ServerDetector.TOMCAT_ID)) {
146
147 return;
148 }
149
150 if (!deployDir.exists()) {
151 return;
152 }
153
154 if (deployDir.isFile()) {
155 FileUtil.delete(deployDir);
156 }
157 else {
158 File webXml = new File(deployDir + "/WEB-INF/web.xml");
159
160 if (!webXml.exists()) {
161 return;
162 }
163
164 if (_log.isInfoEnabled()) {
165 _log.info("Undeploy " + deployDir);
166 }
167
168 FileUtil.delete(deployDir + "/WEB-INF/web.xml");
169
170 DeleteTask.deleteDirectory(deployDir);
171 }
172
173 if (appServerType.equals(ServerDetector.JETTY_ID)) {
174 FileUtil.delete(
175 System.getProperty("jetty.home") + "/contexts/" +
176 deployDir.getName() + ".xml");
177 }
178
179 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
180 File deployedFile = new File(
181 deployDir.getParent(), deployDir.getName() + ".deployed");
182
183 FileUtil.delete(deployedFile);
184 }
185
186 int undeployInterval = PrefsPropsUtil.getInteger(
187 PropsKeys.HOT_UNDEPLOY_INTERVAL, PropsValues.HOT_UNDEPLOY_INTERVAL);
188
189 if (_log.isInfoEnabled()) {
190 _log.info(
191 "Wait " + undeployInterval +
192 " ms to allow the plugin time to fully undeploy");
193 }
194
195 if (undeployInterval > 0) {
196 Thread.sleep(undeployInterval);
197 }
198 }
199
200 private DeployUtil() {
201 }
202
203 private String _getResourcePath(String resource) throws IOException {
204 InputStream is = getClass().getResourceAsStream(
205 "dependencies/" + resource);
206
207 if (is == null) {
208 return null;
209 }
210
211 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
212
213 File file = new File(
214 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
215 resource);
216
217
218 File parentFile = file.getParentFile();
219
220 if (parentFile != null) {
221 parentFile.mkdirs();
222 }
223
224 StreamUtil.transfer(is, new FileOutputStream(file));
225
226
227 return FileUtil.getAbsolutePath(file);
228 }
229
230 private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
231
232 private static DeployUtil _instance = new DeployUtil();
233
234 }