001    /**
002     * Copyright (c) 2000-2012 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")) {
036                            return true;
037                    }
038    
039                    return false;
040            }
041    
042            public boolean isHookPlugin(File file) throws AutoDeployException {
043                    if (isMatchingFile(file, "WEB-INF/liferay-hook.xml") &&
044                            !isMatchingFile(file, "WEB-INF/liferay-portlet.xml")) {
045    
046                            return true;
047                    }
048    
049                    return false;
050            }
051    
052            public boolean isLiferayPackage(File file) {
053                    String fileName = file.getName();
054    
055                    if (fileName.endsWith(".lpkg")) {
056                            return true;
057                    }
058    
059                    return false;
060            }
061    
062            public boolean isMatchingFile(File file, String checkXmlFile)
063                    throws AutoDeployException {
064    
065                    if (!isMatchingFileExtension(file)) {
066                            return false;
067                    }
068    
069                    ZipFile zipFile = null;
070    
071                    try {
072                            zipFile = new ZipFile(file);
073    
074                            if (zipFile.getEntry(checkXmlFile) == null) {
075                                    if (_log.isDebugEnabled()) {
076                                            _log.debug(
077                                                    file.getPath() + " does not have " + checkXmlFile);
078                                    }
079    
080                                    return false;
081                            }
082                            else {
083                                    return true;
084                            }
085                    }
086                    catch (IOException ioe) {
087                            throw new AutoDeployException(ioe);
088                    }
089                    finally {
090                            if (zipFile != null) {
091                                    try {
092                                            zipFile.close();
093                                    }
094                                    catch (IOException ioe) {
095                                    }
096                            }
097                    }
098            }
099    
100            public boolean isMatchingFileExtension(File file) {
101                    String fileName = file.getName().toLowerCase();
102    
103                    if (fileName.endsWith(".war") || fileName.endsWith(".zip")) {
104                            if (_log.isDebugEnabled()) {
105                                    _log.debug(file.getPath() + " has a matching extension");
106                            }
107    
108                            return true;
109                    }
110                    else {
111                            if (_log.isDebugEnabled()) {
112                                    _log.debug(
113                                            file.getPath() + " does not have a matching extension");
114                            }
115    
116                            return false;
117                    }
118            }
119    
120            public boolean isThemePlugin(File file) throws AutoDeployException {
121                    if (isMatchingFile(file, "WEB-INF/liferay-look-and-feel.xml")) {
122                            return true;
123                    }
124    
125                    String fileName = file.getName();
126    
127                    if (isMatchingFile(file, "WEB-INF/liferay-plugin-package.properties") &&
128                            fileName.contains("-theme")) {
129    
130                            return true;
131                    }
132    
133                    return false;
134            }
135    
136            public boolean isWebPlugin(File file) throws AutoDeployException {
137                    String fileName = file.getName();
138    
139                    if (isMatchingFile(file, "WEB-INF/liferay-plugin-package.properties") &&
140                            fileName.contains("-web")) {
141    
142                            return true;
143                    }
144    
145                    return false;
146            }
147    
148            private static Log _log = LogFactoryUtil.getLog(
149                    BaseAutoDeployListener.class);
150    
151    }