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