001/*
002 * (C) Copyright 2010-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Julien Carsique
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     * @since 5.4.2
037     */
038    public static final String STARTUP_CLASS = "org.apache.catalina.startup.Bootstrap";
039
040    private String contextName = null;
041
042    /**
043     * @since 5.6
044     */
045    public static final String TOMCAT_HOME = "tomcat.home";
046
047    /**
048     * @since 5.7
049     */
050    public static final String PARAM_HTTP_TOMCAT_ADMIN_PORT = "nuxeo.server.tomcat_admin.port";
051
052    public TomcatConfigurator(ConfigurationGenerator configurationGenerator) {
053        super(configurationGenerator);
054        log.info("Detected Tomcat server.");
055    }
056
057    /**
058     * @return true if {@link #getTomcatConfig()} file already exists
059     */
060    @Override
061    protected boolean isConfigured() {
062        return new File(generator.getNuxeoHome(), getTomcatConfig()).exists();
063    }
064
065    @Override
066    protected String getDefaultDataDir() {
067        return "nxserver" + File.separator + Environment.DEFAULT_DATA_DIR;
068    }
069
070    @Override
071    public void checkPaths() throws ConfigurationException {
072        super.checkPaths();
073        File oldPath = new File(getRuntimeHome(), "data" + File.separator + "vcsh2repo");
074        String message = String.format("NXP-5370, NXP-5460. " + "Please rename 'vcsh2repo' directory from %s to %s",
075                oldPath, new File(generator.getDataDir(), "h2" + File.separator + "nuxeo"));
076        checkPath(oldPath, message);
077
078        oldPath = new File(getRuntimeHome(), "data" + File.separator + "derby" + File.separator + "nxsqldirectory");
079        message = "NXP-5370, NXP-5460. " + "It is not possible to migrate Derby data."
080                + System.getProperty("line.separator") + "Please remove 'nx*' directories from " + oldPath.getParent()
081                + System.getProperty("line.separator") + "or edit templates/default/" + getTomcatConfig()
082                + System.getProperty("line.separator")
083                + "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";
084        checkPath(oldPath, message);
085    }
086
087    @Override
088    public File getLogConfFile() {
089        return new File(getServerLibDir(), "log4j.xml");
090    }
091
092    @Override
093    public File getConfigDir() {
094        return new File(getRuntimeHome(), Environment.DEFAULT_CONFIG_DIR);
095    }
096
097    /**
098     * @since 5.4.2
099     * @return Path to Tomcat configuration of Nuxeo context
100     */
101    public String getTomcatConfig() {
102        return "conf" + File.separator + "Catalina" + File.separator + "localhost" + File.separator + getContextName()
103                + ".xml";
104    }
105
106    /**
107     * @return Configured context name
108     * @since 5.4.2
109     */
110    public String getContextName() {
111        if (contextName == null) {
112            Properties userConfig = generator.getUserConfig();
113            if (userConfig != null) {
114                contextName = generator.getUserConfig()
115                                       .getProperty(ConfigurationGenerator.PARAM_CONTEXT_PATH, DEFAULT_CONTEXT_NAME)
116                                       .substring(1);
117            } else {
118                contextName = DEFAULT_CONTEXT_NAME.substring(1);
119            }
120        }
121        return contextName;
122    }
123
124    @Override
125    public void prepareWizardStart() {
126        try {
127            // remove Tomcat configuration of Nuxeo context
128            File contextXML = new File(generator.getNuxeoHome(), getTomcatConfig());
129            contextXML.delete();
130
131            // deploy wizard WAR
132            File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
133            File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName() + ".war");
134            nuxeoWAR.delete();
135            FileUtils.copyFile(wizardWAR, nuxeoWAR);
136        } catch (IOException e) {
137            log.error("Could not change Tomcat configuration to run wizard instead of Nuxeo.", e);
138        }
139    }
140
141    @Override
142    public void cleanupPostWizard() {
143        File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName());
144        if (nuxeoWAR.exists()) {
145            try {
146                FileUtils.deleteDirectory(nuxeoWAR);
147            } catch (IOException e) {
148                log.error("Could not delete " + nuxeoWAR, e);
149            }
150        }
151        nuxeoWAR = new File(nuxeoWAR.getPath() + ".war");
152        if (nuxeoWAR.exists()) {
153            if (!FileUtils.deleteQuietly(nuxeoWAR)) {
154                log.warn("Could not delete " + nuxeoWAR);
155                try {
156                    nuxeoWAR.deleteOnExit();
157                } catch (SecurityException e) {
158                    log.warn("Cannot delete " + nuxeoWAR);
159                }
160            }
161        }
162    }
163
164    @Override
165    public boolean isWizardAvailable() {
166        File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
167        return wizardWAR.exists();
168    }
169
170    @Override
171    public File getRuntimeHome() {
172        return new File(generator.getNuxeoHome(), "nxserver");
173    }
174
175    @Override
176    public File getServerLibDir() {
177        return new File(generator.getNuxeoHome(), "lib");
178    }
179
180    @Override
181    protected void checkNetwork() throws ConfigurationException {
182        InetAddress bindAddress = generator.getBindAddress();
183        ConfigurationGenerator.checkPortAvailable(bindAddress,
184                Integer.parseInt(generator.getUserConfig().getProperty(PARAM_HTTP_TOMCAT_ADMIN_PORT)));
185    }
186
187    @Override
188    protected void addServerSpecificParameters(Map<String, String> parametersmigration) {
189        parametersmigration.put("nuxeo.server.tomcat-admin.port", PARAM_HTTP_TOMCAT_ADMIN_PORT);
190    }
191
192}