001/*
002 * (C) Copyright 2010-2012 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 *
019 */
020
021package org.nuxeo.launcher.config;
022
023import java.io.File;
024import java.io.IOException;
025import java.net.InetAddress;
026import java.util.Map;
027import java.util.Properties;
028
029import org.apache.commons.io.FileUtils;
030import org.nuxeo.common.Environment;
031
032/**
033 * @author jcarsique
034 */
035public class TomcatConfigurator extends ServerConfigurator {
036
037    /**
038     * @deprecated Use {@link #getTomcatConfig()}
039     */
040    @Deprecated
041    public static final String TOMCAT_CONFIG = "conf/Catalina/localhost/nuxeo.xml";
042
043    /**
044     * @since 5.4.2
045     */
046    public static final String STARTUP_CLASS = "org.apache.catalina.startup.Bootstrap";
047
048    private String contextName = null;
049
050    /**
051     * @since 5.6
052     */
053    public static final String TOMCAT_HOME = "tomcat.home";
054
055    /**
056     * @since 5.7
057     */
058    public static final String PARAM_HTTP_TOMCAT_ADMIN_PORT = "nuxeo.server.tomcat_admin.port";
059
060    public TomcatConfigurator(ConfigurationGenerator configurationGenerator) {
061        super(configurationGenerator);
062        log.info("Detected Tomcat server.");
063    }
064
065    /**
066     * @return true if {@link #getTomcatConfig()} file already exists
067     */
068    @Override
069    protected boolean isConfigured() {
070        return new File(generator.getNuxeoHome(), getTomcatConfig()).exists();
071    }
072
073    @Override
074    protected String getDefaultDataDir() {
075        return "nxserver" + File.separator + Environment.DEFAULT_DATA_DIR;
076    }
077
078    @Override
079    public void checkPaths() throws ConfigurationException {
080        super.checkPaths();
081        File oldPath = new File(getRuntimeHome(), "data" + File.separator + "vcsh2repo");
082        String message = String.format("NXP-5370, NXP-5460. " + "Please rename 'vcsh2repo' directory from %s to %s",
083                oldPath, new File(generator.getDataDir(), "h2" + File.separator + "nuxeo"));
084        checkPath(oldPath, message);
085
086        oldPath = new File(getRuntimeHome(), "data" + File.separator + "derby" + File.separator + "nxsqldirectory");
087        message = "NXP-5370, NXP-5460. "
088                + "It is not possible to migrate Derby data."
089                + System.getProperty("line.separator")
090                + "Please remove 'nx*' directories from "
091                + oldPath.getParent()
092                + System.getProperty("line.separator")
093                + "or edit templates/default/"
094                + getTomcatConfig()
095                + System.getProperty("line.separator")
096                + "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";
097        checkPath(oldPath, message);
098    }
099
100    @Override
101    public File getLogConfFile() {
102        return new File(getServerLibDir(), "log4j.xml");
103    }
104
105    @Override
106    public File getConfigDir() {
107        return new File(getRuntimeHome(), Environment.DEFAULT_CONFIG_DIR);
108    }
109
110    /**
111     * @since 5.4.2
112     * @return Path to Tomcat configuration of Nuxeo context
113     */
114    public String getTomcatConfig() {
115        return "conf" + File.separator + "Catalina" + File.separator + "localhost" + File.separator + getContextName()
116                + ".xml";
117    }
118
119    /**
120     * @return Configured context name
121     * @since 5.4.2
122     */
123    public String getContextName() {
124        if (contextName == null) {
125            Properties userConfig = generator.getUserConfig();
126            if (userConfig != null) {
127                contextName = generator.getUserConfig().getProperty(ConfigurationGenerator.PARAM_CONTEXT_PATH,
128                        DEFAULT_CONTEXT_NAME).substring(1);
129            } else {
130                contextName = DEFAULT_CONTEXT_NAME.substring(1);
131            }
132        }
133        return contextName;
134    }
135
136    @Override
137    public void prepareWizardStart() {
138        try {
139            // remove Tomcat configuration of Nuxeo context
140            File contextXML = new File(generator.getNuxeoHome(), getTomcatConfig());
141            contextXML.delete();
142
143            // deploy wizard WAR
144            File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
145            File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName() + ".war");
146            nuxeoWAR.delete();
147            FileUtils.copyFile(wizardWAR, nuxeoWAR);
148        } catch (IOException e) {
149            log.error("Could not change Tomcat configuration to run wizard instead of Nuxeo.", e);
150        }
151    }
152
153    @Override
154    public void cleanupPostWizard() {
155        File nuxeoWAR = new File(generator.getNuxeoHome(), "webapps" + File.separator + getContextName());
156        if (nuxeoWAR.exists()) {
157            try {
158                FileUtils.deleteDirectory(nuxeoWAR);
159            } catch (IOException e) {
160                log.error("Could not delete " + nuxeoWAR, e);
161            }
162        }
163        nuxeoWAR = new File(nuxeoWAR.getPath() + ".war");
164        if (nuxeoWAR.exists()) {
165            if (!FileUtils.deleteQuietly(nuxeoWAR)) {
166                log.warn("Could not delete " + nuxeoWAR);
167                try {
168                    nuxeoWAR.deleteOnExit();
169                } catch (SecurityException e) {
170                    log.warn("Cannot delete " + nuxeoWAR);
171                }
172            }
173        }
174    }
175
176    @Override
177    public boolean isWizardAvailable() {
178        File wizardWAR = new File(generator.getNuxeoHome(), "templates" + File.separator + "nuxeo-wizard.war");
179        return wizardWAR.exists();
180    }
181
182    @Override
183    public File getRuntimeHome() {
184        return new File(generator.getNuxeoHome(), "nxserver");
185    }
186
187    @Override
188    public File getServerLibDir() {
189        return new File(generator.getNuxeoHome(), "lib");
190    }
191
192    @Override
193    protected void checkNetwork() throws ConfigurationException {
194        InetAddress bindAddress = generator.getBindAddress();
195        ConfigurationGenerator.checkPortAvailable(bindAddress,
196                Integer.parseInt(generator.getUserConfig().getProperty(PARAM_HTTP_TOMCAT_ADMIN_PORT)));
197    }
198
199    @Override
200    protected void addServerSpecificParameters(Map<String, String> parametersmigration) {
201        parametersmigration.put("nuxeo.server.tomcat-admin.port", PARAM_HTTP_TOMCAT_ADMIN_PORT);
202    }
203
204}