You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by ge...@apache.org on 2010/10/04 20:27:12 UTC

svn commit: r1004353 - in /karaf/branches/karaf-2.0.x/features/core: ./ src/main/java/org/apache/karaf/features/ src/main/java/org/apache/karaf/features/internal/

Author: gertv
Date: Mon Oct  4 18:27:12 2010
New Revision: 1004353

URL: http://svn.apache.org/viewvc?rev=1004353&view=rev
Log:
[KARAF-225] FeaturesService should publish OSGi events if EventAdmin is available

Added:
    karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/EventConstants.java
    karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/EventAdminListener.java
Modified:
    karaf/branches/karaf-2.0.x/features/core/pom.xml
    karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java

Modified: karaf/branches/karaf-2.0.x/features/core/pom.xml
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.0.x/features/core/pom.xml?rev=1004353&r1=1004352&r2=1004353&view=diff
==============================================================================
--- karaf/branches/karaf-2.0.x/features/core/pom.xml (original)
+++ karaf/branches/karaf-2.0.x/features/core/pom.xml Mon Oct  4 18:27:12 2010
@@ -107,6 +107,7 @@
                             org.osgi.service.command,
                             org.apache.felix.gogo.commands,
                             org.apache.karaf.shell.console,
+                            org.osgi.service.event*;resolution:=optional,
                             *
                         </Import-Package>
                         <Private-Package>

Added: karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/EventConstants.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/EventConstants.java?rev=1004353&view=auto
==============================================================================
--- karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/EventConstants.java (added)
+++ karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/EventConstants.java Mon Oct  4 18:27:12 2010
@@ -0,0 +1,46 @@
+/*
+ * 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.features;
+
+/**
+ * Constants for EventAdmin events
+ */
+public final class EventConstants {
+
+    public static final String TYPE = "type";
+    public static final String EVENT = "event";
+    public static final String TIMESTAMP = "timestamp";
+
+    public static final String FEATURE_NAME = "name";
+    public static final String FEATURE_VERSION = "version";
+
+    public static final String REPOSITORY_NAME = "name";
+    public static final String REPOSITORY_URI = "uri";
+
+    public static final String TOPIC_EVENTS = "org/apache/karaf/features";
+    public static final String TOPIC_FEATURES_INSTALLED = TOPIC_EVENTS + "/features/INSTALLED";
+    public static final String TOPIC_FEATURES_UNINSTALLED = TOPIC_EVENTS + "/features/UNINSTALLED";
+    public static final String TOPIC_REPOSITORY_ADDED = TOPIC_EVENTS + "/repositories/ADDED";
+    public static final String TOPIC_REPOSITORY_REMOVED = TOPIC_EVENTS + "/repositories/REMOVED";
+
+    private EventConstants() {
+        // non-instantiable class
+    }
+
+
+
+}

