001/* 002 * (C) Copyright 2006-2010 Nuxeo SAS (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.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 * bstefanescu 016 */ 017package org.nuxeo.runtime.reload; 018 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.IOException; 022import java.util.ArrayList; 023import java.util.List; 024 025import org.nuxeo.common.Environment; 026import org.nuxeo.runtime.api.Framework; 027 028/** 029 * Restart a Nuxeo. For now works only on Unix systems. 030 * <p> 031 * Usage: <code>NuxeoRestart.restart()</code> 032 * 033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 034 */ 035public class NuxeoRestart { 036 037 public static void restart() throws IOException { 038 List<String> cmd = new ArrayList<String>(); 039 String javaHome = System.getProperty("java.home"); 040 File java = new File(new File(javaHome), "bin/java").getCanonicalFile(); 041 if (java.isFile()) { 042 cmd.add(java.getAbsolutePath()); 043 } else { // try java 044 cmd.add("java"); 045 } 046 File bundle = Framework.getRuntime().getBundleFile(ReloadComponent.getBundle()); 047 cmd.add("-cp"); 048 cmd.add(bundle.getAbsolutePath()); 049 cmd.add(NuxeoRestart.class.getName()); 050 Environment env = Environment.getDefault(); 051 if (env.isJBoss()) { 052 String home = System.getProperty("jboss.home.dir"); 053 File bin = new File(home, "bin"); 054 File file = new File(bin, "nuxeoctl").getCanonicalFile(); 055 if (file.isFile()) { 056 cmd.add(file.getAbsolutePath()); 057 cmd.add("start"); 058 } else { 059 file = new File(bin, "jbossctl").getCanonicalFile(); 060 if (file.isFile()) { 061 cmd.add(file.getAbsolutePath()); 062 cmd.add("start"); 063 } 064 } 065 } else if (env.isTomcat()) { 066 String home = System.getProperty("catalina.base"); 067 File bin = new File(home, "bin"); 068 File file = new File(bin, "nuxeoctl").getCanonicalFile(); 069 if (file.isFile()) { 070 cmd.add(file.getAbsolutePath()); 071 cmd.add("start"); 072 } else { 073 file = new File(bin, "catalina.sh").getCanonicalFile(); 074 if (file.isFile()) { 075 cmd.add(file.getAbsolutePath()); 076 cmd.add("start"); 077 } 078 } 079 } else { 080 File file = new File(env.getHome(), "bin/nuxeoctl").getCanonicalFile(); 081 if (file.isFile()) { 082 cmd.add(file.getAbsolutePath()); 083 cmd.add("start"); 084 } else { 085 file = new File(env.getHome(), "nxserverctl.sh").getCanonicalFile(); 086 if (file.isFile()) { 087 cmd.add(file.getAbsolutePath()); 088 cmd.add("start"); 089 } 090 } 091 } 092 093 if (cmd.size() <= 1) { 094 throw new FileNotFoundException("Could not find startup script"); 095 } 096 097 // create lock file 098 File lock = File.createTempFile("nx-restart", ".lock").getCanonicalFile(); 099 lock.deleteOnExit(); 100 cmd.add(lock.getAbsolutePath()); 101 102 new ProcessBuilder().command(cmd).start(); 103 104 try { 105 Framework.shutdown(); 106 } finally { 107 System.exit(100); // signal for restart 108 } 109 } 110 111 /** 112 * First argument is the script to run followed by script arguments. The last argument is the lock file. 113 */ 114 public static void main(String[] args) throws Exception { 115 if (args.length < 2) { 116 System.out.println("Usage: RestartNuxeo script lock"); 117 System.exit(1); 118 } 119 String[] newArgs = new String[args.length - 1]; 120 System.arraycopy(args, 0, newArgs, 0, newArgs.length); 121 File lock = new File(args[args.length - 1]); 122 File script = new File(args[0]); 123 boolean ok = false; 124 try { 125 // wait for the lock file to be removed 126 while (lock.isFile()) { 127 Thread.sleep(2000); 128 } 129 Thread.sleep(1000); 130 // start nuxeo 131 Runtime.getRuntime().exec(newArgs, new String[] { "JAVA_HOME=" + System.getProperty("java.home") }, 132 script.getParentFile()); 133 ok = true; 134 } finally { 135 if (!ok) { 136 System.exit(2); 137 } 138 } 139 } 140 141}