Author: jbonofre
Date: Tue Sep 18 12:28:18 2012
New Revision: 1387114

URL: http://svn.apache.org/viewvc?rev=1387114&view=rev
Log:
[KARAF-1825] Add startLevel attribute and rename the others operation for 
migration purpose

Modified:
    
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/DevMBean.java
    
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/DevMBeanImpl.java
    karaf/branches/karaf-2.3.x/management/mbeans/system/pom.xml
    
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/SystemMBean.java
    
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java

Modified: 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/DevMBean.java
URL: 
http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/DevMBean.java?rev=1387114&r1=1387113&r2=1387114&view=diff
==============================================================================
--- 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/DevMBean.java
 (original)
+++ 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/DevMBean.java
 Tue Sep 18 12:28:18 2012
@@ -13,8 +13,6 @@
  */
 package org.apache.karaf.management.mbeans.dev;
 
-import java.util.List;
-
 /**
  * MBean providing dev actions.
  */
@@ -23,6 +21,7 @@ public interface DevMBean {
     /**
      * Get the current OSGi framework in use.
      *
+     * @deprecated use SystemMBean#getFramework() instead.
      * @return the name of the OSGi framework in use.
      * @throws Exception
      */
@@ -31,6 +30,7 @@ public interface DevMBean {
     /**
      * OSGi framework options.
      *
+     * @deprecated use SystemMBean#setFrameworkDebug() instead.
      * @param debug enable debug of the OSGi framework to use.
      * @param framework name of the OSGI framework to use.
      * @throws Exception
@@ -40,6 +40,7 @@ public interface DevMBean {
     /**
      * Restart Karaf, with eventually a cleanup.
      *
+     * @deprecated use SystemMBean#reboot() instead.
      * @param clean if true, Karaf is cleanup, false else.
      * @throws Exception
      */

Modified: 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/DevMBeanImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/DevMBeanImpl.java?rev=1387114&r1=1387113&r2=1387114&view=diff
==============================================================================
--- 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/DevMBeanImpl.java
 (original)
+++ 
karaf/branches/karaf-2.3.x/management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/DevMBeanImpl.java
 Tue Sep 18 12:28:18 2012
@@ -55,24 +55,19 @@ public class DevMBeanImpl extends Standa
     public void frameworkOptions(boolean debug, String framework) throws 
Exception {
         Properties properties = new Properties(new 
File(System.getProperty("karaf.base"), "etc/config.properties"));
         if (framework != null) {
-            // swtich the framework is use
+            // switch the framework is use
             if (!framework.equalsIgnoreCase("felix") && 
!framework.equalsIgnoreCase("equinox")) {
                 throw new IllegalArgumentException("Unsupported framework " + 
framework);
             }
             properties.put("karaf.framework", framework.toLowerCase());
         }
-        if (framework == null) {
-            if 
(bundleContext.getBundle(0).getSymbolicName().contains("felix")) {
-                framework = "felix";
-            } else {
-                framework = "equinox";
-            }
-        }
-        if (framework.equals("felix")) {
+        if (debug) {
             properties.put("felix.log.level", "4");
-        } else {
             properties.put("osgi.debug", "etc/equinox-debug.properties");
             // TODO populate the equinox-debug.properties file with the one 
provided in shell/dev module
+        } else {
+            properties.remove("felix.log.level");
+            properties.remove("osgi.debug");
         }
         properties.save();
     }

Modified: karaf/branches/karaf-2.3.x/management/mbeans/system/pom.xml
URL: 
http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/management/mbeans/system/pom.xml?rev=1387114&r1=1387113&r2=1387114&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/management/mbeans/system/pom.xml (original)
+++ karaf/branches/karaf-2.3.x/management/mbeans/system/pom.xml Tue Sep 18 
12:28:18 2012
@@ -44,6 +44,12 @@
         </dependency>
 
         <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <scope>provided</scope>
+        </dependency>
+
+        <dependency>
             <groupId>org.apache.felix</groupId>
             <artifactId>org.apache.felix.utils</artifactId>
             <scope>provided</scope>

Modified: 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/SystemMBean.java
URL: 
http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/SystemMBean.java?rev=1387114&r1=1387113&r2=1387114&view=diff
==============================================================================
--- 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/SystemMBean.java
 (original)
+++ 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/SystemMBean.java
 Tue Sep 18 12:28:18 2012
@@ -21,11 +21,98 @@ package org.apache.karaf.management.mbea
  */
 public interface SystemMBean {
 
+    /**
+     * Shutdown the Karaf instance.
+     *
+     * @throws Exception
+     */
+    void halt() throws Exception;
+
+    /**
+     * Shutdown the Karaf instance at a certain time.
+     *
+     * @param time the time when to shutdown the Karaf instance.
+     * @throws Exception
+     */
+    void halt(String time) throws Exception;
+
+    /**
+     * Reboot the Karaf instance.
+     *
+     * @throws Exception
+     */
+    void reboot() throws Exception;
+
+    /**
+     * Reboot the Karaf instance at a certain time and eventually cleanup the 
instance files.
+     *
+     * @param time the time when to reboot the Karaf instance.
+     * @param clean true to clean the instance files, false else.
+     * @throws Exception
+     */
+    void reboot(String time, boolean clean) throws Exception;
+
+    /**
+     * Get the name of the Karaf instance.
+     *
+     * @return the Karaf instance name.
+     */
     String getName();
+
+    /**
+     * Set the name of the Karaf instance.
+     *
+     * @param name the new Karaf instance name. NB: the change require an 
instance reboot.
+     */
     void setName(String name);
 
+    /**
+     * Get the Karaf version used by the Karaf instance.
+     *
+     * @return the Karaf version.
+     */
     String getVersion();
 
+    /**
+     * Get the current OSGi framework in use.
+     *
+     * @return the name of the OSGi framework in use.
+     */
+    String getFramework();
+
+    /**
+     * Change the OSGi framework to run Karaf.
+     *
+     * @param framework the name of the framework to use (felix or equinox).
+     */
+    void setFramework(String framework) throws Exception;
+
+    /**
+     * Enable or disable the debug option on the OSGi framework.
+     *
+     * @param debug true to enable debug, false else.
+     */
+    void setFrameworkDebug(boolean debug) throws Exception;
+
+    /**
+     * Get the system start level.
+     *
+     * @return the current system start level.
+     */
+    int getStartLevel();
+
+    /**
+     * Set the system start level.
+     *
+     * @param startLevel the new system start level.
+     */
+    void setStartLevel(int startLevel);
+
+    /* for backward compatibility */
+
+    /**
+     * @deprecated use halt() instead.
+     */
     void shutdown() throws Exception;
 
 }

Modified: 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java
URL: 
http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java?rev=1387114&r1=1387113&r2=1387114&view=diff
==============================================================================
--- 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java
 (original)
+++ 
karaf/branches/karaf-2.3.x/management/mbeans/system/src/main/java/org/apache/karaf/management/mbeans/system/internal/SystemMBeanImpl.java
 Tue Sep 18 12:28:18 2012
@@ -19,18 +19,25 @@ package org.apache.karaf.management.mbea
 import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.management.mbeans.system.SystemMBean;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.startlevel.FrameworkStartLevel;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import javax.management.NotCompliantMBeanException;
 import javax.management.StandardMBean;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
+import java.util.Calendar;
+import java.util.GregorianCalendar;
 
 /**
  * System MBean implementation.
  */
 public class SystemMBeanImpl extends StandardMBean implements SystemMBean {
 
+    private final static transient Logger LOGGER = 
LoggerFactory.getLogger(SystemMBeanImpl.class);
+
     private BundleContext bundleContext;
 
     public SystemMBeanImpl() throws NotCompliantMBeanException {
@@ -63,8 +70,66 @@ public class SystemMBeanImpl extends Sta
         return System.getProperty("karaf.version");
     }
 
+    public void halt() throws Exception {
+        halt(null);
+    }
+
+    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), cleanup);
+    }
+
+    public void setFrameworkDebug(boolean debug) throws Exception {
+        Properties properties = new Properties(new 
File(System.getProperty("karaf.base"), "etc/config.properties"));
+        if (debug) {
+            properties.put("felix.log.level", "4");
+            properties.put("osgi.debug", "etc/equinox-debug.properties");
+            // TODO populate the equinox-debug.properties file with the one 
provided in shell/dev module
+        } else {
+            properties.remove("felix.log.level");
+            properties.remove("osgi.debug");
+        }
+        properties.save();
+    }
+
+    public String getFramework() {
+        if (bundleContext.getBundle(0).getSymbolicName().contains("felix")) {
+            return "felix";
+        } else {
+            return "equinox";
+        }
+    }
+
+    public void setFramework(String framework) throws Exception {
+        Properties properties = new Properties(new 
File(System.getProperty("karaf.base"), "etc/config.properties"));
+        if (!framework.equals("felix") || !framework.equals("equinox"))
+            throw new IllegalArgumentException("Framework name is not 
supported. Only felix or equinox are supported.");
+        properties.put("karaf.framework", framework);
+        properties.save();
+    }
+
+    public void setStartLevel(int startLevel) {
+        
getBundleContext().getBundle(0).adapt(FrameworkStartLevel.class).setStartLevel(startLevel,
 null);
+    }
+
+    public int getStartLevel() {
+        return 
getBundleContext().getBundle(0).adapt(FrameworkStartLevel.class).getStartLevel();
+    }
+
+    /* for backward compatibility */
+
+    /**
+     * @deprecated use halt() instead.
+     */
     public void shutdown() throws Exception {
-        bundleContext.getBundle(0).stop();
+        halt();
     }
 
     public BundleContext getBundleContext() {
@@ -75,4 +140,77 @@ public class SystemMBeanImpl extends Sta
         this.bundleContext = bundleContext;
     }
 
+    private void shutdown(final long sleep) {
+        new Thread() {
+            public void run() {
+                try {
+                    sleepWithMsg(sleep, "Shutdown in " + sleep / 1000 / 60 + " 
minute(s)");
+                    getBundleContext().getBundle(0).stop();
+                } catch (Exception e) {
+                    LOGGER.error("Halt error", e);
+                }
+            }
+        }.start();
+    }
+
+    private void reboot(final long sleep, final boolean clean) {
+        new Thread() {
+            public void run() {
+                try {
+                    sleepWithMsg(sleep, "Reboot in " + sleep / 1000 / 60 + " 
minute(s)");
+                    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();
+    }
+
+    private void sleepWithMsg(final long sleep, String msg)
+            throws InterruptedException {
+        if (sleep > 0) {
+            LOGGER.info(msg);
+            System.err.println(msg);
+        }
+        Thread.sleep(sleep);
+    }
+
+    /**
+     * 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;
+    }
+
 }


Reply via email to