1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
47  
48      public void copyContextFile(File file) throws AutoDeployException {
49          try {
50              String tomcatConfDir = PrefsPropsUtil.getString(
51                  PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
52                  PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
53  
54              if (_log.isInfoEnabled()) {
55                  _log.info(
56                      "Copying file " + file.getPath() + " to " + tomcatConfDir);
57              }
58  
59              FileUtil.copyFile(
60                  file, new File(tomcatConfDir + "/" + file.getName()));
61          }
62          catch (Exception e) {
63              throw new AutoDeployException(e.getMessage());
64          }
65      }
66  
67      public File getDocBaseDir(File file, String checkXmlFile)
68          throws AutoDeployException {
69  
70          if (!isMatchingFileExtension(file)) {
71              return null;
72          }
73  
74          String docBase = null;
75  
76          try {
77              String content = FileUtil.read(file);
78  
79              Document doc = SAXReaderUtil.read(content);
80  
81              Element root = doc.getRootElement();
82  
83              docBase = root.attributeValue("docBase");
84          }
85          catch (Exception e) {
86              throw new AutoDeployException(e);
87          }
88  
89          if (Validator.isNull(docBase)) {
90              if (_log.isDebugEnabled()) {
91                  _log.debug(
92                      file.getPath() + " does not have a docBase defined");
93              }
94  
95              return null;
96          }
97  
98          File docBaseDir = new File(docBase);
99  
100         if (!docBaseDir.exists()) {
101             if (_log.isDebugEnabled()) {
102                 _log.debug(docBase + " does not exist");
103             }
104 
105             return null;
106         }
107 
108         if (!docBaseDir.isDirectory()) {
109             if (_log.isDebugEnabled()) {
110                 _log.debug(docBase + " is not a directory");
111             }
112 
113             return null;
114         }
115 
116         if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
117             if (_log.isDebugEnabled()) {
118                 _log.debug(docBase + " does not have " + checkXmlFile);
119             }
120 
121             return null;
122         }
123 
124         return docBaseDir;
125     }
126 
127     public boolean isMatchingFileExtension(File file) {
128         if (file.getName().endsWith(".xml")) {
129             if (_log.isDebugEnabled()) {
130                 _log.debug(file.getPath() + " has a matching extension");
131             }
132 
133             return true;
134         }
135         else {
136             if (_log.isDebugEnabled()) {
137                 _log.debug(
138                     file.getPath() + " does not have a matching extension");
139             }
140 
141             return false;
142         }
143     }
144 
145     private static Log _log =
146         LogFactoryUtil.getLog(BaseExplodedTomcatListener.class);
147 
148 }