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.lang3.SystemUtils; 028import org.apache.commons.logging.Log; 029import org.apache.commons.logging.LogFactory; 030import org.apache.commons.logging.impl.SimpleLog; 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 ([email protected]) 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: " + String.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 Thread.currentThread().interrupt(); 123 throw new RuntimeException(e); 124 } 125 } 126 }.start(); 127 return true; 128 } 129 130 /** 131 * @since 5.6 132 * @return Configured server URL (may differ from current URL) 133 */ 134 public static synchronized String getServerURL() { 135 if (cgForRestart == null) { 136 cgForRestart = new ConfigurationGenerator(); 137 cgForRestart.init(); 138 } 139 return cgForRestart.getUserConfig().getProperty(ConfigurationGenerator.PARAM_NUXEO_URL); 140 } 141}