felix-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fmesc...@apache.org
Subject svn commit: r1403000 - in /felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal: MetaTypeInformationImpl.java MetaTypeProviderTracker.java MetaTypeServiceImpl.java
Date Sun, 28 Oct 2012 14:34:50 GMT
Author: fmeschbe
Date: Sun Oct 28 14:34:50 2012
New Revision: 1403000

URL: http://svn.apache.org/viewvc?rev=1403000&view=rev
Log:
FELIX-3740 Track MetaTypeProvider services in the MetaTypeServiceImpl
instead of in the MetaTypeInformationImpl objects.

Modified:
    felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeInformationImpl.java
    felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
    felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeServiceImpl.java

Modified: felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeInformationImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeInformationImpl.java?rev=1403000&r1=1402999&r2=1403000&view=diff
==============================================================================
--- felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeInformationImpl.java
(original)
+++ felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeInformationImpl.java
Sun Oct 28 14:34:50 2012
@@ -57,8 +57,6 @@ public class MetaTypeInformationImpl imp
 
     private final Map metaTypeProviders;
 
-    private final MetaTypeProviderTracker providerTacker;
-
     private Set locales;
 
 
@@ -68,16 +66,11 @@ public class MetaTypeInformationImpl imp
         this.pids = new HashSet();
         this.factoryPids = new HashSet();
         this.metaTypeProviders = new HashMap();
-
-        this.providerTacker = new MetaTypeProviderTracker( serviceBundleContext, this );
-        this.providerTacker.open();
     }
 
 
     void dispose()
     {
-        this.providerTacker.close();
-
         this.pids.clear();
         this.factoryPids.clear();
         this.locales = null;

Modified: felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java?rev=1403000&r1=1402999&r2=1403000&view=diff
==============================================================================
--- felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
(original)
+++ felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
Sun Oct 28 14:34:50 2012
@@ -30,28 +30,29 @@ import org.osgi.util.tracker.ServiceTrac
 public class MetaTypeProviderTracker extends ServiceTracker
 {
 
-    final MetaTypeInformationImpl mti;
+    final MetaTypeServiceImpl mti;
 
 
-    public MetaTypeProviderTracker( BundleContext context, final MetaTypeInformationImpl
mti )
+    public MetaTypeProviderTracker( BundleContext context, final String serviceType, final
MetaTypeServiceImpl mti )
     {
-        super( context, MetaTypeProvider.class, null );
+        super( context, serviceType, null );
         this.mti = mti;
     }
 
 
     public Object addingService( ServiceReference reference )
     {
-        // only care for services of our bundle
-        if ( !this.mti.getBundle().equals( reference.getBundle() ) )
+        final Object service = this.context.getService( reference );
+        final RegistrationPropertyHolder rph;
+        if ( service instanceof MetaTypeProvider )
         {
-            return null;
+            rph = new RegistrationPropertyHolder( reference, ( MetaTypeProvider ) service
);
+            rph.addMetaTypeProvider( this.mti );
+        }
+        else
+        {
+            rph = null;
         }
-
-        final MetaTypeProvider mtp = ( MetaTypeProvider ) this.context.getService( reference
);
-        final RegistrationPropertyHolder rph = new RegistrationPropertyHolder( reference,
mtp );
-
-        rph.addMetaTypeProvider( this.mti );
 
         return rph;
     }
@@ -59,22 +60,23 @@ public class MetaTypeProviderTracker ext
 
     public void modifiedService( ServiceReference reference, Object service )
     {
-        RegistrationPropertyHolder rph = ( RegistrationPropertyHolder ) service;
-        rph.update( this.mti, reference );
+        ( ( RegistrationPropertyHolder ) service ).update( this.mti );
     }
 
 
     public void removedService( ServiceReference reference, Object service )
     {
-        RegistrationPropertyHolder rph = ( RegistrationPropertyHolder ) service;
-        rph.removeMetaTypeProvider( this.mti );
+        ( ( RegistrationPropertyHolder ) service ).removeMetaTypeProvider( this.mti );
         this.context.ungetService( reference );
     }
 
-    private static class RegistrationPropertyHolder
+
+    static class RegistrationPropertyHolder
     {
         private String[] pids;
         private String[] factoryPids;
+
+        private final ServiceReference reference;
         private final MetaTypeProvider provider;
 
 
@@ -83,6 +85,8 @@ public class MetaTypeProviderTracker ext
             this.pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID
);
             this.factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
                 MetaTypeProvider.METATYPE_FACTORY_PID );
+
+            this.reference = reference;
             this.provider = provider;
         }
 
@@ -93,38 +97,50 @@ public class MetaTypeProviderTracker ext
         }
 
 
