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