Author: veithen
Date: Sat Oct  8 19:03:38 2011
New Revision: 1180461

URL: http://svn.apache.org/viewvc?rev=1180461&view=rev
Log:
Register discovered implementations as OSGi services.

Added:
    
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java
   (with props)
Modified:
    
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Activator.java
    
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Implementation.java
    
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/ImplementationFactory.java
    
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/OSGiOMMetaFactoryLocator.java
    
webservices/axiom/branches/osgi-redesign/modules/axiom-dom/src/main/resources/META-INF/axiom.xml
    
webservices/axiom/branches/osgi-redesign/modules/axiom-impl/src/main/resources/META-INF/axiom.xml

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Activator.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Activator.java?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Activator.java
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Activator.java
 Sat Oct  8 19:03:38 2011
@@ -28,7 +28,7 @@ public class Activator implements Bundle
     private BundleTracker tracker;
 
     public void start(BundleContext context) throws Exception {
-        OSGiOMMetaFactoryLocator locator = new OSGiOMMetaFactoryLocator();
+        OSGiOMMetaFactoryLocator locator = new 
OSGiOMMetaFactoryLocator(context);
         OMAbstractFactory.setMetaFactoryLocator(locator);
         tracker = new BundleTracker(context, Bundle.ACTIVE, locator);
         tracker.open();

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Implementation.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Implementation.java?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Implementation.java
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/Implementation.java
 Sat Oct  8 19:03:38 2011
@@ -21,14 +21,20 @@ package org.apache.axiom.locator;
 import org.apache.axiom.om.OMMetaFactory;
 
 final class Implementation {
+    private final String name;
     private final OMMetaFactory metaFactory;
     private final Feature[] features;
     
-    Implementation(OMMetaFactory metaFactory, Feature[] features) {
+    Implementation(String name, OMMetaFactory metaFactory, Feature[] features) 
{
+        this.name = name;
         this.metaFactory = metaFactory;
         this.features = features;
     }
 
+    String getName() {
+        return name;
+    }
+
     OMMetaFactory getMetaFactory() {
         return metaFactory;
     }

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/ImplementationFactory.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/ImplementationFactory.java?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/ImplementationFactory.java
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/ImplementationFactory.java
 Sat Oct  8 19:03:38 2011
@@ -49,7 +49,7 @@ final class ImplementationFactory {
     
     static Implementation createDefaultImplementation(Loader loader, String 
className) {
         OMMetaFactory metaFactory = load(loader, className);
-        return metaFactory == null ? null : new Implementation(metaFactory,
+        return metaFactory == null ? null : new Implementation(null, 
metaFactory,
                 new Feature[] { new Feature(OMAbstractFactory.FEATURE_DEFAULT, 
Integer.MAX_VALUE) });
     }
     
@@ -110,34 +110,38 @@ final class ImplementationFactory {
     }
     
     private static Implementation parseImplementation(Loader loader, Element 
implementation) {
+        String name = implementation.getAttributeNS(null, "name");
+        if (name.length() == 0) {
+            log.error("Encountered " + QNAME_IMPLEMENTATION + " element 
without name attribute");
+            return null;
+        }
         String className = implementation.getAttributeNS(null, "metaFactory");
         if (className.length() == 0) {
             log.error("Encountered " + QNAME_IMPLEMENTATION + " element 
without metaFactory attribute");
             return null;
-        } else {
-            OMMetaFactory metaFactory = load(loader, className);
-            if (metaFactory == null) {
-                return null;
-            }
-            List/*<Feature>*/ features = new ArrayList();
-            Node child = implementation.getFirstChild();
-            while (child != null) {
-                if (child instanceof Element) {
-                    QName childQName = getQName(child);
-                    if (childQName.equals(QNAME_FEATURE)) {
-                        Feature feature = parseFeature((Element)child);
-                        if (feature != null) {
-                            features.add(feature);
-                        }
-                    } else {
-                        log.warn("Skipping unexpected element " + childQName + 
"; only "
-                                + QNAME_FEATURE + " is expected");
+        }
+        OMMetaFactory metaFactory = load(loader, className);
+        if (metaFactory == null) {
+            return null;
+        }
+        List/*<Feature>*/ features = new ArrayList();
+        Node child = implementation.getFirstChild();
+        while (child != null) {
+            if (child instanceof Element) {
+                QName childQName = getQName(child);
+                if (childQName.equals(QNAME_FEATURE)) {
+                    Feature feature = parseFeature((Element)child);
+                    if (feature != null) {
+                        features.add(feature);
                     }
+                } else {
+                    log.warn("Skipping unexpected element " + childQName + "; 
only "
+                            + QNAME_FEATURE + " is expected");
                 }
-                child = child.getNextSibling();
             }
-            return new Implementation(metaFactory, 
(Feature[])features.toArray(new Feature[features.size()]));
+            child = child.getNextSibling();
         }
+        return new Implementation(name, metaFactory, 
(Feature[])features.toArray(new Feature[features.size()]));
     }
 
     private static Feature parseFeature(Element feature) {

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/OSGiOMMetaFactoryLocator.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/OSGiOMMetaFactoryLocator.java?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/OSGiOMMetaFactoryLocator.java
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/OSGiOMMetaFactoryLocator.java
 Sat Oct  8 19:03:38 2011
@@ -20,16 +20,26 @@ package org.apache.axiom.locator;
 
 import java.net.URL;
 import java.util.ArrayList;
+import java.util.Hashtable;
+import java.util.Iterator;
 import java.util.List;
 
 import org.apache.axiom.om.OMMetaFactory;
 import org.osgi.framework.Bundle;
+import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
 import org.osgi.util.tracker.BundleTrackerCustomizer;
 
 final class OSGiOMMetaFactoryLocator extends PriorityBasedOMMetaFactoryLocator 
implements BundleTrackerCustomizer {
+    private final BundleContext apiBundleContext;
     private final List/*<Implementation>*/ implementations = new ArrayList();
     
+    OSGiOMMetaFactoryLocator(BundleContext apiBundleContext) {
+        this.apiBundleContext = apiBundleContext;
+    }
+
     // Need to synchronize access because the implementations may be reloaded 
concurrently
     public synchronized OMMetaFactory getOMMetaFactory(String feature) {
         return super.getOMMetaFactory(feature);
@@ -38,12 +48,24 @@ final class OSGiOMMetaFactoryLocator ext
     public Object addingBundle(Bundle bundle, BundleEvent event) {
         URL descriptorUrl = 
bundle.getEntry(ImplementationFactory.DESCRIPTOR_RESOURCE);
         if (descriptorUrl != null) {
-            List discoveredImplementations = 
ImplementationFactory.parseDescriptor(new OSGiLoader(bundle), descriptorUrl);
+            List/*<Implementation>*/ discoveredImplementations = 
ImplementationFactory.parseDescriptor(new OSGiLoader(bundle), descriptorUrl);
+            List/*<RegisteredImplementation>*/ registeredImplementations = new 
ArrayList(discoveredImplementations.size());
             synchronized (this) {
                 implementations.addAll(discoveredImplementations);
                 loadImplementations(implementations);
             }
-            return discoveredImplementations;
+            for (Iterator it = discoveredImplementations.iterator(); 
it.hasNext(); ) {
+                Implementation implementation = (Implementation)it.next();
+                Hashtable properties = new Hashtable();
+                properties.put("implementationName", implementation.getName());
+                // TODO: we should add the features and priorities to the 
properties as well
+                ServiceRegistration registration = 
bundle.getBundleContext().registerService(OMMetaFactory.class.getName(), 
implementation.getMetaFactory(), properties);
+                ServiceReference reference = registration.getReference();
+                // Let the OSGi runtime know that the axiom-api bundle is 
using the service
+                apiBundleContext.getService(reference);
+                registeredImplementations.add(new 
RegisteredImplementation(implementation, registration, reference));
+            }
+            return registeredImplementations;
         } else {
             return null;
         }
@@ -54,8 +76,15 @@ final class OSGiOMMetaFactoryLocator ext
 
     public void removedBundle(Bundle bundle, BundleEvent event, Object object) 
{
         if (object != null) {
+            for (Iterator it = ((List)object).iterator(); it.hasNext(); ) {
+                RegisteredImplementation registeredImplementation = 
(RegisteredImplementation)it.next();
+                
apiBundleContext.ungetService(registeredImplementation.getReference());
+                registeredImplementation.getRegistration().unregister();
+                synchronized (this) {
+                    
implementations.remove(registeredImplementation.getImplementation());
+                }
+            }
             synchronized (this) {
-                implementations.removeAll((List)object);
                 loadImplementations(implementations);
             }
         }

Added: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java?rev=1180461&view=auto
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java
 (added)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java
 Sat Oct  8 19:03:38 2011
@@ -0,0 +1,47 @@
+/*
+ * 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.axiom.locator;
+
+import org.osgi.framework.ServiceReference;
+import org.osgi.framework.ServiceRegistration;
+
+class RegisteredImplementation {
+    private final Implementation implementation;
+    private final ServiceRegistration registration;
+    private final ServiceReference reference;
+    
+    RegisteredImplementation(Implementation implementation,
+            ServiceRegistration registration, ServiceReference reference) {
+        this.implementation = implementation;
+        this.registration = registration;
+        this.reference = reference;
+    }
+
+    Implementation getImplementation() {
+        return implementation;
+    }
+
+    ServiceRegistration getRegistration() {
+        return registration;
+    }
+
+    ServiceReference getReference() {
+        return reference;
+    }
+}

Propchange: 
webservices/axiom/branches/osgi-redesign/modules/axiom-api/src/main/java/org/apache/axiom/locator/RegisteredImplementation.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-dom/src/main/resources/META-INF/axiom.xml
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-dom/src/main/resources/META-INF/axiom.xml?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-dom/src/main/resources/META-INF/axiom.xml
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-dom/src/main/resources/META-INF/axiom.xml
 Sat Oct  8 19:03:38 2011
@@ -18,7 +18,7 @@
   ~ under the License.
   -->
 <implementations xmlns="http://ws.apache.org/axiom/";>
-    <implementation 
metaFactory="org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactory">
+    <implementation name="doom" 
metaFactory="org.apache.axiom.om.impl.dom.factory.OMDOMMetaFactory">
         <feature name="dom" priority="100"/>
     </implementation>
 </implementations>
\ No newline at end of file

Modified: 
webservices/axiom/branches/osgi-redesign/modules/axiom-impl/src/main/resources/META-INF/axiom.xml
URL: 
http://svn.apache.org/viewvc/webservices/axiom/branches/osgi-redesign/modules/axiom-impl/src/main/resources/META-INF/axiom.xml?rev=1180461&r1=1180460&r2=1180461&view=diff
==============================================================================
--- 
webservices/axiom/branches/osgi-redesign/modules/axiom-impl/src/main/resources/META-INF/axiom.xml
 (original)
+++ 
webservices/axiom/branches/osgi-redesign/modules/axiom-impl/src/main/resources/META-INF/axiom.xml
 Sat Oct  8 19:03:38 2011
@@ -18,7 +18,7 @@
   ~ under the License.
   -->
 <implementations xmlns="http://ws.apache.org/axiom/";>
-    <implementation 
metaFactory="org.apache.axiom.om.impl.llom.factory.OMLinkedListMetaFactory">
+    <implementation name="llom" 
metaFactory="org.apache.axiom.om.impl.llom.factory.OMLinkedListMetaFactory">
         <feature name="default" priority="100"/>
     </implementation>
 </implementations>
\ No newline at end of file


Reply via email to