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