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