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.UnsecureSAXReaderUtil;
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            @Override
062            public int deploy(AutoDeploymentContext autoDeploymentContext)
063                    throws AutoDeployException {
064    
065                    File file = autoDeploymentContext.getFile();
066    
067                    return deploy(file);
068            }
069    
070            public File getDocBaseDir(File file, String checkXmlFile)
071                    throws AutoDeployException {
072    
073                    if (!isMatchingFileExtension(file)) {
074                            return null;
075                    }
076    
077                    String docBase = null;
078    
079                    try {
080                            String content = FileUtil.read(file);
081    
082                            Document document = UnsecureSAXReaderUtil.read(content);
083    
084                            Element rootElement = document.getRootElement();
085    
086                            docBase = rootElement.attributeValue("docBase");
087    
088                            docBase = String.valueOf(
089                                    PropertyConverter.interpolate(docBase, _systemConfiguration));
090                    }
091                    catch (Exception e) {
092                            throw new AutoDeployException(e);
093                    }
094    
095                    if (Validator.isNull(docBase)) {
096                            if (_log.isDebugEnabled()) {
097                                    _log.debug(file.getPath() + " does not have a docBase defined");
098                            }
099    
100                            return null;
101                    }
102    
103                    File docBaseDir = new File(docBase);
104    
105                    if (!docBaseDir.exists()) {
106                            if (_log.isDebugEnabled()) {
107                                    _log.debug(docBase + " does not exist");
108                            }
109    
110                            return null;
111                    }
112    
113                    if (!docBaseDir.isDirectory()) {
114                            if (_log.isDebugEnabled()) {
115                                    _log.debug(docBase + " is not a directory");
116                            }
117    
118                            return null;
119                    }
120    
121                    if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
122                            if (_log.isDebugEnabled()) {
123                                    _log.debug(docBase + " does not have " + checkXmlFile);
124                            }
125    
126                            return null;
127                    }
128    
129                    return docBaseDir;
130            }
131    
132            public boolean isMatchingFileExtension(File file) {
133                    if (file.getName().endsWith(".xml")) {
134                            if (_log.isDebugEnabled()) {
135                                    _log.debug(file.getPath() + " has a matching extension");
136                            }
137    
138                            return true;
139                    }
140    
141                    if (_log.isDebugEnabled()) {
142                            _log.debug(file.getPath() + " does not have a matching extension");
143                    }
144    
145                    return false;
146            }
147    
148            protected abstract int deploy(File file) throws AutoDeployException;
149    
150            private static Log _log = LogFactoryUtil.getLog(
151                    BaseExplodedTomcatListener.class);
152    
153            private static SystemConfiguration _systemConfiguration =
154                    new SystemConfiguration();
155    
156    }