1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
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.Validator;
28  import com.liferay.portal.util.DocumentUtil;
29  import com.liferay.portal.util.PrefsPropsUtil;
30  import com.liferay.portal.util.PropsUtil;
31  import com.liferay.portal.util.PropsValues;
32  import com.liferay.util.FileUtil;
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  /**
43   * <a href="BaseExplodedTomcatListener.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Olaf Fricke
46   * @author Brian Wing Shun Chan
47   *
48   */
49  public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
50  
51      public void copyContextFile(File file) throws AutoDeployException {
52          try {
53              String tomcatConfDir = PrefsPropsUtil.getString(
54                  PropsUtil.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 }