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.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  /**
42   * <a href="BaseExplodedTomcatListener.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Olaf Fricke
45   * @author Brian Wing Shun Chan
46   *
47   */
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 }