001/*
002 * (C) Copyright 2010-2015 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 *     tdelprat, jcarsique
018 */
019
020package org.nuxeo.ecm.admin;
021
022import java.io.File;
023import java.io.IOException;
024import org.apache.commons.lang3.StringUtils;
025import org.apache.commons.lang3.SystemUtils;
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.common.Environment;
029import org.nuxeo.launcher.config.ConfigurationGenerator;
030import org.nuxeo.runtime.api.Framework;
031
032/**
033 * Helper class to call NuxeoCtl restart.
034 *
035 * @author Tiry (tdelprat@nuxeo.com)
036 */
037public class NuxeoCtlManager {
038
039    protected static final String CMD_POSIX = "nuxeoctl";
040
041    protected static final String CMD_WIN = "nuxeoctl.bat";
042
043    protected static final Log log = LogFactory.getLog(NuxeoCtlManager.class);
044
045    private ConfigurationGenerator cg;
046
047    /**
048     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_WINDOWS}
049     */
050    @Deprecated
051    public static boolean isWindows() {
052        return SystemUtils.IS_OS_WINDOWS;
053    }
054
055    private static String winEscape(String command) {
056        return command.replaceAll("([ ()<>&])", "^$1");
057    }
058
059    protected static boolean doExec(String path, String logPath) {
060        try {
061            String[] cmd = getCommand(path);
062            if (log.isDebugEnabled()) {
063                log.debug("Restart command: " + StringUtils.join(cmd, " "));
064            }
065            ProcessBuilder pb = new ProcessBuilder(cmd).redirectOutput(new File(logPath, "restart.log")).redirectError(
066                    new File(logPath, "restart-err.log"));
067            pb.start();
068        } catch (IOException e) {
069            log.error("Unable to restart server", e);
070            return false;
071        }
072
073        return true;
074    }
075
076    /**
077     * Gets the OS dependent command for nuxeoctl restartbg
078     *
079     * @param path the Nuxeo bin path
080     * @return an array of String to pass to the {@link ProcessBuilder}
081     * @since 9.2
082     */
083    protected static String[] getCommand(String path) {
084        if (SystemUtils.IS_OS_WINDOWS) {
085            return new String[] { "cmd", "/C", winEscape(new File(path, CMD_WIN).getPath()), "--gui=false",
086                    "restartbg" };
087        }
088        return new String[] { "/bin/sh", "-c", "\"" + new File(path, CMD_POSIX).getPath() + "\"" + " restartbg" };
089    }
090
091    private static boolean restartInProgress = false;
092
093    public static synchronized boolean restart() {
094        if (restartInProgress) {
095            return false;
096        }
097        restartInProgress = true;
098        String nuxeoHome = Framework.getProperty(Environment.NUXEO_HOME);
099        final String binPath = new File(nuxeoHome, "bin").getPath();
100        final String logDir = Framework.getProperty(Environment.NUXEO_LOG_DIR, nuxeoHome);
101        new Thread("restart thread") {
102            @Override
103            public void run() {
104                try {
105                    log.info("Restarting Nuxeo server");
106                    Thread.sleep(3000);
107                    doExec(binPath, logDir);
108                } catch (InterruptedException e) {
109                    log.error("Restart failed", e);
110                }
111            }
112        }.start();
113        return true;
114    }
115
116    public String restartServer() {
117        restart();
118        return "Nuxeo server is restarting";
119    }
120
121    /**
122     * @since 5.6
123     * @return Configured server URL (may differ from current URL)
124     */
125    public String getServerURL() {
126        if (cg == null) {
127            cg = new ConfigurationGenerator();
128            cg.init();
129        }
130        return cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_NUXEO_URL);
131    }
132
133}