001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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(
092                                            file.getPath() + " does not have a docBase defined");
093                            }
094    
095                            return null;
096                    }
097    
098                    File docBaseDir = new File(docBase);
099    
100                    if (!docBaseDir.exists()) {
101                            if (_log.isDebugEnabled()) {
102                                    _log.debug(docBase + " does not exist");
103                            }
104    
105                            return null;
106                    }
107    
108                    if (!docBaseDir.isDirectory()) {
109                            if (_log.isDebugEnabled()) {
110                                    _log.debug(docBase + " is not a directory");
111                            }
112    
113                            return null;
114                    }
115    
116                    if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
117                            if (_log.isDebugEnabled()) {
118                                    _log.debug(docBase + " does not have " + checkXmlFile);
119                            }
120    
121                            return null;
122                    }
123    
124                    return docBaseDir;
125            }
126    
127            public boolean isMatchingFileExtension(File file) {
128                    if (file.getName().endsWith(".xml")) {
129                            if (_log.isDebugEnabled()) {
130                                    _log.debug(file.getPath() + " has a matching extension");
131                            }
132    
133                            return true;
134                    }
135                    else {
136                            if (_log.isDebugEnabled()) {
137                                    _log.debug(
138                                            file.getPath() + " does not have a matching extension");
139                            }
140    
141                            return false;
142                    }
143            }
144    
145            protected abstract void deploy(File file) throws AutoDeployException;
146    
147            private static Log _log = LogFactoryUtil.getLog(
148                    BaseExplodedTomcatListener.class);
149    
150            private static SystemConfiguration _systemConfiguration =
151                    new SystemConfiguration();
152    
153    }