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