+    @Option(name = "-h", aliases = "--halt", description = "Halt the Karaf 
container.", required =


I'm not sure what "halt" means in this case. (What is the difference between system:shutdown and system:shutdown -h?) "Halt" just means "stop", so it seems vague whether the Karaf process is being terminated with the -h option, or still running but no longer responsive to subsequent user input, or ?

@Option(name = "-r", aliases = "--reboot", description = "Reboot the Karaf 
container."


OK, but Karaf might be better off without a reboot option. For systems that hold/run applications (Karaf, Tomcat, WebLogic, etc.) an admin IMO should do a clean shutdown followed by an explicit restart from an external script, rather than have it done internally by Karaf and risk the prior shutdown not being completely clean, or the subsequent startup not being initialized as if you restarted manually. So I'm not sure many admins would use -reboot. Just my $0.02.

Regards,
Glen


On 12/01/2011 09:53 AM, [email protected] wrote:
Author: jbonofre
Date: Thu Dec  1 14:53:06 2011
New Revision: 1209110

URL: http://svn.apache.org/viewvc?rev=1209110&view=rev
Log:
[KARAF-1073] Add support of -r (reboot) and -h (halt) options to the 
system:shutdown.
system:shutdown command replaces the dev:reboot command.

Removed:
     karaf/trunk/shell/dev/src/main/java/org/apache/karaf/shell/dev/Restart.java
Modified:
     karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml
     
karaf/trunk/system/commands/src/main/java/org/apache/karaf/system/commands/Shutdown.java
     
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/SystemService.java
     
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/internal/SystemServiceImpl.java
     
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/SystemMBean.java
     
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/internal/SystemMBeanImpl.java

Modified: 
karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml
URL: 
http://svn.apache.org/viewvc/karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml 
(original)
+++ karaf/trunk/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml 
Thu Dec  1 14:53:06 2011
@@ -33,9 +33,6 @@
              <action class="org.apache.karaf.shell.dev.PrintStackTraces" />
          </command>
          <command>
-<action class="org.apache.karaf.shell.dev.Restart" />
-</command>
-<command>
              <action class="org.apache.karaf.shell.dev.SystemProperty" />
          </command>
          <command>

Modified: 
karaf/trunk/system/commands/src/main/java/org/apache/karaf/system/commands/Shutdown.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/system/commands/src/main/java/org/apache/karaf/system/commands/Shutdown.java?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- 
karaf/trunk/system/commands/src/main/java/org/apache/karaf/system/commands/Shutdown.java
 (original)
+++ 
karaf/trunk/system/commands/src/main/java/org/apache/karaf/system/commands/Shutdown.java
 Thu Dec  1 14:53:06 2011
@@ -31,6 +31,15 @@ public class Shutdown extends OsgiComman
      @Option(name = "-f", aliases = "--force", description = "Force the shutdown 
without confirmation message.", required = false, multiValued = false)
      boolean force = false;

+    @Option(name = "-r", aliases = "--reboot", description = "Reboot the Karaf 
container.", required = false, multiValued = false)
+    boolean reboot = false;
+
+    @Option(name = "-h", aliases = "--halt", description = "Halt the Karaf 
container.", required = false, multiValued = false)
+    boolean halt = false;
+
+    @Option(name = "-c", aliases = "--clean", description = "Clean the Karaf 
container (working directory) during reboot.", required = false, multiValued = false)
+    boolean clean = false;
+
      @Argument(name = "time", index = 0, description = "Shutdown after a specified 
delay. The time argument can have different" +
              " formats. First, it can be an abolute time in the format hh:mm, in 
which hh is the hour (1 or 2 digits) and mm" +
              " is the minute of the hour (in two digits). Second, it can be in the 
format +m, in which m is the number of minutes" +
@@ -44,15 +53,24 @@ public class Shutdown extends OsgiComman
      }

      protected Object doExecute() throws Exception {
+
          if (force) {
-            systemService.shutdown(time);
+            if (reboot) {
+                systemService.reboot(time, clean);
+            } else {
+                systemService.halt(time);
+            }
              return null;
          }

          for (; ; ) {
              StringBuffer sb = new StringBuffer();
              String karafName = System.getProperty("karaf.name");
-            System.err.println(String.format("Confirm: shutdown instance %s 
(yes/no): ",karafName));
+            if (reboot) {
+                System.err.println(String.format("Confirm: reboot instance %s 
(yes/no): ",karafName));
+            } else {
+                System.err.println(String.format("Confirm: halt instance %s 
(yes/no): ",karafName));
+            }
              System.err.flush();
              for (; ; ) {
                  int c = session.getKeyboard().read();
@@ -68,7 +86,11 @@ public class Shutdown extends OsgiComman
              }
              String str = sb.toString();
              if (str.equals("yes")) {
-                systemService.shutdown(time);
+                if (reboot) {
+                    systemService.reboot(time, clean);
+                } else {
+                    systemService.halt(time);
+                }
              }
              return null;
          }

Modified: 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/SystemService.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/system/core/src/main/java/org/apache/karaf/system/SystemService.java?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/SystemService.java
 (original)
+++ 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/SystemService.java
 Thu Dec  1 14:53:06 2011
@@ -22,19 +22,37 @@ package org.apache.karaf.system;
  public interface SystemService {

      /**
-     * Shutdown the Karaf container.
+     * Halt the Karaf container.
       */
-    void shutdown() throws Exception;
+    void halt() throws Exception;

      /**
-     * Shutdown the Karaf container.
+     * Halt the Karaf container.
       *
       * @param time shutdown delay. The time argument can have different 
formats.
-     *  First, it can be an abolute time in the format hh:mm, in which hh is 
the hour (1 or 2 digits) and mm
+     *  First, it can be an absolute time in the format hh:mm, in which hh is 
the hour (1 or 2 digits) and mm
       *  is the minute of the hour (in two digits). Second, it can be in the 
format +m, in which m is the number of minutes
       *  to wait. The word now is an alias for +0.
       */
-    void shutdown(String time) throws Exception;
+    void halt(String time) throws Exception;
+
+    /**
+     * Reboot the Karaf container.
+     *
+     * @throws Exception
+     */
+    void reboot() throws Exception;
+
+    /**
+     * Reboot the Karaf container.
+     *
+     * @param time reboot delay. The time argument can have different formats.
+     *  First, it can be an absolute time in the format hh:mm, in which hh is 
the hour (1 or 2 digits) and mm
+     *  is the minute of the hour (in two digits). Second, it can be in the 
format +m, in which m is the number of minutes
+     *  to wait. The word now is an alias for +0.
+     *  @param clean Force a clean restart by deleting the working directory.
+     */
+    void reboot(String time, boolean clean) throws Exception;

      /**
       * Set the system start level.

Modified: 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/internal/SystemServiceImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/system/core/src/main/java/org/apache/karaf/system/internal/SystemServiceImpl.java?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/internal/SystemServiceImpl.java
 (original)
+++ 
karaf/trunk/system/core/src/main/java/org/apache/karaf/system/internal/SystemServiceImpl.java
 Thu Dec  1 14:53:06 2011
@@ -43,38 +43,20 @@ public class SystemServiceImpl implement
          return this.bundleContext;
      }

-    public void shutdown() throws Exception {
-        shutdown(null);
+    public void halt() throws Exception {
+        halt(null);
      }

-    public void shutdown(String time) throws Exception {
-        long sleep = 0;
-        if (time != null) {
-            if (!time.equals("now")) {
-                if (time.startsWith("+")) {
-                    // delay in number of minutes provided
-                    time = time.substring(1);
-                    try {
-                        sleep = Long.parseLong(time) * 60 * 1000;
-                    } catch (Exception e) {
-                        throw new IllegalArgumentException("Time " + time + " is 
not valid");
-                    }
-                } else {
-                    // try to parse the date in hh:mm
-                    String[] strings = time.split(":");
-                    if (strings.length != 2) {
-                        throw new IllegalArgumentException("Time " + time + " is 
not valid");
-                    }
-                    GregorianCalendar currentDate = new GregorianCalendar();
-                    GregorianCalendar shutdownDate = new 
GregorianCalendar(currentDate.get(Calendar.YEAR), 
currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE), 
Integer.parseInt(strings[0]), Integer.parseInt(strings[1]));
-                    if (shutdownDate.before(currentDate)) {
-                        shutdownDate.set(Calendar.DATE, 
shutdownDate.get(Calendar.DATE) + 1);
-                    }
-                    sleep = shutdownDate.getTimeInMillis() - 
currentDate.getTimeInMillis();
-                }
-            }
-        }
-        shutdown(sleep);
+    public void halt(String time) throws Exception {
+        shutdown(timeToSleep(time));
+    }
+
+    public void reboot() throws Exception {
+        reboot(null, false);
+    }
+
+    public void reboot(String time, boolean cleanup) throws Exception {
+        reboot(timeToSleep(time), true);
      }

      private void shutdown(final long sleep) {
@@ -83,12 +65,32 @@ public class SystemServiceImpl implement
                  try {
                      if (sleep>  0) {
                          LOGGER.info("Shutdown in " + sleep / 1000 / 60 + " 
minute(s)");
+                        System.err.println("Shutdown in " + sleep / 1000 / 60 + " 
minutes(s)");
                      }
                      Thread.sleep(sleep);
                      Bundle bundle = getBundleContext().getBundle(0);
                      bundle.stop();
                  } catch (Exception e) {
-                    LOGGER.error("Error when shutting down", e);
+                    LOGGER.error("Halt error", e);
+                }
+            }
+        }.start();
+    }
+
+    private void reboot(final long sleep, final boolean clean) {
+        new Thread() {
+            public void run() {
+                try {
+                    if (sleep>  0) {
+                        LOGGER.info("Reboot in " + sleep / 1000 / 60 + " 
minute(s)");
+                        System.err.println("Reboot in " + sleep / 1000 / 60 + " 
minute(s)");
+                    }
+                    Thread.sleep(sleep);
+                    System.setProperty("karaf.restart", "true");
+                    System.setProperty("karaf.restart.clean", 
Boolean.toString(clean));
+                    bundleContext.getBundle(0).stop();
+                } catch (Exception e) {
+                    LOGGER.error("Reboot error", e);
                  }
              }
          }.start();
@@ -128,4 +130,40 @@ public class SystemServiceImpl implement
          }
      }

+    /**
+     * Convert a time string to sleep period (in millisecond).
+     *
+     * @param time the time string.
+     * @return the corresponding sleep period in millisecond.
+     */
+    private long timeToSleep(String time) throws Exception {
+        long sleep = 0;
+        if (time != null) {
+            if (!time.equals("now")) {
+                if (time.startsWith("+")) {
+                    // delay in number of minutes provided
+                    time = time.substring(1);
+                    try {
+                        sleep = Long.parseLong(time) * 60 * 1000;
+                    } catch (Exception e) {
+                        throw new IllegalArgumentException("Time " + time + " is 
not valid");
+                    }
+                } else {
+                    // try to parse the date in hh:mm
+                    String[] strings = time.split(":");
+                    if (strings.length != 2) {
+                        throw new IllegalArgumentException("Time " + time + " is 
not valid");
+                    }
+                    GregorianCalendar currentDate = new GregorianCalendar();
+                    GregorianCalendar shutdownDate = new 
GregorianCalendar(currentDate.get(Calendar.YEAR), 
currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DATE), 
Integer.parseInt(strings[0]), Integer.parseInt(strings[1]));
+                    if (shutdownDate.before(currentDate)) {
+                        shutdownDate.set(Calendar.DATE, 
shutdownDate.get(Calendar.DATE) + 1);
+                    }
+                    sleep = shutdownDate.getTimeInMillis() - 
currentDate.getTimeInMillis();
+                }
+            }
+        }
+        return sleep;
+    }
+
  }

Modified: 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/SystemMBean.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/SystemMBean.java?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/SystemMBean.java
 (original)
+++ 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/SystemMBean.java
 Thu Dec  1 14:53:06 2011
@@ -21,8 +21,10 @@ package org.apache.karaf.system.manageme
   */
  public interface SystemMBean {

-    void shutdown() throws Exception;
-    void shutdown(String time) throws Exception;
+    void halt() throws Exception;
+    void halt(String time) throws Exception;
+    void reboot() throws Exception;
+    void reboot(String time, boolean clean) throws Exception;

      void setStartLevel(int startLevel) throws Exception;
      int getStartLevel() throws Exception;

Modified: 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/internal/SystemMBeanImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/internal/SystemMBeanImpl.java?rev=1209110&r1=1209109&r2=1209110&view=diff
==============================================================================
--- 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/internal/SystemMBeanImpl.java
 (original)
+++ 
karaf/trunk/system/management/src/main/java/org/apache/karaf/system/management/internal/SystemMBeanImpl.java
 Thu Dec  1 14:53:06 2011
@@ -41,12 +41,20 @@ public class SystemMBeanImpl extends Sta
          return this.systemService;
      }

-    public void shutdown() throws Exception {
-        systemService.shutdown();
+    public void halt() throws Exception {
+        systemService.halt();
      }

-    public void shutdown(String time) throws Exception {
-        systemService.shutdown(time);
+    public void halt(String time) throws Exception {
+        systemService.halt(time);
+    }
+
+    public void reboot() throws Exception {
+        systemService.reboot();
+    }
+
+    public void reboot(String time, boolean clean) throws Exception {
+        systemService.reboot(time, clean);
      }

      public void setStartLevel(int startLevel) throws Exception {




--
Glen Mazza
Talend Community Coders
http://coders.talend.com
blog: http://www.jroller.com/gmazza

Reply via email to