001    /**
002     * Copyright (c) 2000-2010 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    /**
033     * @author Olaf Fricke
034     * @author Brian Wing Shun Chan
035     */
036    public abstract class BaseExplodedTomcatListener implements AutoDeployListener {
037    
038            public void copyContextFile(File file) throws AutoDeployException {
039                    try {
040                            String tomcatConfDir = PrefsPropsUtil.getString(
041                                    PropsKeys.AUTO_DEPLOY_TOMCAT_CONF_DIR,
042                                    PropsValues.AUTO_DEPLOY_TOMCAT_CONF_DIR);
043    
044                            if (_log.isInfoEnabled()) {
045                                    _log.info(
046                                            "Copying file " + file.getPath() + " to " + tomcatConfDir);
047                            }
048    
049                            FileUtil.copyFile(
050                                    file, new File(tomcatConfDir + "/" + file.getName()));
051                    }
052                    catch (Exception e) {
053                            throw new AutoDeployException(e.getMessage());
054                    }
055            }
056    
057            public File getDocBaseDir(File file, String checkXmlFile)
058                    throws AutoDeployException {
059    
060                    if (!isMatchingFileExtension(file)) {
061                            return null;
062                    }
063    
064                    String docBase = null;
065    
066                    try {
067                            String content = FileUtil.read(file);
068    
069                            Document doc = SAXReaderUtil.read(content);
070    
071                            Element root = doc.getRootElement();
072    
073                            docBase = root.attributeValue("docBase");
074                    }
075                    catch (Exception e) {
076                            throw new AutoDeployException(e);
077                    }
078    
079                    if (Validator.isNull(docBase)) {
080                            if (_log.isDebugEnabled()) {
081                                    _log.debug(
082                                            file.getPath() + " does not have a docBase defined");
083                            }
084    
085                            return null;
086                    }
087    
088                    File docBaseDir = new File(docBase);
089    
090                    if (!docBaseDir.exists()) {
091                            if (_log.isDebugEnabled()) {
092                                    _log.debug(docBase + " does not exist");
093                            }
094    
095                            return null;
096                    }
097    
098                    if (!docBaseDir.isDirectory()) {
099                            if (_log.isDebugEnabled()) {
100                                    _log.debug(docBase + " is not a directory");
101                            }
102    
103                            return null;
104                    }
105    
106                    if (!FileUtil.exists(docBase + "/" + checkXmlFile)) {
107                            if (_log.isDebugEnabled()) {
108                                    _log.debug(docBase + " does not have " + checkXmlFile);
109                            }
110    
111                            return null;
112                    }
113    
114                    return docBaseDir;
115            }
116    
117            public boolean isMatchingFileExtension(File file) {
118                    if (file.getName().endsWith(".xml")) {
119                            if (_log.isDebugEnabled()) {
120                                    _log.debug(file.getPath() + " has a matching extension");
121                            }
122    
123                            return true;
124                    }
125                    else {
126                            if (_log.isDebugEnabled()) {
127                                    _log.debug(
128                                            file.getPath() + " does not have a matching extension");
129                            }
130    
131                            return false;
132                    }
133            }
134    
135            private static Log _log = LogFactoryUtil.getLog(
136                    BaseExplodedTomcatListener.class);
137    
138    }