1
22
23 package com.liferay.portal.deploy.auto.exploded.tomcat;
24
25 import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
26 import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
27 import com.liferay.portal.kernel.util.FileUtil;
28 import com.liferay.portal.kernel.util.Validator;
29 import com.liferay.portal.util.DocumentUtil;
30 import com.liferay.portal.util.PrefsPropsUtil;
31 import com.liferay.portal.util.PropsKeys;
32 import com.liferay.portal.util.PropsValues;
33
34 import java.io.File;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38
39 import org.dom4j.Document;
40 import org.dom4j.Element;
41
42
49 public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
50
51 public void copyContextFile(File file) throws AutoDeployException {
52 try {
53 String tomcatConfDir = PrefsPropsUtil.getString(
54 PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
55 PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
56
57 if (_log.isInfoEnabled()) {
58 _log.info(
59 "Copying file " + file.getPath() + " to " + tomcatConfDir);
60 }
61
62 FileUtil.copyFile(
63 file, new File(tomcatConfDir + "/" + file.getName()));
64 }
65 catch (Exception e) {
66 throw new AutoDeployException(e.getMessage());
67 }
68 }
69
70 public File getDocBaseDir(File file, String checkXmlFile)
71 throws AutoDeployException {
72
73 if (!isMatchingFileExtension(file)) {
74 return null;
75 }
76
77 String docBase = null;
78
79 try {
80 String content = FileUtil.read(file);
81
82 Document doc = DocumentUtil.readDocumentFromXML(content);
83
84 Element root = doc.getRootElement();
85
86 docBase = root.attributeValue("docBase");
87 }
88 catch (Exception e) {
89 throw new AutoDeployException(e);
90 }
91
92 if (Validator.isNull(docBase)) {
93 if (_log.isDebugEnabled()) {
94 _log.debug(
95 file.getPath() + " does not have a docBase defined");
96 }
97
98 return null;
99 }
100
101 File docBaseDir = new File(docBase);
102
103 if (!docBaseDir.exists()) {
104 if (_log.isDebugEnabled()) {
105 _log.debug(docBase + " does not exist");
106 }
107
108 return null;
109 }
110
111 if (!docBaseDir.isDirectory()) {
112 if (_log.isDebugEnabled()) {
113 _log.debug(docBase + " is not a directory");
114 }
115
116 return null;
117 }
118
119 if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
120 if (_log.isDebugEnabled()) {
121 _log.debug(docBase + " does not have " + checkXmlFile);
122 }
123
124 return null;
125 }
126
127 return docBaseDir;
128 }
129
130 public boolean isMatchingFileExtension(File file) {
131 if (file.getName().endsWith(".xml")) {
132 if (_log.isDebugEnabled()) {
133 _log.debug(file.getPath() + " has a matching extension");
134 }
135
136 return true;
137 }
138 else {
139 if (_log.isDebugEnabled()) {
140 _log.debug(
141 file.getPath() + " does not have a matching extension");
142 }
143
144 return false;
145 }
146 }
147
148 private static Log _log =
149 LogFactory.getLog(BaseExplodedTomcatListener.class);
150
151 }