001/*
002 * (C) Copyright 2010-2012 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Julien Carsique
016 *
017 */
018
019package org.nuxeo.launcher.config;
020
021import java.io.File;
022import java.io.IOException;
023import java.net.InetAddress;
024import java.util.Map;
025import java.util.Properties;
026
027import org.apache.commons.io.FileUtils;
028import org.nuxeo.common.Environment;
029
030/**
031 * @author jcarsique
032 */
033public class TomcatConfigurator extends ServerConfigurator {
034
035    /**
036     * @deprecated Use {@link #getTomcatConfig()}
037     */
038    @Deprecated
039    public static final String TOMCAT_CONFIG = "conf/Catalina/localhost/nuxeo.xml";
040
041    /**
042     * @since 5.4.2
043     */
044    public static final String STARTUP_CLASS = "org.apache.catalina.startup.Bootstrap";
045
046    private String contextName = null;
047
048    /**
049     * @since 5.6
050     */
051    public static final String TOMCAT_HOME = "tomcat.home";
052
053    /**
054     * @since 5.7
055     */
056    public static final String PARAM_HTTP_TOMCAT_ADMIN_PORT = "nuxeo.server.tomcat_admin.port";
057
058    public TomcatConfigurator(ConfigurationGenerator configurationGenerator) {
059        super(configurationGenerator);
060        log.info("Detected Tomcat server.");
061    }
062
063    /**
064     * @return true if {@link #getTomcatConfig()} file already exists
065     */
066    @Override
067    protected boolean isConfigured() {
068        return new File(generator.getNuxeoHome(), getTomcatConfig()).exists();
069    }
070
071    @Override
072    protected String getDefaultDataDir() {
073        return "nxserver" + File.separator + Environment.DEFAULT_DATA_DIR;
074    }
075
076    @Override
077    public void checkPaths() throws ConfigurationException {
078        super.checkPaths();
079        File oldPath = new File(getRuntimeHome(), "data" + File.separator + "vcsh2repo");
080        String message = String.format("NXP-5370, NXP-5460. " + "Please rename 'vcsh2repo' directory from %s to %s",
081                oldPath, new File(generator.getDataDir(), "h2" + File.separator + "nuxeo"));
082        checkPath(oldPath, message);
083
084        oldPath = new File(getRuntimeHome(), "data" + File.separator + "derby" + File.separator + "nxsqldirectory");
085        message = "NXP-5370, NXP-5460. "
086                + "It is not possible to migrate Derby data."
087                + System.getProperty("line.separator")
088                + "Please remove 'nx*' directories from "
089                + oldPath.getParent()
090                + System.getProperty("line.separator")
091                + "or edit templates/default/"
092                + getTomcatConfig()
093                + System.getProperty("line.separator")
094                + "following https://github.com/nuxeo/nuxeo-distribution/blob/release-5.3.2/nuxeo-distribution-resources/src/main/resources/templates-tomcat/default/conf/Catalina/localhost/nuxeo.xml";
095        checkPath(oldPath, message);
096    }
097
098    @Override
099    public File getLogConfFile() {
100        return new File(getServerLibDir(), "log4j.xml");
101    }
102
103    @Override
104    public File getConfigDir() {
105        return new File(getRuntimeHome(), Environment.DEFAULT_CONFIG_DIR);
106    }
107
108    /**
109     * @since 5.4.2
110     * @return Path to Tomcat configuration of Nuxeo context
111     */
112    public String getTomcatConfig() {
113        return "conf" + File.separator + "Catalina" + File.separator + "localhost" + File.separator + getContextName()
114                + ".xml";
115    }
116
117    /**
118     * @return Configured context name
119     * @since 5.4.2
120     */
121    public String getContextName() {
122        if (contextName == null) {
123            Properties userConfig = generator.getUserConfig();
124            if (userConfig != null) {
125                contextName = generator.getUserConfig().getProperty(ConfigurationGenerator.PARAM_CONTEXT_PATH,
126                        DEFAULT_CONTEXT_NAME).substring(1);
127            } else {
128                contextName = DEFAULT_CONTEXT_NAME.substring(1);
129            }
130        }
131        return contextName;
132    }
133
134    @Override
135    public void prepareWizardStart() {
136        try {
137            // remove Tomcat configuration of Nuxeo context
138            File contextXML = new File(generator.getNuxeoHome(), getTomcatConfig());
139            contextXML.delete();
140
141            // deploy wizard WAR
142            File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
143            File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName() + ".war");
144            nuxeoWAR.delete();
145            FileUtils.copyFile(wizardWAR, nuxeoWAR);
146        } catch (IOException e) {
147            log.error("Could not change Tomcat configuration to run wizard instead of Nuxeo.", e);
148        }
149    }
150
151    @Override
152    public void cleanupPostWizard() {
153        File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName());
154        if (nuxeoWAR.exists()) {
155            try {
156                FileUtils.deleteDirectory(nuxeoWAR);
157            } catch (IOException e) {
158                log.error("Could not delete " + nuxeoWAR, e);
159            }
160        }
161        nuxeoWAR = new File(nuxeoWAR.getPath() + ".war");
162        if (nuxeoWAR.exists()) {
163            if (!FileUtils.deleteQuietly(nuxeoWAR)) {
164                log.warn("Could not delete " + nuxeoWAR);
165                try {
166                    nuxeoWAR.deleteOnExit();
167                } catch (SecurityException e) {
168                    log.warn("Cannot delete " + nuxeoWAR);
169                }
170            }
171        }
172    }
173
174    @Override
175    public boolean isWizardAvailable() {
176        File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
177        return wizardWAR.exists();
178    }
179
180    @Override
181    public File getRuntimeHome() {
182        return new File(generator.getNuxeoHome(), "nxserver");
183    }
184
185    @Override
186    public File getServerLibDir() {
187        return new File(generator.getNuxeoHome(), "lib");
188    }
189
190    @Override
191    protected void checkNetwork() throws ConfigurationException {
192        InetAddress bindAddress = generator.getBindAddress();
193        ConfigurationGenerator.checkPortAvailable(bindAddress,
194                Integer.parseInt(generator.getUserConfig().getProperty(PARAM_HTTP_TOMCAT_ADMIN_PORT)));
195    }
196
197    @Override
198    protected void addServerSpecificParameters(Map<String, String> parametersmigration) {
199        parametersmigration.put("nuxeo.server.tomcat-admin.port", PARAM_HTTP_TOMCAT_ADMIN_PORT);
200    }
201
202}