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.deploy.auto.exploded.tomcat;
016    
017    import com.liferay.portal.kernel.deploy.auto.AutoDeployException;
018    import com.liferay.portal.kernel.deploy.auto.AutoDeployListener;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.FileUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.kernel.xml.Document;
025    import com.liferay.portal.kernel.xml.Element;
026    import com.liferay.portal.kernel.xml.SAXReaderUtil;
027    import com.liferay.portal.util.PrefsPropsUtil;
028    import com.liferay.portal.util.PropsValues;
029    
030    import java.io.File;
031    
032    import org.apache.commons.configuration.PropertyConverter;
033    import org.apache.commons.configuration.SystemConfiguration;
034    
035    /**
036     * @author Olaf Fricke
037     * @author Brian Wing Shun Chan
038     */
039    public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
040    
041            public void copyContextFile(File file) throws AutoDeployException {
042                    try {
043                            String tomcatConfDir = PrefsPropsUtil.getString(
044                                    PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
045                                    PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
046    
047                            if (_log.isInfoEnabled()) {
048                                    _log.info(
049                                            "Copying file " + file.getPath() + " to " + tomcatConfDir);
050                            }
051    
052                            FileUtil.copyFile(
053                                    file, new File(tomcatConfDir + "/" + file.getName()));
054                    }
055                    catch (Exception e) {
056                            throw new AutoDeployException(e.getMessage());
057                    }
058            }
059    
060            public void deploy(File file, String context) throws AutoDeployException {
061                    deploy(file);
062            }
063    
064            public File getDocBaseDir(File file, String checkXmlFile)
065                    throws AutoDeployException {
066    
067                    if (!isMatchingFileExtension(file)) {
068                            return null;
069                    }
070    
071                    String docBase = null;
072    
073                    try {
074                            String content = FileUtil.read(file);
075    
076                            Document document = SAXReaderUtil.read(content);
077    
078                            Element rootElement = document.getRootElement();
079    
080                            docBase = rootElement.attributeValue("docBase");
081    
082                            docBase = String.valueOf(
083                                    PropertyConverter.interpolate(docBase, _systemConfiguration));
084                    }
085                    catch (Exception e) {
086                            throw new AutoDeployException(e);
087                    }
088    
089                    if (Validator.isNull(docBase)) {
090                            if (_log.isDebugEnabled()) {
091                                    _log.debug(file.getPath() + " does not have a docBase defined");
092                            }
093    
094                            return null;
095                    }
096    
097                    File docBaseDir = new File(docBase);
098    
099                    if (!docBaseDir.exists()) {
100                            if (_log.isDebugEnabled()) {
101                                    _log.debug(docBase + " does not exist");
102                            }
103    
104                            return null;
105                    }
106    
107                    if (!docBaseDir.isDirectory()) {
108                            if (_log.isDebugEnabled()) {
109                                    _log.debug(docBase + " is not a directory");
110                            }
111    
112                            return null;
113                    }
114    
115                    if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
116                            if (_log.isDebugEnabled()) {
117                                    _log.debug(docBase + " does not have " + checkXmlFile);
118                            }
119    
120                            return null;
121                    }
122    
123                    return docBaseDir;
124            }
125    
126            public boolean isMatchingFileExtension(File file) {
127                    if (file.getName().endsWith(".xml")) {
128                            if (_log.isDebugEnabled()) {
129                                    _log.debug(file.getPath() + " has a matching extension");
130                            }
131    
132                            return true;
133                    }
134                    else {
135                            if (_log.isDebugEnabled()) {
136                                    _log.debug(
137                                            file.getPath() + " does not have a matching extension");
138                            }
139    
140                            return false;
141                    }
142            }
143    
144            protected abstract void deploy(File file) throws AutoDeployException;
145    
146            private static Log _log = LogFactoryUtil.getLog(
147                    BaseExplodedTomcatListener.class);
148    
149            private static SystemConfiguration _systemConfiguration =
150                    new SystemConfiguration();
151    
152    }