1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.deploy.auto.exploded.tomcat;
16  
17  import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
18  import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.ServerDetector;
24  import com.liferay.portal.kernel.util.Validator;
25  import com.liferay.portal.kernel.xml.Document;
26  import com.liferay.portal.kernel.xml.Element;
27  import com.liferay.portal.kernel.xml.SAXReaderUtil;
28  import com.liferay.portal.util.PrefsPropsUtil;
29  import com.liferay.portal.util.PropsValues;
30  
31  import java.io.File;
32  
33  /**
34   * <a href="BaseExplodedTomcatListener.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Olaf Fricke
37   * @author Brian Wing Shun Chan
38   */
39  public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
40  
41      public void copyContextFile(File file) throws AutoDeployException {
42          try {
43              if (ServerDetector.isGlassfish()) {
44                  return;
45              }
46  
47              String tomcatConfDir = PrefsPropsUtil.getString(
48                  PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
49                  PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
50  
51              if (_log.isInfoEnabled()) {
52                  _log.info(
53                      "Copying file " + file.getPath() + " to " + tomcatConfDir);
54              }
55  
56              FileUtil.copyFile(
57                  file, new File(tomcatConfDir + "/" + file.getName()));
58          }
59          catch (Exception e) {
60              throw new AutoDeployException(e.getMessage());
61          }
62      }
63  
64      public File getDocBaseDir(File file, String checkXmlFile)
65          throws AutoDeployException {
66  
67          if (!isMatchingFileExtension(file)) {
68              return null;
69          }
70  
71          String docBase = null;
72  
73          try {
74              String content = FileUtil.read(file);
75  
76              Document document = SAXReaderUtil.read(content);
77  
78              Element rootElement = document.getRootElement();
79  
80              docBase = rootElement.attributeValue("docBase");
81          }
82          catch (Exception e) {
83              throw new AutoDeployException(e);
84          }
85  
86          if (Validator.isNull(docBase)) {
87              if (_log.isDebugEnabled()) {
88                  _log.debug(
89                      file.getPath() + " does not have a docBase defined");
90              }
91  
92              return null;
93          }
94  
95          File docBaseDir = new File(docBase);
96  
97          if (!docBaseDir.exists()) {
98              if (_log.isDebugEnabled()) {
99                  _log.debug(docBase + " does not exist");
100             }
101 
102             return null;
103         }
104 
105         if (!docBaseDir.isDirectory()) {
106             if (_log.isDebugEnabled()) {
107                 _log.debug(docBase + " is not a directory");
108             }
109 
110             return null;
111         }
112 
113         if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
114             if (_log.isDebugEnabled()) {
115                 _log.debug(docBase + " does not have " + checkXmlFile);
116             }
117 
118             return null;
119         }
120 
121         return docBaseDir;
122     }
123 
124     public boolean isMatchingFileExtension(File file) {
125         if (file.getName().endsWith(".xml")) {
126             if (_log.isDebugEnabled()) {
127                 _log.debug(file.getPath() + " has a matching extension");
128             }
129 
130             return true;
131         }
132         else {
133             if (_log.isDebugEnabled()) {
134                 _log.debug(
135                     file.getPath() + " does not have a matching extension");
136             }
137 
138             return false;
139         }
140     }
141 
142     private static Log _log = LogFactoryUtil.getLog(
143         BaseExplodedTomcatListener.class);
144 
145 }