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 2014/03/06 18:38:18 UTC

[3/6] git commit: [KARAF-2805] Command execution should trigger an EventAdmin event

[KARAF-2805] Command execution should trigger an EventAdmin event

Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/09be4b1f
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/09be4b1f
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/09be4b1f

Branch: refs/heads/master
Commit: 09be4b1f5a5a16d8be44222e11f58e02766b8185
Parents: a33fd10
Author: Guillaume Nodet <gn...@gmail.com>
Authored: Thu Mar 6 12:16:21 2014 +0100
Committer: Guillaume Nodet <gn...@gmail.com>
Committed: Thu Mar 6 13:29:08 2014 +0100

----------------------------------------------------------------------
 .../shell/impl/console/osgi/Activator.java      | 14 +++++
 .../impl/console/osgi/EventAdminListener.java   | 66 ++++++++++++++++++++
 2 files changed, 80 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/09be4b1f/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/Activator.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/Activator.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/Activator.java
index 1dba465..94b45c0 100644
--- a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/Activator.java
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/Activator.java
@@ -18,6 +18,8 @@
  */
 package org.apache.karaf.shell.impl.console.osgi;
 
+import java.io.Closeable;
+
 import org.apache.felix.gogo.runtime.threadio.ThreadIOImpl;
 import org.apache.karaf.shell.api.console.SessionFactory;
 import org.apache.karaf.shell.impl.action.osgi.CommandExtender;
@@ -40,6 +42,8 @@ public class Activator implements BundleActivator {
 
     private CommandExtender actionExtender;
 
+    private Closeable eventAdminListener;
+
     private TerminalFactory terminalFactory;
     private LocalConsoleManager localConsoleManager;
 
@@ -51,6 +55,13 @@ public class Activator implements BundleActivator {
         sessionFactory = new SecuredSessionFactoryImpl(context, threadIO);
         sessionFactory.getCommandProcessor().addConverter(new Converters(context));
         sessionFactory.getCommandProcessor().addConstant(".context", context.getBundle(0).getBundleContext());
+        try {
+            EventAdminListener listener = new EventAdminListener(context);
+            sessionFactory.getCommandProcessor().addListener(listener);
+            eventAdminListener = listener;
+        } catch (NoClassDefFoundError error) {
+            // Ignore the listener if EventAdmin package isn't present
+        }
 
         sessionFactory.register(new ManagerImpl(sessionFactory, sessionFactory));
 
@@ -74,5 +85,8 @@ public class Activator implements BundleActivator {
         actionExtender.stop(context);
         threadIO.stop();
         terminalFactory.destroy();
+        if (eventAdminListener != null) {
+            eventAdminListener.close();
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/karaf/blob/09be4b1f/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/EventAdminListener.java
----------------------------------------------------------------------
diff --git a/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/EventAdminListener.java b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/EventAdminListener.java
new file mode 100644
index 0000000..56905d2
--- /dev/null
+++ b/shell/core/src/main/java/org/apache/karaf/shell/impl/console/osgi/EventAdminListener.java
@@ -0,0 +1,66 @@
+/*
+ * 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.impl.console.osgi;
+
+import java.io.Closeable;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.felix.gogo.api.CommandSessionListener;
+import org.apache.felix.service.command.CommandSession;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.util.tracker.ServiceTracker;
+
+public class EventAdminListener implements CommandSessionListener, Closeable
+{
+
+    private ServiceTracker<EventAdmin, EventAdmin> tracker;
+
+    public EventAdminListener(BundleContext bundleContext)
+    {
+        tracker = new ServiceTracker<EventAdmin, EventAdmin>(bundleContext, EventAdmin.class.getName(), null);
+        tracker.open();
+    }
+
+    public void close() {
+        tracker.close();
+    }
+
+    public void beforeExecute(CommandSession session, CharSequence command) {
+        if (command.toString().trim().length() > 0) {
+            EventAdmin admin = tracker.getService();
+            if (admin != null) {
+                Map<String, Object> props = new HashMap<String, Object>();
+                props.put("command", command.toString());
+                Event event = new Event("org/apache/karaf/shell/console/EXECUTING", props);
+                admin.postEvent(event);
+            }
+        }
+    }
+
+    public void afterExecute(CommandSession session, CharSequence command, Exception exception) {
+    }
+
+    public void afterExecute(CommandSession session, CharSequence command, Object result) {
+    }
+
+}