1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.deploy.auto.exploded.tomcat;
16  
17  import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
18  import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.FileUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.Validator;
24  import com.liferay.portal.kernel.xml.Document;
25  import com.liferay.portal.kernel.xml.Element;
26  import com.liferay.portal.kernel.xml.SAXReaderUtil;
27  import com.liferay.portal.util.PrefsPropsUtil;
28  import com.liferay.portal.util.PropsValues;
29  
30  import java.io.File;
31  
32  /**
33   * <a href="BaseExplodedTomcatListener.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Olaf Fricke
36   * @author Brian Wing Shun Chan
37   */
38  public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
39  
40      public void copyContextFile(File file) throws AutoDeployException {
41          try {
42              String tomcatConfDir = PrefsPropsUtil.getString(
43                  PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
44                  PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
45  
46              if (_log.isInfoEnabled()) {
47                  _log.info(
48                      "Copying file " + file.getPath() + " to " + tomcatConfDir);
49              }
50  
51              FileUtil.copyFile(
52                  file, new File(tomcatConfDir + "/" + file.getName()));
53          }
54          catch (Exception e) {
55              throw new AutoDeployException(e.getMessage());
56          }
57      }
58  
59      public File getDocBaseDir(File file, String checkXmlFile)
60          throws AutoDeployException {
61  
62          if (!isMatchingFileExtension(file)) {
63              return null;
64          }
65  
66          String docBase = null;
67  
68          try {
69              String content = FileUtil.read(file);
70  
71              Document doc = SAXReaderUtil.read(content);
72  
73              Element root = doc.getRootElement();
74  
75              docBase = root.attributeValue("docBase");
76          }
77          catch (Exception e) {
78              throw new AutoDeployException(e);
79          }
80  
81          if (Validator.isNull(docBase)) {
82              if (_log.isDebugEnabled()) {
83                  _log.debug(
84                      file.getPath() + " does not have a docBase defined");
85              }
86  
87              return null;
88          }
89  
90          File docBaseDir = new File(docBase);
91  
92          if (!docBaseDir.exists()) {
93              if (_log.isDebugEnabled()) {
94                  _log.debug(docBase + " does not exist");
95              }
96  
97              return null;
98          }
99  
100         if (!docBaseDir.isDirectory()) {
101             if (_log.isDebugEnabled()) {
102                 _log.debug(docBase + " is not a directory");
103             }
104 
105             return null;
106         }
107 
108         if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
109             if (_log.isDebugEnabled()) {
110                 _log.debug(docBase + " does not have " + checkXmlFile);
111             }
112 
113             return null;
114         }
115 
116         return docBaseDir;
117     }
118 
119     public boolean isMatchingFileExtension(File file) {
120         if (file.getName().endsWith(".xml")) {
121             if (_log.isDebugEnabled()) {
122                 _log.debug(file.getPath() + " has a matching extension");
123             }
124 
125             return true;
126         }
127         else {
128             if (_log.isDebugEnabled()) {
129                 _log.debug(
130                     file.getPath() + " does not have a matching extension");
131             }
132 
133             return false;
134         }
135     }
136 
137     private static Log _log = LogFactoryUtil.getLog(
138         BaseExplodedTomcatListener.class);
139 
140 }