Repository: karaf Updated Branches: refs/heads/karaf-2.x 9380e0669 -> 03604511a
[KARAF-2996] Improve shutdown sleep time management Project: http://git-wip-us.apache.org/repos/asf/karaf/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/03604511 Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/03604511 Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/03604511 Branch: refs/heads/karaf-2.x Commit: 03604511aa75999eeba8f629c66b3118904ebdbd Parents: 9380e06 Author: Jean-Baptiste Onofré <[email protected]> Authored: Thu Aug 7 11:12:23 2014 +0200 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Thu Aug 7 11:12:23 2014 +0200 ---------------------------------------------------------------------- .../mbeans/system/internal/SystemMBeanImpl.java | 31 ++++++++++------ .../src/main/webapp/users-guide/start-stop.conf | 7 +++- .../org/apache/karaf/shell/osgi/Shutdown.java | 39 +++++++++++++------- 3 files changed, 50 insertions(+), 27 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf/blob/03604511/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java ---------------------------------------------------------------------- diff --git a/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java b/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java index b4fb8eb..386d419 100644 --- a/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java +++ b/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java @@ -191,26 +191,35 @@ public class SystemMBeanImpl extends StandardMBean implements SystemMBean { 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 { + if (time.contains(":")) { // try to parse the date in hh:mm String[] strings = time.split(":"); if (strings.length != 2) { - throw new IllegalArgumentException("Time " + time + " is not valid"); + throw new IllegalArgumentException("Time " + time + " is not valid (not in hh:mm format)"); + } + int hour = Integer.parseInt(strings[0]); + int minute = Integer.parseInt(strings[1]); + if (hour < 0 || hour > 23) { + throw new IllegalArgumentException("Time " + time + " is not valid (hour " + hour + " is not between 0 and 23)"); + } + if (minute < 0 || minute > 59) { + throw new IllegalArgumentException("Time " + time + " is not valid (minute " + minute + " is not between 0 and 59)"); } 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])); + GregorianCalendar shutdownDate = new GregorianCalendar(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH), hour, minute); if (shutdownDate.before(currentDate)) { shutdownDate.set(Calendar.DATE, shutdownDate.get(Calendar.DATE) + 1); } sleep = shutdownDate.getTimeInMillis() - currentDate.getTimeInMillis(); + } else { + if (time.startsWith("+")) { + time = time.substring(1); + } + try { + sleep = Long.parseLong(time) * 60 * 1000; + } catch (Exception e) { + throw new IllegalArgumentException("Time " + time + " is not valid"); + } } } } http://git-wip-us.apache.org/repos/asf/karaf/blob/03604511/manual/src/main/webapp/users-guide/start-stop.conf ---------------------------------------------------------------------- diff --git a/manual/src/main/webapp/users-guide/start-stop.conf b/manual/src/main/webapp/users-guide/start-stop.conf index 807daf4..c01141f 100644 --- a/manual/src/main/webapp/users-guide/start-stop.conf +++ b/manual/src/main/webapp/users-guide/start-stop.conf @@ -100,7 +100,10 @@ The shutdown command asks you to confirm that you really want to shutdown. If yo system:shutdown -f {code} -It's also possible to delay the shutdown using the time argument. 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 work now is an alias for +0. +It's also possible to delay the shutdown using the time argument. 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 (or +m), in which m is the number of minutes to wait. +The word now is an alias for 0. The following command will shutdown Karaf at 10:35am: @@ -111,7 +114,7 @@ system:shutdown 10:35 The following command will shutdown Karaf in 10 minutes: {code} -system:shutdown +10 +system:shutdown 10 {code} If you're running from the main console, exiting the shell using {{logout}} or {{Ctrl+D}} will also terminate the Karaf instance. http://git-wip-us.apache.org/repos/asf/karaf/blob/03604511/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/Shutdown.java ---------------------------------------------------------------------- diff --git a/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/Shutdown.java b/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/Shutdown.java index 1edd022..90301b3 100644 --- a/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/Shutdown.java +++ b/shell/osgi/src/main/java/org/apache/karaf/shell/osgi/Shutdown.java @@ -36,8 +36,8 @@ public class Shutdown extends OsgiCommandSupport { @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" + - " to wait. The word now is an alias for +0.", required = false, multiValued = false) + " is the minute of the hour (in two digits). Second, it can be in the format m (or +m), in which m is the number of minutes" + + " to wait. The word now is an alias for 0.", required = false, multiValued = false) String time; protected Object doExecute() throws Exception { @@ -45,28 +45,39 @@ public class Shutdown extends OsgiCommandSupport { 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) { - System.err.println("Invalid time argument."); - return null; - } - } else { + if (time.contains(":")) { // try to parse the date in hh:mm String[] strings = time.split(":"); if (strings.length != 2) { - System.err.println("Invalid time argument."); + System.err.println("Time " + time + " is not valid (not in hh:mm format)"); + return null; + } + int hour = Integer.parseInt(strings[0]); + int minute = Integer.parseInt(strings[1]); + if (hour < 0 || hour > 23) { + System.err.println("Time " + time + " is not valid (hour " + hour + " is not between 0 and 23)"); + return null; + } + if (minute < 0 || minute > 59) { + System.err.println("Time " + time + " is not valid (minute " + minute + " is not between 0 and 59)"); return null; } 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])); + GregorianCalendar shutdownDate = new GregorianCalendar(currentDate.get(Calendar.YEAR), currentDate.get(Calendar.MONTH), currentDate.get(Calendar.DAY_OF_MONTH), hour, minute); if (shutdownDate.before(currentDate)) { shutdownDate.set(Calendar.DATE, shutdownDate.get(Calendar.DATE) + 1); } sleep = shutdownDate.getTimeInMillis() - currentDate.getTimeInMillis(); + } else { + if (time.startsWith("+")) { + time = time.substring(1); + } + try { + sleep = Long.parseLong(time) * 60 * 1000; + } catch (Exception e) { + System.err.println("Time " + time + " is not valid"); + return null; + } } } }
