Implement mappings from UserAdminEvents (FELIX-86)


git-svn-id: https://svn.apache.org/repos/asf/incubator/felix/trunk@416926 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/org.apache.felix.eventadmin.bridge.useradmin/pom.xml b/org.apache.felix.eventadmin.bridge.useradmin/pom.xml
new file mode 100644
index 0000000..60e368a
--- /dev/null
+++ b/org.apache.felix.eventadmin.bridge.useradmin/pom.xml
@@ -0,0 +1,46 @@
+<project>
+  <parent>
+    <groupId>org.apache.felix</groupId>
+    <artifactId>felix</artifactId>
+    <version>0.8.0-SNAPSHOT</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <packaging>osgi-bundle</packaging>
+  <name>Apache Felix EventAdmin Bridge UserAdmin</name>
+  <artifactId>org.apache.felix.eventadmin.bridge.useradmin</artifactId>
+  <dependencies>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.core</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+    <dependency>
+      <groupId>${pom.groupId}</groupId>
+      <artifactId>org.osgi.compendium</artifactId>
+      <version>${pom.version}</version>
+      <scope>provided</scope>
+    </dependency>
+  </dependencies>
+  <build>
+    <plugins>
+      <plugin>
+        <groupId>org.apache.felix.plugins</groupId>
+        <artifactId>maven-osgi-plugin</artifactId>
+        <version>${pom.version}</version>
+        <extensions>true</extensions>
+        <configuration>
+          <osgiManifest>
+            <bundleName>EventAdmin Bridge UserAdmin</bundleName>
+            <bundleVendor>Apache Software Foundation</bundleVendor>
+            <bundleDescription>
+              This bundle provides a bridge between UserAdmin and EventAdmin events.
+            </bundleDescription>
+            <bundleActivator>auto-detect</bundleActivator>
+            <bundleSymbolicName>${pom.artifactId}</bundleSymbolicName>
+          </osgiManifest>
+        </configuration>
+      </plugin>
+    </plugins>
+  </build>
+</project>
diff --git a/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/Activator.java b/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/Activator.java
new file mode 100644
index 0000000..f8116e1
--- /dev/null
+++ b/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/Activator.java
@@ -0,0 +1,53 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.bridge.useradmin;
+
+import org.osgi.framework.BundleActivator;
+import org.osgi.framework.BundleContext;
+
+/**
+ * The BundleActivator that will register an UserAdminEventListener service with the 
+ * framework on start. Subsequently, UserAdminEvents will be bridged to available 
+ * EventAdmin services (as per spec).
+ * 
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class Activator implements BundleActivator
+{
+    /**
+     * This registers an UserAdminEventListener service with the framework that bridges 
+     * UserAdminEvents to the EventAdmin.
+     * 
+     * @param context The context to use
+     * 
+     * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
+     */
+    public void start(final BundleContext context) throws Exception
+    {
+        new UserAdminEventToEventAdminBridge(context);
+    }
+
+    /**
+     * Stop the bridging of UserAdminEvents to the EventAdmin.
+     * 
+     * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
+     */
+    public void stop(final BundleContext context) throws Exception
+    {
+        // Services are unregistered by the framework
+    }
+}
diff --git a/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/UserAdminEventToEventAdminBridge.java b/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/UserAdminEventToEventAdminBridge.java
new file mode 100644
index 0000000..4b25c36
--- /dev/null
+++ b/org.apache.felix.eventadmin.bridge.useradmin/src/main/java/org/apache/felix/eventadmin/bridge/useradmin/UserAdminEventToEventAdminBridge.java
@@ -0,0 +1,124 @@
+/*
+ *   Copyright 2006 The Apache Software Foundation
+ *
+ *   Licensed 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.bridge.useradmin;
+
+import java.util.Arrays;
+import java.util.Hashtable;
+
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.event.EventConstants;
+import org.osgi.service.useradmin.UserAdmin;
+import org.osgi.service.useradmin.UserAdminEvent;
+import org.osgi.service.useradmin.UserAdminListener;
+
+/**
+ * @author <a href="mailto:felix-dev@incubator.apache.org">Felix Project Team</a>
+ */
+public class UserAdminEventToEventAdminBridge implements UserAdminListener
+{
+    private final BundleContext m_context;
+
+    public UserAdminEventToEventAdminBridge(final BundleContext context)
+    {
+        m_context = context;
+
+        m_context
+            .registerService(UserAdminListener.class.getName(), this, null);
+    }
+
+    public void roleChanged(final UserAdminEvent event)
+    {
+        final ServiceReference ref = m_context
+            .getServiceReference(EventAdmin.class.getName());
+
+        if(null != ref)
+        {
+            final EventAdmin eventAdmin = (EventAdmin) m_context
+                .getService(ref);
+
+            if(null != eventAdmin)
+            {
+                final String topic;
+
+                switch(event.getType())
+                {
+                    case UserAdminEvent.ROLE_CHANGED:
+                        topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CHANGED";
+                        break;
+                    case UserAdminEvent.ROLE_CREATED:
+                        topic = "org/osgi/service/useradmin/UserAdmin/ROLE_CREATED";
+                        break;
+                    case UserAdminEvent.ROLE_REMOVED:
+                        topic = "org/osgi/service/useradmin/UserAdmin/ROLE_REMOVED";
+                        break;
+                    default:
+                        m_context.ungetService(ref);
+                        return;
+                }
+
+                eventAdmin.postEvent(new Event(topic, new Hashtable()
+                {
+                    {
+                        put(EventConstants.EVENT, event);
+
+                        put("role", event.getRole());
+
+                        put("role.name", event.getRole().getName());
+
+                        put("role.type", new Integer(event.getRole().getType()));
+
+                        final ServiceReference ref = event
+                            .getServiceReference();
+
+                        if(null == ref)
+                        {
+                            throw new IllegalArgumentException(
+                                "UserAdminEvent.getServiceReference() may not be null");
+                        }
+
+                        put(EventConstants.SERVICE, ref);
+
+                        put(EventConstants.SERVICE_ID, ref
+                            .getProperty(EventConstants.SERVICE_ID));
+
+                        final Object objectClass = ref
+                            .getProperty(Constants.OBJECTCLASS);
+
+                        if(!(objectClass instanceof String[])
+                            || !Arrays.asList((String[]) objectClass).contains(
+                                UserAdmin.class.getName()))
+                        {
+                            throw new IllegalArgumentException(
+                                "Bad objectclass: " + objectClass);
+                        }
+
+                        put(EventConstants.SERVICE_OBJECTCLASS, objectClass);
+
+                        put(EventConstants.SERVICE_PID, ref
+                            .getProperty(EventConstants.SERVICE_PID));
+                    }
+                }));
+
+                m_context.ungetService(ref);
+            }
+        }
+    }
+}