001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.deploy.auto;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.StringUtil;
020    
021    import java.io.File;
022    import java.io.IOException;
023    
024    import java.util.regex.Matcher;
025    import java.util.regex.Pattern;
026    import java.util.zip.ZipFile;
027    
028    /**
029     * @author Ivica Cardic
030     * @author Brian Wing Shun Chan
031     * @author Ryan Park
032     */
033    public abstract class BaseAutoDeployListener implements AutoDeployListener {
034    
035            public boolean isExtPlugin(File file) {
036                    Matcher matcher = _extPluginPattern.matcher(file.getName());
037    
038                    return matcher.find();
039            }
040    
041            public boolean isHookPlugin(File file) throws AutoDeployException {
042                    Matcher matcher = _hookPluginPattern.matcher(file.getName());
043    
044                    if (matcher.find() &&
045                            isMatchingFile(file, "WEB-INF/liferay-hook.xml", false) &&
046                            !isMatchingFile(file, "WEB-INF/liferay-portlet.xml", false)) {
047    
048                            return true;
049                    }
050    
051                    return false;
052            }
053    
054            public boolean isLayoutTemplatePlugin(File file)
055                    throws AutoDeployException {
056    
057                    if (isMatchingFile(file, "WEB-INF/liferay-layout-templates.xml") &&
058                            !isThemePlugin(file)) {
059    
060                            return true;
061                    }
062    
063                    return false;
064            }
065    
066            public boolean isLiferayPackage(File file) {
067                    String fileName = file.getName();
068    
069                    if (fileName.endsWith(".lpkg")) {
070                            return true;
071                    }
072    
073                    return false;
074            }
075    
076            public boolean isMatchingFile(File file, String checkXmlFile)
077                    throws AutoDeployException {
078    
079                    return isMatchingFile(file, checkXmlFile, true);
080            }
081    
082            public boolean isMatchingFile(
083                            File file, String checkXmlFile, boolean checkFileExtension)
084                    throws AutoDeployException {
085    
086                    if (checkFileExtension && !isMatchingFileExtension(file)) {
087                            return false;
088                    }
089    
090                    ZipFile zipFile = null;
091    
092                    try {
093                            zipFile = new ZipFile(file);
094    
095                            if (zipFile.getEntry(checkXmlFile) == null) {
096                                    if (_log.isDebugEnabled()) {
097                                            _log.debug(
098                                                    file.getPath() + " does not have " + checkXmlFile);
099                                    }
100    
101                                    return false;
102                            }
103                            else {
104                                    return true;
105                            }
106                    }
107                    catch (IOException ioe) {
108                            throw new AutoDeployException(ioe);
109                    }
110                    finally {
111                            if (zipFile != null) {
112                                    try {
113                                            zipFile.close();
114                                    }
115                                    catch (IOException ioe) {
116                                    }
117                            }
118                    }
119            }
120    
121            public boolean isMatchingFileExtension(File file) {
122                    return isMatchingFileExtension(file, ".war", ".zip");
123            }
124    
125            public boolean isMatchingFileExtension(File file, String ... extensions) {
126                    String fileName = file.getName();
127    
128                    fileName = StringUtil.toLowerCase(fileName);
129    
130                    for (String extension : extensions) {
131                            if (fileName.endsWith(extension)) {
132                                    if (_log.isDebugEnabled()) {
133                                            _log.debug(file.getPath() + " has a matching extension");
134                                    }
135    
136                                    return true;
137                            }
138                    }
139    
140                    if (_log.isDebugEnabled()) {
141                            _log.debug(file.getPath() + " does not have a matching extension");
142                    }
143    
144                    return false;
145            }
146    
147            public boolean isThemePlugin(File file) throws AutoDeployException {
148                    if (isMatchingFile(file, "WEB-INF/liferay-look-and-feel.xml")) {
149                            return true;
150                    }
151    
152                    Matcher matcher = _themePluginPattern.matcher(file.getName());
153    
154                    if (matcher.find() &&
155                            isMatchingFile(
156                                    file, "WEB-INF/liferay-plugin-package.properties", false)) {
157    
158                            return true;
159                    }
160    
161                    return false;
162            }
163    
164            public boolean isWebPlugin(File file) throws AutoDeployException {
165                    Matcher matcher = _webPluginPattern.matcher(file.getName());
166    
167                    if (matcher.find() &&
168                            isMatchingFile(
169                                    file, "WEB-INF/liferay-plugin-package.properties", false)) {
170    
171                            return true;
172                    }
173    
174                    return false;
175            }
176    
177            protected boolean isJarFile(File file) {
178                    return isMatchingFileExtension(file, ".jar");
179            }
180    
181            private static final Log _log = LogFactoryUtil.getLog(
182                    BaseAutoDeployListener.class);
183    
184            private static final Pattern _extPluginPattern = Pattern.compile(
185                    "-(E|e)xt[-0-9.]*\\+?\\.(war|zip)$");
186            private static final Pattern _hookPluginPattern = Pattern.compile(
187                    "-(H|h)ook[-0-9.]*\\+?\\.(war|zip)$");
188            private static final Pattern _themePluginPattern = Pattern.compile(
189                    "-(T|t)heme[-0-9.]*\\+?\\.(war|zip)$");
190            private static final Pattern _webPluginPattern = Pattern.compile(
191                    "-(W|w)eb[-0-9.]*\\+?\\.(war|zip)$");
192    
193    }