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