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