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