karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbono...@apache.org
Subject svn commit: r1539080 - in /karaf/branches/karaf-2.3.x: management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/ management/mbeans/dev/src/main/java/org/apache/karaf/management/mbeans/dev/internal/ shell/dev/src/main/java/org/apache/k...
Date Tue, 05 Nov 2013 17:32:25 GMT
Author: jbonofre
Date: Tue Nov  5 17:32:24 2013
New Revision: 1539080

URL: http://svn.apache.org/r1539080
Log:
[KARAF-2474] Improve the DevMBean with system properties operations

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/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.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=1539080&r1=1539079&r2=1539080&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 Nov  5 17:32:24 2013
@@ -13,6 +13,8 @@
  */
 package org.apache.karaf.management.mbeans.dev;
 
+import java.util.Map;
+
 /**
  * MBean providing dev actions.
  */
@@ -46,4 +48,29 @@ public interface DevMBean {
      */
     void restart(boolean clean) throws Exception;
 
+    /**
+     * Get system (and eventually OSGi) properties.
+     *
+     * @param unset if true, show the OSGi properties even if unset.
+     * @param dumpToFile if true, dump system properties into a file (in the data folder)
+     * @return the system properties
+     */
+    Map getProperties(boolean unset, boolean dumpToFile) throws Exception;
+
+    /**
+     * Get the value of a property.
+     * @param key the system property key.
+     * @return the system property value.
+     */
+    String getProperty(String key);
+
+    /**
+     * Set the value of a system property.
+     *
+     * @param key the system property key.
+     * @param value the system property value.
+     * @param persistent if true, persist the new value to the etc/system.properties file.
+     */
+    void setProperty(String key, String value, boolean persistent) 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=1539080&r1=1539079&r2=1539080&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 Nov  5 17:32:24 2013
@@ -16,14 +16,14 @@ package org.apache.karaf.management.mbea
 import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.management.mbeans.dev.DevMBean;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
 
 import javax.management.NotCompliantMBeanException;
 import javax.management.StandardMBean;
 import java.io.File;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.PrintWriter;
-import java.util.Scanner;
+import java.io.PrintStream;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 /**
  * Implementation of the DevMBean.
@@ -78,4 +78,108 @@ public class DevMBeanImpl extends Standa
         bundleContext.getBundle(0).stop();
     }
 
+    public Map getProperties(boolean unset, boolean dumpToFile) throws Exception {
+        Map<String, String> result = new HashMap<String, String>();
+
+        java.util.Properties props = (java.util.Properties) System.getProperties().clone();
+
+        String def = null;
+        if (unset) {
+            def = "unset";
+        }
+
+        setProperty(props, Constants.FRAMEWORK_BEGINNING_STARTLEVEL, def);
+        setProperty(props, Constants.FRAMEWORK_BOOTDELEGATION, def);
+        setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT, def);
+        setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_APP, def);
+        setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_BOOT, def);
+        setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_EXT, def);
+        setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK, def);
+        setProperty(props, Constants.FRAMEWORK_EXECPERMISSION, def);
+        setProperty(props, Constants.FRAMEWORK_EXECUTIONENVIRONMENT, def);
+        setProperty(props, Constants.FRAMEWORK_LANGUAGE, def);
+        setProperty(props, Constants.FRAMEWORK_LIBRARY_EXTENSIONS, def);
+        setProperty(props, Constants.FRAMEWORK_OS_NAME, def);
+        setProperty(props, Constants.FRAMEWORK_OS_VERSION, def);
+        setProperty(props, Constants.FRAMEWORK_PROCESSOR, def);
+        setProperty(props, Constants.FRAMEWORK_SECURITY, def);
+        setProperty(props, Constants.FRAMEWORK_SECURITY_OSGI, def);
+        setProperty(props, Constants.FRAMEWORK_STORAGE, def);
+        setProperty(props, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT, def);
+        setProperty(props, Constants.FRAMEWORK_SYSTEMPACKAGES, def);
+        setProperty(props, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, def);
+        setProperty(props, Constants.FRAMEWORK_VENDOR, def);
+        setProperty(props, Constants.FRAMEWORK_VERSION, def);
+        setProperty(props, Constants.FRAMEWORK_WINDOWSYSTEM, def);
+
+        setProperty(props, Constants.SUPPORTS_BOOTCLASSPATH_EXTENSION, def);
+        setProperty(props, Constants.SUPPORTS_FRAMEWORK_EXTENSION, def);
+        setProperty(props, Constants.SUPPORTS_FRAMEWORK_FRAGMENT, def);
+        setProperty(props, Constants.SUPPORTS_FRAMEWORK_REQUIREBUNDLE, def);
+
+        if (dumpToFile) {
+            PrintStream ps = new PrintStream(new File(bundleContext.getProperty("karaf.data"),
"dump-properties-" + java.lang.System.currentTimeMillis() + ".properties"));
+            ps.println("#Dump of the System and OSGi properties");
+            ps.println("#Dump execute at " + new SimpleDateFormat().format(new Date()));
+            printOrderedProperties(props, ps);
+            ps.flush();
+            ps.close();
+        } else {
+            printOrderedProperties(props, result);
+        }
+
+        return result;
+    }
+
+    private void printOrderedProperties(java.util.Properties props, PrintStream out) {
+        Set<Object> keys = props.keySet();
+        Vector<String> order = new Vector<String>(keys.size());
+        for (Iterator<Object> i = keys.iterator(); i.hasNext(); ) {
+            Object str = (Object) i.next();
+            order.add((String) str);
+        }
+        Collections.sort(order);
+        for (Iterator<String> i = order.iterator(); i.hasNext(); ) {
+            String key = (String) i.next();
+            out.println(key + "=" + props.getProperty(key));
+        }
+    }
+
+    private void printOrderedProperties(java.util.Properties props, Map<String, String>
result) {
+        Set<Object> keys = props.keySet();
+        Vector<String> order = new Vector<String>(keys.size());
+        for (Iterator<Object> i = keys.iterator(); i.hasNext(); ) {
+            Object str = (Object) i.next();
+            order.add((String) str);
+        }
+        Collections.sort(order);
+        for (Iterator<String> i = order.iterator(); i.hasNext(); ) {
+            String key = (String) i.next();
+            result.put(key, props.getProperty(key));
+        }
+    }
+
+    private void setProperty(java.util.Properties props, String key, String def) {
+        String val = bundleContext.getProperty(key);
+        if (val == null && def != null) {
+            props.setProperty(key, def);
+        } else if (val != null) {
+            props.setProperty(key, val);
+        }
+    }
+
+    public String getProperty(String key) {
+        return System.getProperty(key);
+    }
+
+    public void setProperty(String key, String value, boolean persistent) throws Exception
{
+        if (persistent) {
+            String base = System.getProperty("karaf.base");
+            Properties props = new Properties(new File(base, "etc/system.properties"));
+            props.put(key, value);
+            props.save();
+        }
+        System.setProperty(key, value);
+    }
+
 }

Modified: karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java?rev=1539080&r1=1539079&r2=1539080&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java
(original)
+++ karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java
Tue Nov  5 17:32:24 2013
@@ -17,12 +17,16 @@
 package org.apache.karaf.shell.dev;
 
 import java.io.File;
+import java.io.PrintStream;
+import java.text.SimpleDateFormat;
+import java.util.*;
 
 import org.apache.felix.gogo.commands.Argument;
 import org.apache.felix.gogo.commands.Command;
 import org.apache.felix.gogo.commands.Option;
 import org.apache.felix.utils.properties.Properties;
 import org.apache.karaf.shell.console.OsgiCommandSupport;
+import org.osgi.framework.Constants;
 
 /**
  * Command that allow access to system properties easily.
@@ -30,10 +34,16 @@ import org.apache.karaf.shell.console.Os
 @Command(scope = "dev", name = "system-property", description = "Get or set a system property.")
 public class SystemProperty extends OsgiCommandSupport {
 
-    @Option(name = "-p", aliases = { "--persistent" }, description = "Persist the new value
to the etc/system.properties file")
+    @Option(name = "-p", aliases = {"--persistent"}, description = "Persist the new value
to the etc/system.properties file")
     boolean persistent;
 
-    @Argument(index = 0, name = "key", description = "The system property name")
+    @Option(name = "-f", aliases = {"--file-dump"}, description = "Dump system properties
into a file (in the data folder)")
+    boolean dumpToFile;
+
+    @Option(name = "-u", aliases = {"--show-unset"}, description = "Show the OSGi properties
even if unset")
+    boolean unset;
+
+    @Argument(index = 0, name = "key", required = false, description = "The system property
name")
     String key;
 
     @Argument(index = 1, name = "value", required = false, description = "New value for the
system property")
@@ -41,6 +51,58 @@ public class SystemProperty extends Osgi
 
     @Override
     protected Object doExecute() throws Exception {
+        if (key == null && value == null) {
+            java.util.Properties props = (java.util.Properties) System.getProperties().clone();
+
+            String def = null;
+            if (unset) {
+                def = "unset";
+            }
+
+            setProperty(props, Constants.FRAMEWORK_BEGINNING_STARTLEVEL, def);
+            setProperty(props, Constants.FRAMEWORK_BOOTDELEGATION, def);
+            setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT, def);
+            setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_APP, def);
+            setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_BOOT, def);
+            setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_EXT, def);
+            setProperty(props, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK, def);
+            setProperty(props, Constants.FRAMEWORK_EXECPERMISSION, def);
+            setProperty(props, Constants.FRAMEWORK_EXECUTIONENVIRONMENT, def);
+            setProperty(props, Constants.FRAMEWORK_LANGUAGE, def);
+            setProperty(props, Constants.FRAMEWORK_LIBRARY_EXTENSIONS, def);
+            setProperty(props, Constants.FRAMEWORK_OS_NAME, def);
+            setProperty(props, Constants.FRAMEWORK_OS_VERSION, def);
+            setProperty(props, Constants.FRAMEWORK_PROCESSOR, def);
+            setProperty(props, Constants.FRAMEWORK_SECURITY, def);
+            setProperty(props, Constants.FRAMEWORK_SECURITY_OSGI, def);
+            setProperty(props, Constants.FRAMEWORK_STORAGE, def);
+            setProperty(props, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT, def);
+            setProperty(props, Constants.FRAMEWORK_SYSTEMPACKAGES, def);
+            setProperty(props, Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA, def);
+            setProperty(props, Constants.FRAMEWORK_VENDOR, def);
+            setProperty(props, Constants.FRAMEWORK_VERSION, def);
+            setProperty(props, Constants.FRAMEWORK_WINDOWSYSTEM, def);
+
+            setProperty(props, Constants.SUPPORTS_BOOTCLASSPATH_EXTENSION, def);
+            setProperty(props, Constants.SUPPORTS_FRAMEWORK_EXTENSION, def);
+            setProperty(props, Constants.SUPPORTS_FRAMEWORK_FRAGMENT, def);
+            setProperty(props, Constants.SUPPORTS_FRAMEWORK_REQUIREBUNDLE, def);
+
+            if (dumpToFile) {
+                PrintStream ps = new PrintStream(new File(bundleContext.getProperty("karaf.data"),
"dump-properties-" + System.currentTimeMillis() + ".properties"));
+                ps.println("#Dump of the System and OSGi properties with the command dev:system-property");
+                ps.println("#Dump executed at " + new SimpleDateFormat().format(new Date()));
+                printOrderedProperties(props, ps);
+                ps.flush();
+                ps.close();
+            } else {
+                System.out.println("OSGi and System properties: ");
+                printOrderedProperties(props, System.out);
+            }
+
+            return null;
+        }
+
         if (value != null) {
             if (persistent) {
                 String base = System.getProperty("karaf.base");
@@ -48,9 +110,35 @@ public class SystemProperty extends Osgi
                 props.put(key, value);
                 props.save();
             }
-            return System.setProperty(key, value);
+            System.setProperty(key, value);
         } else {
-            return System.getProperty(key);
+            System.out.println(System.getProperty(key));
         }
+
+        return null;
     }
+
+    private void setProperty(java.util.Properties props, String key, String def) {
+        String val = bundleContext.getProperty(key);
+        if (val == null && def != null) {
+            props.setProperty(key, def);
+        } else if (val != null) {
+            props.setProperty(key, val);
+        }
+    }
+
+    private void printOrderedProperties(java.util.Properties props, PrintStream out) {
+        Set<Object> keys = props.keySet();
+        Vector<String> order = new Vector<String>(keys.size());
+        for (Iterator<Object> i = keys.iterator(); i.hasNext(); ) {
+            Object str = (Object) i.next();
+            order.add((String) str);
+        }
+        Collections.sort(order);
+        for (Iterator<String> i = order.iterator(); i.hasNext(); ) {
+            String key = (String) i.next();
+            out.println(key + "=" + props.getProperty(key));
+        }
+    }
+
 }



Mime
View raw message