-        void addMetaTypeProvider( final MetaTypeInformationImpl mti )
+        String[] getPids()
+        {
+            return pids;
+        }
+
+
+        String[] getFactoryPids()
+        {
+            return factoryPids;
+        }
+
+
+        void addMetaTypeProvider( final MetaTypeServiceImpl mti )
         {
             if ( pids != null )
             {
-                mti.addSingletonMetaTypeProvider( pids, provider );
+                mti.addSingletonMetaTypeProvider( reference.getBundle(), pids, provider );
             }
 
             if ( factoryPids != null )
             {
-                mti.addFactoryMetaTypeProvider( factoryPids, provider );
+                mti.addFactoryMetaTypeProvider( reference.getBundle(), factoryPids, provider
);
             }
         }
 
 
-        void removeMetaTypeProvider( final MetaTypeInformationImpl mti )
+        void removeMetaTypeProvider( final MetaTypeServiceImpl mti )
         {
             if ( pids != null )
             {
-                mti.removeSingletonMetaTypeProvider( pids );
+                mti.removeSingletonMetaTypeProvider( reference.getBundle(), pids );
             }
 
             if ( factoryPids != null )
             {
-                mti.removeFactoryMetaTypeProvider( factoryPids );
+                mti.removeFactoryMetaTypeProvider( reference.getBundle(), factoryPids );
             }
         }
 
 
-        void update( final MetaTypeInformationImpl mti, final ServiceReference reference
)
+        void update( final MetaTypeServiceImpl mti )
         {
-            String[] pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID
);
-            String[] factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
+            String[] pids = ServiceMetaTypeInformation.getStringPlus( this.reference, MetaTypeProvider.METATYPE_PID
);
+            String[] factoryPids = ServiceMetaTypeInformation.getStringPlus( this.reference,
                 MetaTypeProvider.METATYPE_FACTORY_PID );
 
             if ( !Arrays.equals( pids, this.pids ) || !Arrays.equals( factoryPids, this.factoryPids
) )

Modified: felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeServiceImpl.java
URL: http://svn.apache.org/viewvc/felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeServiceImpl.java?rev=1403000&r1=1402999&r2=1403000&view=diff
==============================================================================
--- felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeServiceImpl.java
(original)
+++ felix/trunk/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeServiceImpl.java
Sun Oct 28 14:34:50 2012
@@ -20,20 +20,24 @@ package org.apache.felix.metatype.intern
 
 
 import java.io.IOException;
+import java.lang.ref.SoftReference;
 import java.net.URL;
 import java.util.Enumeration;
-import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.felix.metatype.MetaData;
 import org.apache.felix.metatype.MetaDataReader;
+import org.apache.felix.metatype.internal.MetaTypeProviderTracker.RegistrationPropertyHolder;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleEvent;
 import org.osgi.framework.Constants;
+import org.osgi.framework.ServiceReference;
 import org.osgi.framework.SynchronousBundleListener;
 import org.osgi.service.log.LogService;
 import org.osgi.service.metatype.MetaTypeInformation;
+import org.osgi.service.metatype.MetaTypeProvider;
 import org.osgi.service.metatype.MetaTypeService;
 
 
@@ -52,6 +56,7 @@ class MetaTypeServiceImpl implements Met
 
     private final Map bundleMetaTypeInformation;
 
