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                    if (_file.isDirectory()) {
092                            File xmlFile = new File(_file, checkXmlFile);
093    
094                            return xmlFile.exists();
095                    }
096                    else {
097                            ZipFile zipFile = null;
098    
099                            try {
100                                    zipFile = new ZipFile(_file);
101    
102                                    if (zipFile.getEntry(checkXmlFile) == null) {
103                                            if (_log.isDebugEnabled()) {
104                                                    _log.debug(
105                                                            _file.getPath() + " does not have " + checkXmlFile);
106                                            }
107    
108                                            return false;
109                                    }
110                                    else {
111                                            return true;
112                                    }
113                            }
114                            catch (IOException ioe) {
115                                    throw new AutoDeployException(ioe);
116                            }
117                            finally {
118                                    if (zipFile != null) {
119                                            try {
120                                                    zipFile.close();
121                                            }
122                                            catch (IOException ioe) {
123                                            }
124                                    }
125                            }
126                    }
127            }
128    
129            public boolean isMatchingFileExtension(String... extensions) {
130                    String fileName = _file.getName();
131    
132                    fileName = StringUtil.toLowerCase(fileName);
133    
134                    for (String extension : extensions) {
135                            if (fileName.endsWith(extension)) {
136                                    if (_log.isDebugEnabled()) {
137                                            _log.debug(_file.getPath() + " has a matching extension");
138                                    }
139    
140                                    return true;
141                            }
142                    }
143    
144                    if (_log.isDebugEnabled()) {
145                            _log.debug(_file.getPath() + " does not have a matching extension");
146                    }
147    
148                    return false;
149            }
150    
151            public boolean isThemePlugin() throws AutoDeployException {
152                    if (isMatchingFile("WEB-INF/liferay-look-and-feel.xml", false)) {
153                            return true;
154                    }
155    
156                    Matcher matcher = _themePluginPattern.matcher(_file.getName());
157    
158                    if (matcher.find() &&
159                            isMatchingFile(
160                                    "WEB-INF/liferay-plugin-package.properties", false)) {
161    
162                            return true;
163                    }
164    
165                    return false;
166            }
167    
168            public boolean isWarOrZip() {
169                    return isMatchingFileExtension(".war", ".zip");
170            }
171    
172            public boolean isWebPlugin() throws AutoDeployException {
173                    Matcher matcher = _webPluginPattern.matcher(_file.getName());
174    
175                    if (matcher.find() &&
176                            isMatchingFile(
177                                    "WEB-INF/liferay-plugin-package.properties", false)) {
178    
179                            return true;
180                    }
181    
182                    return false;
183            }
184    
185            protected boolean isJarFile() {
186                    return isMatchingFileExtension(".jar");
187            }
188    
189            private static final Log _log = LogFactoryUtil.getLog(
190                    PluginAutoDeployListenerHelper.class);
191    
192            private static final Pattern _extPluginPattern = Pattern.compile(
193                    "-(E|e)xt[-0-9.]*\\+?\\.(war|zip)$");
194            private static final Pattern _hookPluginPattern = Pattern.compile(
195                    "-(H|h)ook[-0-9.]*\\+?\\.(war|zip)$");
196            private static final Pattern _themePluginPattern = Pattern.compile(
197                    "-(T|t)heme[-0-9.]*\\+?\\.(war|zip)$");
198            private static final Pattern _webPluginPattern = Pattern.compile(
199                    "-(W|w)eb[-0-9.]*\\+?\\.(war|zip)$");
200    
201            private final File _file;
202    
203    }