1   /**
2    * Copyright (c) 2000-2009 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   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.deploy.auto.exploded.tomcat;
21  
22  import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
23  import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
24  import com.liferay.portal.kernel.log.Log;
25  import com.liferay.portal.kernel.log.LogFactoryUtil;
26  import com.liferay.portal.kernel.util.FileUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.kernel.xml.Document;
29  import com.liferay.portal.kernel.xml.Element;
30  import com.liferay.portal.kernel.xml.SAXReaderUtil;
31  import com.liferay.portal.util.PrefsPropsUtil;
32  import com.liferay.portal.util.PropsKeys;
33  import com.liferay.portal.util.PropsValues;
34  
35  import java.io.File;
36  
37  /**
38   * <a href="BaseExplodedTomcatListener.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Olaf Fricke
41   * @author Brian Wing Shun Chan
42   *
43   */
44  public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
45  
46      public void copyContextFile(File file) throws AutoDeployException {
47          try {
48              String tomcatConfDir = PrefsPropsUtil.getString(
49                  PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
50                  PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
51  
52              if (_log.isInfoEnabled()) {
53                  _log.info(
54                      "Copying file " + file.getPath() + " to " + tomcatConfDir);
55              }
56  
57              FileUtil.copyFile(
58                  file, new File(tomcatConfDir + "/" + file.getName()));
59          }
60          catch (Exception e) {
61              throw new AutoDeployException(e.getMessage());
62          }
63      }
64  
65      public File getDocBaseDir(File file, String checkXmlFile)
66          throws AutoDeployException {
67  
68          if (!isMatchingFileExtension(file)) {
69              return null;
70          }
71  
72          String docBase = null;
73  
74          try {
75              String content = FileUtil.read(file);
76  
77              Document doc = SAXReaderUtil.read(content);
78  
79              Element root = doc.getRootElement();
80  
81              docBase = root.attributeValue("docBase");
82          }
83          catch (Exception e) {
84              throw new AutoDeployException(e);
85          }
86  
87          if (Validator.isNull(docBase)) {
88              if (_log.isDebugEnabled()) {
89                  _log.debug(
90                      file.getPath() + " does not have a docBase defined");
91              }
92  
93              return null;
94          }
95  
96          File docBaseDir = new File(docBase);
97  
98          if (!docBaseDir.exists()) {
99              if (_log.isDebugEnabled()) {
100                 _log.debug(docBase + " does not exist");
101             }
102 
103             return null;
104         }
105 
106         if (!docBaseDir.isDirectory()) {
107             if (_log.isDebugEnabled()) {
108                 _log.debug(docBase + " is not a directory");
109             }
110 
111             return null;
112         }
113 
114         if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
115             if (_log.isDebugEnabled()) {
116                 _log.debug(docBase + " does not have " + checkXmlFile);
117             }
118 
119             return null;
120         }
121 
122         return docBaseDir;
123     }
124 
125     public boolean isMatchingFileExtension(File file) {
126         if (file.getName().endsWith(".xml")) {
127             if (_log.isDebugEnabled()) {
128                 _log.debug(file.getPath() + " has a matching extension");
129             }
130 
131             return true;
132         }
133         else {
134             if (_log.isDebugEnabled()) {
135                 _log.debug(
136                     file.getPath() + " does not have a matching extension");
137             }
138 
139             return false;
140         }
141     }
142 
143     private static Log _log =
144         LogFactoryUtil.getLog(BaseExplodedTomcatListener.class);
145 
146 }