This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/karaf.git

commit 63b0a6c5e5e61209a058f7412814976a324bfe96
Author: Guillaume Nodet <[email protected]>
AuthorDate: Wed Nov 22 20:42:18 2017 +0100

    [KARAF-5475] Remove "log_" prefix in subtype, use a synchronous bundle 
listener to obtain the subject correctly
---
 .../java/org/apache/karaf/audit/Activator.java     |   6 +-
 .../impl/adapter/BundleEventAdapter.java           | 127 +++++++++++++++++++++
 2 files changed, 132 insertions(+), 1 deletion(-)

diff --git a/audit/src/main/java/org/apache/karaf/audit/Activator.java 
b/audit/src/main/java/org/apache/karaf/audit/Activator.java
index bc1ddf5..959bd50 100644
--- a/audit/src/main/java/org/apache/karaf/audit/Activator.java
+++ b/audit/src/main/java/org/apache/karaf/audit/Activator.java
@@ -415,7 +415,11 @@ public class Activator extends BaseActivator implements 
ManagedService {
 
         private String _subtype() {
             String topic = event.getTopic();
-            return topic.substring(topic.lastIndexOf('/') + 
1).toLowerCase(Locale.ENGLISH);
+            String subtype = topic.substring(topic.lastIndexOf('/') + 
1).toLowerCase(Locale.ENGLISH);
+            if (subtype.startsWith("log_")) {
+                subtype = subtype.substring("log_".length());
+            }
+            return subtype;
         }
 
         @Override
diff --git 
a/services/eventadmin/src/main/java/org/apache/felix/eventadmin/impl/adapter/BundleEventAdapter.java
 
b/services/eventadmin/src/main/java/org/apache/felix/eventadmin/impl/adapter/BundleEventAdapter.java
new file mode 100644
index 0000000..d59a1d0
--- /dev/null
+++ 
b/services/eventadmin/src/main/java/org/apache/felix/eventadmin/impl/adapter/BundleEventAdapter.java
@@ -0,0 +1,127 @@
+/*
+ * 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.felix.eventadmin.impl.adapter;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleEvent;
+import org.osgi.framework.BundleListener;
+import org.osgi.framework.SynchronousBundleListener;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.event.EventConstants;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+/**
+ * This class registers itself as a listener for bundle events and posts them 
via
+ * the EventAdmin as specified in 113.6.4 OSGi R4 compendium.
+ *
+ * @author <a href="mailto:[email protected]";>Felix Project Team</a>
+ */
+public class BundleEventAdapter extends AbstractAdapter implements 
SynchronousBundleListener
+{
+    /**
+     * The constructor of the adapter. This will register the adapter with the 
given
+     * context as a <tt>BundleListener</tt> and subsequently, will post 
received
+     * events via the given EventAdmin.
+     *
+     * @param context The bundle context with which to register as a listener.
+     * @param admin The <tt>EventAdmin</tt> to use for posting events.
+     */
+    public BundleEventAdapter(final BundleContext context, final EventAdmin 
admin)
+    {
+        super(admin);
+        context.addBundleListener(this);
+    }
+
+    @Override
+    public void destroy(BundleContext context) {
+        context.removeBundleListener(this);
+    }
+
+    /**
+     * Once a bundle event is received this method assembles and posts an 
event via
+     * the <tt>EventAdmin</tt> as specified in 113.6.4 OSGi R4 compendium.
+     *
+     * @param event The event to adapt.
+     */
+    @Override
+    public void bundleChanged(final BundleEvent event)
+    {
+        final Dictionary<String, Object> properties = new Hashtable<String, 
Object>();
+
+        properties.put(EventConstants.EVENT, event);
+
+        properties.put("bundle.id", new Long(event.getBundle()
+            .getBundleId()));
+
+        final String symbolicName = event.getBundle().getSymbolicName();
+
+        if (null != symbolicName)
+        {
+            properties.put(EventConstants.BUNDLE_SYMBOLICNAME,
+                symbolicName);
+        }
+
+        properties.put("bundle", event.getBundle());
+
+        final StringBuffer topic = new StringBuffer(BundleEvent.class
+            .getName().replace('.', '/')).append('/');
+
+        switch (event.getType())
+        {
+            case BundleEvent.INSTALLED:
+                topic.append("INSTALLED");
+                break;
+            case BundleEvent.STARTING:
+                topic.append("STARTING");
+                break;
+            case BundleEvent.STARTED:
+                topic.append("STARTED");
+                break;
+            case BundleEvent.STOPPING:
+                topic.append("STOPPING");
+                break;
+            case BundleEvent.STOPPED:
+                topic.append("STOPPED");
+                break;
+            case BundleEvent.UPDATED:
+                topic.append("UPDATED");
+                break;
+            case BundleEvent.UNINSTALLED:
+                topic.append("UNINSTALLED");
+                break;
+            case BundleEvent.RESOLVED:
+                topic.append("RESOLVED");
+                break;
+            case BundleEvent.UNRESOLVED:
+                topic.append("UNRESOLVED");
+                break;
+            default:
+                return; // IGNORE EVENT
+        }
+
+        try {
+            getEventAdmin().postEvent(new Event(topic.toString(), properties));
+        } catch (IllegalStateException e) {
+            // This is o.k. - indicates that we are stopped.
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
"[email protected]" <[email protected]>.

Reply via email to