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) throws Exception {
102 return _instance._getResourcePath(resource);
103 }
104
105 public static void redeployJetty(String context) throws Exception {
106 String contextsDirName = System.getProperty("jetty.home") + "/contexts";
107
108 File contextXml = new File(contextsDirName + "/" + context + ".xml");
109
110 if (contextXml.exists()) {
111 FileUtils.touch(contextXml);
112 }
113 else {
114 Map<String, String> filterMap = new HashMap<String, String>();
115
116 filterMap.put("context", context);
117
118 copyDependencyXml(
119 "jetty-context-configure.xml", contextsDirName,
120 context + ".xml", filterMap, true);
121 }
122 }
123
124 public static void redeployTomcat(String context) throws Exception {
125 File webXml = new File(getAutoDeployDestDir(), "/WEB-INF/web.xml");
126
127 FileUtils.touch(webXml);
128 }
129
130 public static void undeploy(String appServerType, File deployDir)
131 throws Exception {
132
133 boolean undeployEnabled = PrefsPropsUtil.getBoolean(
134 PropsKeys.HOT_UNDEPLOY_ENABLED, PropsValues.HOT_UNDEPLOY_ENABLED);
135
136 if (!undeployEnabled) {
137 return;
138 }
139
140 if (!appServerType.equals(ServerDetector.GLASSFISH_ID) &&
141 !appServerType.equals(ServerDetector.JBOSS_ID) &&
142 !appServerType.equals(ServerDetector.JETTY_ID) &&
143 !appServerType.equals(ServerDetector.TOMCAT_ID) &&
144 !appServerType.equals(ServerDetector.WEBLOGIC_ID)) {
145
146 return;
147 }
148
149 if (!deployDir.exists()) {
150 return;
151 }
152
153 if (deployDir.isFile()) {
154 FileUtil.delete(deployDir);
155 }
156 else {
157 File webXml = new File(deployDir + "/WEB-INF/web.xml");
158
159 if (!webXml.exists()) {
160 return;
161 }
162
163 if (_log.isInfoEnabled()) {
164 _log.info("Undeploy " + deployDir);
165 }
166
167 FileUtil.delete(deployDir + "/WEB-INF/web.xml");
168
169 DeleteTask.deleteDirectory(deployDir);
170 }
171
172 if (appServerType.equals(ServerDetector.JETTY_ID)) {
173 FileUtil.delete(
174 System.getProperty("jetty.home") + "/contexts/" +
175 deployDir.getName() + ".xml");
176 }
177
178 if (appServerType.equals(ServerDetector.JBOSS_ID)) {
179 File deployedFile = new File(
180 deployDir.getParent(), deployDir.getName() + ".deployed");
181
182 FileUtil.delete(deployedFile);
183 }
184
185 int undeployInterval = PrefsPropsUtil.getInteger(
186 PropsKeys.HOT_UNDEPLOY_INTERVAL, PropsValues.HOT_UNDEPLOY_INTERVAL);
187
188 if (_log.isInfoEnabled()) {
189 _log.info(
190 "Wait " + undeployInterval +
191 " ms to allow the plugin time to fully undeploy");
192 }
193
194 if (undeployInterval > 0) {
195 Thread.sleep(undeployInterval);
196 }
197 }
198
199 private DeployUtil() {
200 }
201
202 private String _getResourcePath(String resource) throws IOException {
203 InputStream is = getClass().getResourceAsStream(
204 "dependencies/" + resource);
205
206 if (is == null) {
207 return null;
208 }
209
210 String tmpDir = SystemProperties.get(SystemProperties.TMP_DIR);
211
212 File file = new File(
213 tmpDir + "/liferay/com/liferay/portal/deploy/dependencies/" +
214 resource);
215
216
217 File parentFile = file.getParentFile();
218
219 if (parentFile != null) {
220 parentFile.mkdirs();
221 }
222
223 StreamUtil.transfer(is, new FileOutputStream(file));
224
225
226 return FileUtil.getAbsolutePath(file);
227 }
228
229 private static Log _log = LogFactoryUtil.getLog(DeployUtil.class);
230
231 private static DeployUtil _instance = new DeployUtil();
232
233 }