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.deploy.auto;
016    
017    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.util.StringUtil;
021    
022    import java.io.File;
023    import java.io.IOException;
024    
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    import java.util.zip.ZipFile;
028    
029    /**
030     * @author Manuel de la Peña
031     */
032    public class PluginAutoDeployListenerHelper {
033    
034            public PluginAutoDeployListenerHelper(File file) {
035                    _file = file;
036            }
037    
038            public boolean isExtPlugin() {
039                    Matcher matcher = _extPluginPattern.matcher(_file.getName());
040    
041                    return matcher.find();
042            }
043    
044            public boolean isHookPlugin() throws AutoDeployException {
045                    Matcher matcher = _hookPluginPattern.matcher(_file.getName());
046    
047                    if (matcher.find() &&
048                            isMatchingFile("WEB-INF/liferay-hook.xml", false) &&
049                            !isMatchingFile("WEB-INF/liferay-portlet.xml", false)) {
050    
051                            return true;
052                    }
053    
054                    return false;
055            }
056    
057            public boolean isLayoutTemplatePlugin() throws AutoDeployException {
058                    if (isMatchingFile("WEB-INF/liferay-layout-templates.xml") &&
059                            !isThemePlugin()) {
060    
061                            return true;
062                    }
063    
064                    return false;
065            }
066    
067            public boolean isLiferayPackage() {
068                    String fileName = _file.getName();
069    
070                    if (fileName.endsWith(".lpkg")) {
071                            return true;
072                    }
073    
074                    return false;
075            }
076    
077            public boolean isMatchingFile(String checkXmlFile)
078                    throws AutoDeployException {
079    
080                    return isMatchingFile(checkXmlFile, true);
081            }
082    
083            public boolean isMatchingFile(
084                            String checkXmlFile, boolean checkFileExtension)
085                    throws AutoDeployException {
086    
087                    if (checkFileExtension && !isWarOrZip()) {
088                            return false;
089                    }
090    
091                    ZipFile zipFile = null;
092    
093                    try {
094                            zipFile = new ZipFile(_file);
095    
096                            if (zipFile.getEntry(checkXmlFile) == null) {
097                                    if (_log.isDebugEnabled()) {
098                                            _log.debug(
099                                                    _file.getPath() + " does not have " + checkXmlFile);
100                                    }
101    
102                                    return false;
103                            }
104                            else {
105                                    return true;
106                            }
107                    }
108                    catch (IOException ioe) {
109                            throw new AutoDeployException(ioe);
110                    }
111                    finally {
112                            if (zipFile != null) {
113                                    try {
114                                            zipFile.close();
115                                    }
116                                    catch (IOException ioe) {
117                                    }
118                            }
119                    }
120            }
121    
122            public boolean isMatchingFileExtension(String ... extensions) {
123                    String fileName = _file.getName();
124    
125                    fileName = StringUtil.toLowerCase(fileName);
126    
127                    for (String extension : extensions) {
128                            if (fileName.endsWith(extension)) {
129                                    if (_log.isDebugEnabled()) {
130                                            _log.debug(_file.getPath() + " has a matching extension");
131                                    }
132    
133                                    return true;
134                            }
135                    }
136    
137                    if (_log.isDebugEnabled()) {
138                            _log.debug(_file.getPath() + " does not have a matching extension");
139                    }
140    
141                    return false;
142            }
143    
144            public boolean isThemePlugin() throws AutoDeployException {
145                    if (isMatchingFile("WEB-INF/liferay-look-and-feel.xml")) {
146                            return true;
147                    }
148    
149                    Matcher matcher = _themePluginPattern.matcher(_file.getName());
150    
151                    if (matcher.find() &&
152                            isMatchingFile(
153                                    "WEB-INF/liferay-plugin-package.properties", false)) {
154    
155                            return true;
156                    }
157    
158                    return false;
159            }
160    
161            public boolean isWarOrZip() {
162                    return isMatchingFileExtension(".war", ".zip");
163            }
164    
165            public boolean isWebPlugin() throws AutoDeployException {
166                    Matcher matcher = _webPluginPattern.matcher(_file.getName());
167    
168                    if (matcher.find() &&
169                            isMatchingFile(
170                                    "WEB-INF/liferay-plugin-package.properties", false)) {
171    
172                            return true;
173                    }
174    
175                    return false;
176            }
177    
178            protected boolean isJarFile() {
179                    return isMatchingFileExtension(".jar");
180            }
181    
182            private static final Log _log = LogFactoryUtil.getLog(
183                    PluginAutoDeployListenerHelper.class);
184    
185            private static final Pattern _extPluginPattern = Pattern.compile(
186                    "-(E|e)xt[-0-9.]*\\+?\\.(war|zip)$");
187            private static final Pattern _hookPluginPattern = Pattern.compile(
188                    "-(H|h)ook[-0-9.]*\\+?\\.(war|zip)$");
189            private static final Pattern _themePluginPattern = Pattern.compile(
190                    "-(T|t)heme[-0-9.]*\\+?\\.(war|zip)$");
191            private static final Pattern _webPluginPattern = Pattern.compile(
192                    "-(W|w)eb[-0-9.]*\\+?\\.(war|zip)$");
193    
194            private final File _file;
195    
196    }