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