001/*
002 * (C) Copyright 2011-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.wizard.helpers;
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.launcher.config.ConfigurationGenerator;
032import org.nuxeo.log4j.ThreadedStreamGobbler;
033import org.nuxeo.wizard.context.Context;
034import org.nuxeo.wizard.context.ParamCollector;
035
036/**
037 * Manages execution of NuxeoCtl
038 *
039 * @author Tiry (tdelprat@nuxeo.com)
040 * @since 5.4.2
041 */
042public class ServerController {
043
044    protected static final String CMD_POSIX = "nuxeoctl";
045
046    protected static final String CMD_WIN = "nuxeoctl.bat";
047
048    protected static Log log = LogFactory.getLog(ServerController.class);
049
050    /**
051     * @deprecated Since 7.4. Use {@link SystemUtils#IS_OS_WINDOWS}
052     */
053    @Deprecated
054    public static boolean isWindows() {
055        return SystemUtils.IS_OS_WINDOWS;
056    }
057
058    private static String winEscape(String command) {
059        return command.replaceAll("([ ()<>&])", "^$1");
060    }
061
062    protected static boolean doExec(String path, String logPath) {
063        String[] cmd;
064        if (SystemUtils.IS_OS_WINDOWS) {
065            cmd = new String[] { "cmd", "/C", winEscape(new File(path, CMD_WIN).getPath()), "--gui=false", "restartbg" };
066        } else {
067            cmd = new String[] { "/bin/sh", "-c", "\"" + new File(path, CMD_POSIX).getPath() + "\"" + " restartbg" };
068        }
069
070        Process p1;
071        try {
072            if (log.isDebugEnabled()) {
073                log.debug("Restart command: " + StringUtils.join(cmd, " "));
074            }
075            ProcessBuilder pb = new ProcessBuilder(cmd);
076            p1 = pb.start();
077        } catch (IOException e) {
078            log.error("Unable to restart server", e);
079            return false;
080        }
081
082        if (SystemUtils.IS_OS_WINDOWS) {
083            File logPathDir = new File(logPath);
084            File out = new File(logPathDir, "restart-" + System.currentTimeMillis() + ".log");
085            File err = new File(logPathDir, "restart-err-" + System.currentTimeMillis() + ".log");
086            OutputStream fout = null;
087            OutputStream ferr = null;
088            try {
089                fout = new FileOutputStream(out);
090                ferr = new FileOutputStream(err);
091            } catch (Exception e) {
092            }
093            new ThreadedStreamGobbler(p1.getInputStream(), fout).start();
094            new ThreadedStreamGobbler(p1.getErrorStream(), ferr).start();
095        } else {
096            new ThreadedStreamGobbler(p1.getInputStream(), SimpleLog.LOG_LEVEL_OFF).start();
097            new ThreadedStreamGobbler(p1.getErrorStream(), SimpleLog.LOG_LEVEL_ERROR).start();
098        }
099        return true;
100    }
101
102    private static boolean restartInProgress = false;
103
104    private static ConfigurationGenerator cgForRestart;
105
106    public static synchronized boolean restart(Context context) {
107        if (restartInProgress) {
108            return false;
109        }
110        ParamCollector collector = context.getCollector();
111        ConfigurationGenerator cg = collector.getConfigurationGenerator();
112        File nuxeoHome = cg.getNuxeoHome();
113        final String logDir = cg.getLogDir().getPath();
114        final String binPath = new File(nuxeoHome, "bin").getPath();
115        new Thread("restart thread") {
116            @Override
117            public void run() {
118                try {
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    /**
130     * @since 5.6
131     * @return Configured server URL (may differ from current URL)
132     */
133    public static synchronized String getServerURL() {
134        if (cgForRestart == null) {
135            cgForRestart = new ConfigurationGenerator();
136            cgForRestart.init();
137        }
138        return cgForRestart.getUserConfig().getProperty(ConfigurationGenerator.PARAM_NUXEO_URL);
139    }
140}