Added: karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/EventAdminListener.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/EventAdminListener.java?rev=1004353&view=auto
==============================================================================
--- karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/EventAdminListener.java (added)
+++ karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/EventAdminListener.java Mon Oct  4 18:27:12 2010
@@ -0,0 +1,92 @@
+/*
+ * 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.features.internal;
+
+import java.util.Dictionary;
+import java.util.Hashtable;
+
+import org.apache.karaf.features.EventConstants;
+import org.apache.karaf.features.FeatureEvent;
+import org.apache.karaf.features.FeaturesListener;
+import org.apache.karaf.features.RepositoryEvent;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.util.tracker.ServiceTracker;
+
+/**
+ * A listener to publish events to EventAdmin
+ */
+public class EventAdminListener implements FeaturesListener {
+
+    private final ServiceTracker tracker;
+
+    public EventAdminListener(BundleContext context) {
+        tracker = new ServiceTracker(context, EventAdmin.class.getName(), null);
+        tracker.open();
+    }
+
+    public void featureEvent(FeatureEvent event) {
+        EventAdmin eventAdmin = (EventAdmin) tracker.getService();
+        if (eventAdmin == null) {
+            return;
+        }
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(EventConstants.TYPE, event.getType());
+        props.put(EventConstants.EVENT, event);
+        props.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
+        props.put(EventConstants.FEATURE_NAME, event.getFeature().getName());
+        props.put(EventConstants.FEATURE_VERSION, event.getFeature().getVersion());
+        String topic;
+        switch (event.getType()) {
+            case FeatureInstalled:
+                topic = EventConstants.TOPIC_FEATURES_INSTALLED;
+                break;
+            case FeatureUninstalled:
+                topic = EventConstants.TOPIC_FEATURES_UNINSTALLED;
+                break;
+            default:
+                throw new IllegalStateException("Unknown features event type: " + event.getType());
+        }
+        eventAdmin.postEvent(new Event(topic, props));
+    }
+
+    public void repositoryEvent(RepositoryEvent event) {
+        EventAdmin eventAdmin = (EventAdmin) tracker.getService();
+        if (eventAdmin == null) {
+            return;
+        }
+        Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(EventConstants.TYPE, event.getType());
+        props.put(EventConstants.EVENT, event);
+        props.put(EventConstants.TIMESTAMP, System.currentTimeMillis());
+        props.put(EventConstants.REPOSITORY_NAME, event.getRepository().getName());
+        props.put(EventConstants.REPOSITORY_URI, event.getRepository().getURI().toString());
+        String topic;
+        switch (event.getType()) {
+            case RepositoryAdded:
+                topic = EventConstants.TOPIC_REPOSITORY_ADDED;
+                break;
+            case RepositoryRemoved:
+                topic = EventConstants.TOPIC_REPOSITORY_REMOVED;
+                break;
+            default:
+                throw new IllegalStateException("Unknown repository event type: " + event.getType());
+        }
+        eventAdmin.postEvent(new Event(topic, props));
+    }
+}

Modified: karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java
URL: http://svn.apache.org/viewvc/karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java?rev=1004353&r1=1004352&r2=1004353&view=diff
==============================================================================
--- karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java (original)
+++ karaf/branches/karaf-2.0.x/features/core/src/main/java/org/apache/karaf/features/internal/FeaturesServiceImpl.java Mon Oct  4 18:27:12 2010
@@ -87,6 +87,10 @@ public class FeaturesServiceImpl impleme
     private boolean bootFeaturesInstalled;
     private List<FeaturesListener> listeners = new CopyOnWriteArrayList<FeaturesListener>();
     private ThreadLocal<Repository> repo = new ThreadLocal<Repository>();
+    private EventAdminListener eventAdminListener;
+
+    public FeaturesServiceImpl() {
+    }
 
     public BundleContext getBundleContext() {
         return bundleContext;
@@ -730,6 +734,15 @@ public class FeaturesServiceImpl impleme
     }
 
     public void start() throws Exception {
+        EventAdminListener listener = null;
+        try {
+            getClass().getClassLoader().loadClass("org.osgi.service.event.EventAdmin");
+            listener = new EventAdminListener(bundleContext);
+        } catch (Throwable t) {
+            // Ignore, if the EventAdmin package is not available, just don't use it
+            LOGGER.debug("EventAdmin package is not available, just don't use it");
+        }
+        this.eventAdminListener = listener;
         if (!loadState()) {
             if (uris != null) {
                 for (URI uri : uris) {
@@ -931,12 +944,18 @@ public class FeaturesServiceImpl impleme
     }
 
     protected void callListeners(FeatureEvent event) {
+        if (eventAdminListener != null) {
+            eventAdminListener.featureEvent(event);
+        }
         for (FeaturesListener listener : listeners) {
             listener.featureEvent(event);
         }
     }
 
     protected void callListeners(RepositoryEvent event) {
+        if (eventAdminListener != null) {
+            eventAdminListener.repositoryEvent(event);
+        }
         for (FeaturesListener listener : listeners) {
             listener.repositoryEvent(event);
         }