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