001    /**
002     * Copyright (c) 2000-2013 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.zip.ZipFile;
025    
026    /**
027     * @author Ivica Cardic
028     * @author Brian Wing Shun Chan
029     * @author Ryan Park
030     */
031    public abstract class BaseAutoDeployListener implements AutoDeployListener {
032    
033            public boolean isExtPlugin(File file) {
034                    String fileName = file.getName();
035    
036                    if (fileName.contains("-ext") && !isJarFile(file)) {
037                            return true;
038                    }
039    
040                    return false;
041            }
042    
043            public boolean isHookPlugin(File file) throws AutoDeployException {
044                    String fileName = file.getName();
045    
046                    if (isMatchingFile(file, "WEB-INF/liferay-hook.xml") &&
047                            !isMatchingFile(file, "WEB-INF/liferay-portlet.xml") &&
048                            !fileName.contains("-theme") && !fileName.contains("-web") &&
049                            !isJarFile(file)) {
050    
051                            return true;
052                    }
053    
054                    return false;
055            }
056    
057            public boolean isLayoutTemplatePlugin(File file)
058                    throws AutoDeployException {
059    
060                    if (isMatchingFile(file, "WEB-INF/liferay-layout-templates.xml") &&
061                            !isThemePlugin(file)) {
062    
063                            return true;
064                    }
065    
066                    return false;
067            }
068    
069            public boolean isLiferayPackage(File file) {
070                    String fileName = file.getName();
071    
072                    if (fileName.endsWith(".lpkg")) {
073                            return true;
074                    }
075    
076                    return false;
077            }
078    
079            public boolean isMatchingFile(File file, String checkXmlFile)
080                    throws AutoDeployException {
081    
082                    if (!isMatchingFileExtension(file)) {
083                            return false;
084                    }
085    
086                    ZipFile zipFile = null;
087    
088                    try {
089                            zipFile = new ZipFile(file);
090    
091                            if (zipFile.getEntry(checkXmlFile) == null) {
092                                    if (_log.isDebugEnabled()) {
093                                            _log.debug(
094                                                    file.getPath() + " does not have " + checkXmlFile);
095                                    }
096    
097                                    return false;
098                            }
099                            else {
100                                    return true;
101                            }
102                    }
103                    catch (IOException ioe) {
104                            throw new AutoDeployException(ioe);
105                    }
106                    finally {
107                            if (zipFile != null) {
108                                    try {
109                                            zipFile.close();
110                                    }
111                                    catch (IOException ioe) {
112                                    }
113                            }
114                    }
115            }
116    
117            public boolean isMatchingFileExtension(File file) {
118                    return isMatchingFileExtension(file, ".war", ".zip");
119            }
120    
121            public boolean isMatchingFileExtension(File file, String ... extensions) {
122                    String fileName = file.getName();
123    
124                    fileName = StringUtil.toLowerCase(fileName);
125    
126                    for (String extension : extensions) {
127                            if (fileName.endsWith(extension)) {
128                                    if (_log.isDebugEnabled()) {
129                                            _log.debug(file.getPath() + " has a matching extension");
130                                    }
131    
132                                    return true;
133                            }
134                    }
135    
136                    if (_log.isDebugEnabled()) {
137                            _log.debug(file.getPath() + " does not have a matching extension");
138                    }
139    
140                    return false;
141            }
142    
143            public boolean isThemePlugin(File file) throws AutoDeployException {
144                    if (isMatchingFile(file, "WEB-INF/liferay-look-and-feel.xml") &&
145                            !isJarFile(file)) {
146    
147                            return true;
148                    }
149    
150                    String fileName = file.getName();
151    
152                    if (isMatchingFile(file, "WEB-INF/liferay-plugin-package.properties") &&
153                            fileName.contains("-theme")) {
154    
155                            return true;
156                    }
157    
158                    return false;
159            }
160    
161            public boolean isWebPlugin(File file) throws AutoDeployException {
162                    String fileName = file.getName();
163    
164                    if (isMatchingFile(file, "WEB-INF/liferay-plugin-package.properties") &&
165                            fileName.contains("-web") && !isJarFile(file)) {
166    
167                            return true;
168                    }
169    
170                    return false;
171            }
172    
173            protected boolean isJarFile(File file) {
174                    return isMatchingFileExtension(file, ".jar");
175            }
176    
177            private static Log _log = LogFactoryUtil.getLog(
178                    BaseAutoDeployListener.class);
179    
180    }