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.FileOutputStream; 024import java.io.IOException; 025import java.io.OutputStream; 026 027import org.apache.commons.lang3.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; 032import org.nuxeo.common.Environment; 033import org.nuxeo.launcher.config.ConfigurationGenerator; 034import org.nuxeo.log4j.ThreadedStreamGobbler; 035import org.nuxeo.runtime.api.Framework; 036 037/** 038 * Helper class to call NuxeoCtl restart. 039 * 040 * @author Tiry (tdelprat@nuxeo.com) 041 */ 042public class NuxeoCtlManager { 043 044 protected static final String CMD_POSIX = "nuxeoctl"; 045 046 protected static final String CMD_WIN = "nuxeoctl.bat"; 047 048 protected static final Log log = LogFactory.getLog(NuxeoCtlManager.class); 049 050 private ConfigurationGenerator cg; 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; 089 OutputStream ferr; 090 try { 091 fout = new FileOutputStream(out); 092 ferr = new FileOutputStream(err); 093 } catch (IOException e) { 094 throw new RuntimeException(e); 095 } 096 new ThreadedStreamGobbler(p1.getInputStream(), fout).start(); 097 new ThreadedStreamGobbler(p1.getErrorStream(), ferr).start(); 098 } else { 099 new ThreadedStreamGobbler(p1.getInputStream(), SimpleLog.LOG_LEVEL_OFF).start(); 100 new ThreadedStreamGobbler(p1.getErrorStream(), SimpleLog.LOG_LEVEL_ERROR).start(); 101 } 102 return true; 103 } 104 105 private static boolean restartInProgress = false; 106 107 public static synchronized boolean restart() { 108 if (restartInProgress) { 109 return false; 110 } 111 restartInProgress = true; 112 String nuxeoHome = Framework.getProperty(Environment.NUXEO_HOME); 113 final String binPath = new File(nuxeoHome, "bin").getPath(); 114 final String logDir = Framework.getProperty(Environment.NUXEO_LOG_DIR, nuxeoHome); 115 new Thread("restart thread") { 116 @Override 117 public void run() { 118 try { 119 log.info("Restarting Nuxeo server"); 120 Thread.sleep(3000); 121 doExec(binPath, logDir); 122 } catch (InterruptedException e) { 123 log.error("Restart failed", e); 124 } 125 } 126 }.start(); 127 return true; 128 } 129 130 public String restartServer() { 131 restart(); 132 return "Nuxeo server is restarting"; 133 } 134 135 /** 136 * @since 5.6 137 * @return Configured server URL (may differ from current URL) 138 */ 139 public String getServerURL() { 140 if (cg == null) { 141 cg = new ConfigurationGenerator(); 142 cg.init(); 143 } 144 return cg.getUserConfig().getProperty(ConfigurationGenerator.PARAM_NUXEO_URL); 145 } 146 147}