You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by gn...@apache.org on 2012/04/10 09:45:57 UTC

svn commit: r1311618 - in /karaf/branches/karaf-2.3.x/shell: console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml

Author: gnodet
Date: Tue Apr 10 07:45:56 2012
New Revision: 1311618

URL: http://svn.apache.org/viewvc?rev=1311618&view=rev
Log:
[KARAF-908] Provide a way to access system properties easily in the shell

Added:
    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/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java
    karaf/branches/karaf-2.3.x/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml

Modified: karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java?rev=1311618&r1=1311617&r2=1311618&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java (original)
+++ karaf/branches/karaf-2.3.x/shell/console/src/main/java/org/apache/karaf/shell/console/jline/Activator.java Tue Apr 10 07:45:56 2012
@@ -18,13 +18,23 @@
  */
 package org.apache.karaf.shell.console.jline;
 
+import java.io.InputStream;
+import java.io.PrintStream;
+
+import org.apache.felix.gogo.runtime.CommandProcessorImpl;
+import org.apache.felix.gogo.runtime.CommandSessionImpl;
+import org.apache.felix.gogo.runtime.activator.EventAdminListener;
+import org.apache.felix.service.command.CommandProcessor;
+import org.apache.felix.service.command.CommandSession;
+import org.apache.felix.service.threadio.ThreadIO;
 import org.fusesource.jansi.AnsiConsole;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
 
 public class Activator implements BundleActivator {
 
-    private org.apache.felix.gogo.runtime.activator.Activator activator = new org.apache.felix.gogo.runtime.activator.Activator();
+    private org.apache.felix.gogo.runtime.activator.Activator activator = new WrappedActivator();
 
     public void start(BundleContext context) throws Exception {
         AnsiConsole.systemInstall();
@@ -36,4 +46,58 @@ public class Activator implements Bundle
         AnsiConsole.systemUninstall();
     }
 
+    protected static class WrappedActivator extends org.apache.felix.gogo.runtime.activator.Activator {
+
+        @Override
+        protected ServiceRegistration newProcessor(ThreadIO tio, BundleContext context) {
+            processor = new WrappedCommandProcessor(tio);
+            try
+            {
+                processor.addListener(new EventAdminListener(context));
+            }
+            catch (NoClassDefFoundError error)
+            {
+                // Ignore the listener if EventAdmin package isn't present
+            }
+
+            // Setup the variables and commands exposed in an OSGi environment.
+            processor.addConstant(CONTEXT, context);
+            processor.addCommand("osgi", processor, "addCommand");
+            processor.addCommand("osgi", processor, "removeCommand");
+            processor.addCommand("osgi", processor, "eval");
+
+            return context.registerService(CommandProcessor.class.getName(), processor, null);
+        }
+
+    }
+
+    protected static class WrappedCommandProcessor extends CommandProcessorImpl {
+
+        public WrappedCommandProcessor(ThreadIO tio) {
+            super(tio);
+        }
+
+        @Override
+        public CommandSession createSession(InputStream in, PrintStream out, PrintStream err) {
+            CommandSessionImpl session = new WrappedCommandSession(this, in, out, err);
+            sessions.put(session, null);
+            return session;
+        }
+    }
+
+    protected static class WrappedCommandSession extends CommandSessionImpl {
+
+        public WrappedCommandSession(CommandProcessorImpl shell, InputStream in, PrintStream out, PrintStream err) {
+            super(shell, in, out, err);
+        }
+        public Object get(String name) {
+            Object val = super.get(name);
+            if (val == null) {
+                val = System.getProperty(name);
+            }
+            return val;
+        }
+
+   }
+
 }

Added: 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=1311618&view=auto
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java (added)
+++ karaf/branches/karaf-2.3.x/shell/dev/src/main/java/org/apache/karaf/shell/dev/SystemProperty.java Tue Apr 10 07:45:56 2012
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.karaf.shell.dev;
+
+import java.io.File;
+
+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;
+
+/**
+ * Command that allow access to system properties easily.
+ */
+@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")
+    boolean persistent;
+
+    @Argument(index = 0, name = "key", description = "The system property name")
+    String key;
+
+    @Argument(index = 1, name = "value", required = false, description = "New value for the system property")
+    String value;
+
+    @Override
+    protected Object doExecute() throws Exception {
+        if (value != null) {
+            if (persistent) {
+                String base = System.getProperty("karaf.base");
+                Properties props = new Properties(new File(base, "etc/system.properties"));
+                props.put(key, value);
+                props.save();
+            }
+            return System.setProperty(key, value);
+        } else {
+            return System.getProperty(key);
+        }
+    }
+}

Modified: karaf/branches/karaf-2.3.x/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.3.x/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml?rev=1311618&r1=1311617&r2=1311618&view=diff
==============================================================================
--- karaf/branches/karaf-2.3.x/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml (original)
+++ karaf/branches/karaf-2.3.x/shell/dev/src/main/resources/OSGI-INF/blueprint/shell-dev.xml Tue Apr 10 07:45:56 2012
@@ -35,6 +35,12 @@
         <command name="dev/restart">
             <action class="org.apache.karaf.shell.dev.Restart" />
         </command>
+        <command name="dev/system-property">
+            <action class="org.apache.karaf.shell.dev.SystemProperty" />
+        </command>
+        <command name="dev/wait-for-service">
+            <action class="org.apache.karaf.shell.dev.WaitForService" />
+        </command>
         <command name="dev/watch">
             <action class="org.apache.karaf.shell.dev.Watch" >
                 <property name="watcher" ref="watcher"/>