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 * @deprecated since 9.10 - use {@link org.nuxeo.ecm.admin.NuxeoCtlManager#restart()} instead which handles windows OS.
037 */
038@Deprecated
039public class NuxeoRestart {
040
041    public static void restart() throws IOException {
042        List<String> cmd = new ArrayList<String>();
043        String javaHome = System.getProperty("java.home");
044        File java = new File(new File(javaHome), "bin/java").getCanonicalFile();
045        if (java.isFile()) {
046            cmd.add(java.getAbsolutePath());
047        } else { // try java
048            cmd.add("java");
049        }
050        File bundle = Framework.getRuntime().getBundleFile(ReloadComponent.getBundle());
051        cmd.add("-cp");
052        cmd.add(bundle.getAbsolutePath());
053        cmd.add(NuxeoRestart.class.getName());
054        Environment env = Environment.getDefault();
055        if (env.isJBoss()) {
056            String home = System.getProperty("jboss.home.dir");
057            File bin = new File(home, "bin");
058            File file = new File(bin, "nuxeoctl").getCanonicalFile();
059            if (file.isFile()) {
060                cmd.add(file.getAbsolutePath());
061                cmd.add("start");
062            } else {
063                file = new File(bin, "jbossctl").getCanonicalFile();
064                if (file.isFile()) {
065                    cmd.add(file.getAbsolutePath());
066                    cmd.add("start");
067                }
068            }
069        } else if (env.isTomcat()) {
070            String home = System.getProperty("catalina.base");
071            File bin = new File(home, "bin");
072            File file = new File(bin, "nuxeoctl").getCanonicalFile();
073            if (file.isFile()) {
074                cmd.add(file.getAbsolutePath());
075                cmd.add("start");
076            } else {
077                file = new File(bin, "catalina.sh").getCanonicalFile();
078                if (file.isFile()) {
079                    cmd.add(file.getAbsolutePath());
080                    cmd.add("start");
081                }
082            }
083        } else {
084            File file = new File(env.getHome(), "bin/nuxeoctl").getCanonicalFile();
085            if (file.isFile()) {
086                cmd.add(file.getAbsolutePath());
087                cmd.add("start");
088            } else {
089                file = new File(env.getHome(), "nxserverctl.sh").getCanonicalFile();
090                if (file.isFile()) {
091                    cmd.add(file.getAbsolutePath());
092                    cmd.add("start");
093                }
094            }
095        }
096
097        if (cmd.size() <= 1) {
098            throw new FileNotFoundException("Could not find startup script");
099        }
100
101        // create lock file
102        File lock = Framework.createTempFile("nx-restart", ".lock").getCanonicalFile();
103        lock.deleteOnExit();
104        cmd.add(lock.getAbsolutePath());
105
106        new ProcessBuilder().command(cmd).start();
107
108        try {
109            Framework.shutdown();
110        } catch (InterruptedException cause) {
111            Thread.currentThread().interrupt();
112            throw new RuntimeException("Interrupted during shutdown, still proceeding", cause);
113        } finally {
114            System.exit(100); // signal for restart
115        }
116    }
117
118    /**
119     * First argument is the script to run followed by script arguments. The last argument is the lock file.
120     */
121    public static void main(String[] args) throws Exception {
122        if (args.length < 2) {
123            System.out.println("Usage: RestartNuxeo script lock");
124            System.exit(1);
125        }
126        String[] newArgs = new String[args.length - 1];
127        System.arraycopy(args, 0, newArgs, 0, newArgs.length);
128        File lock = new File(args[args.length - 1]);
129        File script = new File(args[0]);
130        boolean ok = false;
131        try {
132            // wait for the lock file to be removed
133            while (lock.isFile()) {
134                Thread.sleep(2000);
135            }
136            Thread.sleep(1000);
137            // start nuxeo
138            Runtime.getRuntime().exec(newArgs, new String[] { "JAVA_HOME=" + System.getProperty("java.home") },
139                    script.getParentFile());
140            ok = true;
141        } finally {
142            if (!ok) {
143                System.exit(2);
144            }
145        }
146    }
147
148}