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;
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  
27  import java.io.File;
28  import java.io.IOException;
29  
30  import java.util.zip.ZipFile;
31  
32  /**
33   * <a href="BaseAutoDeployListener.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Ivica Cardic
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public abstract class BaseAutoDeployListener implements AutoDeployListener {
40  
41      public boolean isHookPlugin(File file) throws AutoDeployException {
42          if ((isMatchingFile(
43                  file, "WEB-INF/liferay-plugin-package.properties")) &&
44              (file.getName().indexOf("-hook") != -1)) {
45  
46              return true;
47          }
48  
49          return false;
50      }
51  
52      public boolean isMatchingFile(File file, String checkXmlFile)
53          throws AutoDeployException {
54  
55          if (!isMatchingFileExtension(file)) {
56              return false;
57          }
58  
59          ZipFile zipFile = null;
60  
61          try {
62              zipFile = new ZipFile(file);
63  
64              if (zipFile.getEntry(checkXmlFile) == null) {
65                  if (_log.isDebugEnabled()) {
66                      _log.debug(
67                          file.getPath() + " does not have " + checkXmlFile);
68                  }
69  
70                  return false;
71              }
72              else {
73                  return true;
74              }
75          }
76          catch (IOException ioe) {
77              throw new AutoDeployException(ioe);
78          }
79          finally {
80              if (zipFile != null) {
81                  try {
82                      zipFile.close();
83                  }
84                  catch (IOException ioe) {
85                  }
86              }
87          }
88      }
89  
90      public boolean isMatchingFileExtension(File file) {
91          String fileName = file.getName().toLowerCase();
92  
93          if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
94              if (_log.isDebugEnabled()) {
95                  _log.debug(file.getPath() + " has a matching extension");
96              }
97  
98              return true;
99          }
100         else {
101             if (_log.isDebugEnabled()) {
102                 _log.debug(
103                     file.getPath() + " does not have a matching extension");
104             }
105 
106             return false;
107         }
108     }
109 
110     public boolean isThemePlugin(File file) throws AutoDeployException {
111         if (isMatchingFile(file, "WEB-INF/liferay-look-and-feel.xml")) {
112             return true;
113         }
114 
115         if ((isMatchingFile(
116                 file, "WEB-INF/liferay-plugin-package.properties")) &&
117             (file.getName().indexOf("-theme") != -1)) {
118 
119             return true;
120         }
121 
122         return false;
123     }
124 
125     public boolean isWebPlugin(File file) throws AutoDeployException {
126         if ((isMatchingFile(
127                 file, "WEB-INF/liferay-plugin-package.properties")) &&
128             (file.getName().indexOf("-web") != -1)) {
129 
130             return true;
131         }
132 
133         return false;
134     }
135 
136     private static Log _log =
137          LogFactoryUtil.getLog(BaseAutoDeployListener.class);
138 
139 }