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