+    private final MetaTypeProviderTracker providerTracker;
 
     /**
      * Creates an instance of this class.
@@ -62,26 +67,19 @@ class MetaTypeServiceImpl implements Met
     MetaTypeServiceImpl( BundleContext bundleContext )
     {
         this.bundleContext = bundleContext;
-        this.bundleMetaTypeInformation = new HashMap();
+        this.bundleMetaTypeInformation = new ConcurrentHashMap();
 
         bundleContext.addBundleListener( this );
+
+        this.providerTracker = new MetaTypeProviderTracker( bundleContext, MetaTypeProvider.class.getName(),
this );
+        this.providerTracker.open();
     }
 
 
     void dispose()
     {
-        MetaTypeInformationImpl[] mti;
-        synchronized ( bundleMetaTypeInformation )
-        {
-            mti = ( MetaTypeInformationImpl[] ) this.bundleMetaTypeInformation.values().toArray(
-                new MetaTypeInformationImpl[bundleMetaTypeInformation.values().size()] );
-            this.bundleMetaTypeInformation.clear();
-        }
-
-        for ( int i = 0; i < mti.length; i++ )
-        {
-            mti[i].dispose();
-        }
+        this.providerTracker.close();
+        this.bundleMetaTypeInformation.clear();
     }
 
 
@@ -89,16 +87,15 @@ class MetaTypeServiceImpl implements Met
     {
         if ( event.getType() == BundleEvent.STOPPING )
         {
-            MetaTypeInformationImpl mti;
-            synchronized ( this.bundleMetaTypeInformation )
+            SoftReference mtir = ( SoftReference ) this.bundleMetaTypeInformation.remove(
new Long( event.getBundle()
+                .getBundleId() ) );
+            if ( mtir != null )
             {
-                mti = ( MetaTypeInformationImpl ) this.bundleMetaTypeInformation.remove(
new Long( event.getBundle()
-                    .getBundleId() ) );
-            }
-
-            if ( mti != null )
-            {
-                mti.dispose();
+                MetaTypeInformationImpl mti = ( MetaTypeInformationImpl ) mtir.get();
+                if ( mti != null )
+                {
+                    mti.dispose();
+                }
             }
         }
     }
@@ -123,12 +120,7 @@ class MetaTypeServiceImpl implements Met
             return null;
         }
 
-        MetaTypeInformation mti;
-        synchronized ( this.bundleMetaTypeInformation )
-        {
-            mti = ( MetaTypeInformation ) this.bundleMetaTypeInformation.get( new Long( bundle.getBundleId()
) );
-        }
-
+        MetaTypeInformationImpl mti = getMetaTypeInformationInternal( bundle );
         if ( mti == null )
         {
             mti = fromDocuments( bundle );
@@ -138,17 +130,14 @@ class MetaTypeServiceImpl implements Met
             }
 
             MetaTypeInformationImpl impl = null;
-            synchronized ( this.bundleMetaTypeInformation )
+            if ( bundle.getState() == Bundle.ACTIVE )
             {
-                if ( bundle.getState() == Bundle.ACTIVE )
-                {
-                    this.bundleMetaTypeInformation.put( new Long( bundle.getBundleId() ),
mti );
-                }
-                else
-                {
-                    impl = ( MetaTypeInformationImpl ) mti;
-                    mti = null;
-                }
+                putMetaTypeInformationInternal( bundle, mti );
+            }
+            else
+            {
+                impl = mti;
+                mti = null;
             }
 
             if ( impl != null )
@@ -161,7 +150,7 @@ class MetaTypeServiceImpl implements Met
     }
 
 
-    private MetaTypeInformation fromDocuments( Bundle bundle )
+    private MetaTypeInformationImpl fromDocuments( Bundle bundle )
     {
         MetaDataReader reader = new MetaDataReader();
 
@@ -191,4 +180,82 @@ class MetaTypeServiceImpl implements Met
         }
         return cmti;
     }
+
+
+    protected void addSingletonMetaTypeProvider( final Bundle bundle, final String[] pids,
MetaTypeProvider mtp )
+    {
+        MetaTypeInformationImpl mti = getMetaTypeInformationInternal( bundle );
+        if ( mti != null )
+        {
+            mti.addSingletonMetaTypeProvider( pids, mtp );
+        }
+    }
+
+
+    protected void addFactoryMetaTypeProvider( final Bundle bundle, final String[] factoryPids,
MetaTypeProvider mtp )
+    {
+        MetaTypeInformationImpl mti = getMetaTypeInformationInternal( bundle );
+        if ( mti != null )
+        {
+            mti.addFactoryMetaTypeProvider( factoryPids, mtp );
+        }
+    }
+
+
+    protected boolean removeSingletonMetaTypeProvider( final Bundle bundle, final String[]
pids )
+    {
+        MetaTypeInformationImpl mti = getMetaTypeInformationInternal( bundle );
+        if ( mti != null )
+        {
+            return mti.removeSingletonMetaTypeProvider( pids );
+        }
+        return false;
+    }
+
+
+    protected boolean removeFactoryMetaTypeProvider( final Bundle bundle, final String[]
factoryPids )
+    {
+        MetaTypeInformationImpl mti = getMetaTypeInformationInternal( bundle );
+        if ( mti != null )
+        {
+            return mti.removeFactoryMetaTypeProvider( factoryPids );
+        }
+        return false;
+    }
+
+
+    private void putMetaTypeInformationInternal( final Bundle bundle, final MetaTypeInformationImpl
mti )
+    {
+        final ServiceReference refs[] = this.providerTracker.getServiceReferences();
+        if ( refs != null )
+        {
+            for ( int i = 0; i < refs.length; i++ )
+            {
+                ServiceReference ref = refs[i];
+                if ( bundle.equals( ref.getBundle() ) )
+                {
+                    final MetaTypeProviderTracker.RegistrationPropertyHolder holder = ( RegistrationPropertyHolder
) this.providerTracker
+                        .getService( ref );
+                    if ( holder.getPids() != null )
+                    {
+                        mti.addSingletonMetaTypeProvider( holder.getPids(), holder.getProvider()
);
+                    }
+                    if ( holder.getFactoryPids() != null )
+                    {
+                        mti.addFactoryMetaTypeProvider( holder.getFactoryPids(), holder.getProvider()
);
+                    }
+                }
+            }
+        }
+
+        this.bundleMetaTypeInformation.put( new Long( bundle.getBundleId() ),
+            new SoftReference( mti ) );
+    }
+
+
+    private MetaTypeInformationImpl getMetaTypeInformationInternal( final Bundle bundle )
+    {
+        SoftReference mtir = ( SoftReference ) this.bundleMetaTypeInformation.get( new Long(
bundle.getBundleId() ) );
+        return ( MetaTypeInformationImpl ) ( ( mtir == null ) ? null : mtir.get() );
+    }
 }



Mime
View raw message