FELIX-3736 Properly handle MetaTypeProvider service modifications
git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1402163 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java b/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
index 107550a..c4417ba 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/internal/MetaTypeProviderTracker.java
@@ -19,6 +19,8 @@
package org.apache.felix.metatype.internal;
+import java.util.Arrays;
+
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;
import org.osgi.service.metatype.MetaTypeProvider;
@@ -46,47 +48,92 @@
return null;
}
- MetaTypeProvider mtp = ( MetaTypeProvider ) super.addingService( reference );
+ final MetaTypeProvider mtp = ( MetaTypeProvider ) this.context.getService( reference );
+ final RegistrationPropertyHolder rph = new RegistrationPropertyHolder( reference, mtp );
- final String[] pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID );
- if ( pids != null )
- {
- mti.addSingletonMetaTypeProvider( pids, mtp );
- }
+ rph.addMetaTypeProvider( this.mti );
- final String[] factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
- MetaTypeProvider.METATYPE_FACTORY_PID );
- if ( factoryPids != null )
- {
- mti.addFactoryMetaTypeProvider( factoryPids, mtp );
- }
-
- return mtp;
+ return rph;
}
public void modifiedService( ServiceReference reference, Object service )
{
- // TODO This is tricky: we must know the registration before the update !!
- super.modifiedService( reference, service );
+ RegistrationPropertyHolder rph = ( RegistrationPropertyHolder ) service;
+ rph.update( this.mti, reference );
}
public void removedService( ServiceReference reference, Object service )
{
- final String[] pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID );
- if ( pids != null )
+ RegistrationPropertyHolder rph = ( RegistrationPropertyHolder ) service;
+ rph.removeMetaTypeProvider( this.mti );
+ this.context.ungetService( reference );
+ }
+
+ private static class RegistrationPropertyHolder
+ {
+ private String[] pids;
+ private String[] factoryPids;
+ private final MetaTypeProvider provider;
+
+
+ RegistrationPropertyHolder( final ServiceReference reference, final MetaTypeProvider provider )
{
- mti.removeSingletonMetaTypeProvider( pids );
+ this.pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID );
+ this.factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
+ MetaTypeProvider.METATYPE_FACTORY_PID );
+ this.provider = provider;
}
- final String[] factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
- MetaTypeProvider.METATYPE_FACTORY_PID );
- if ( factoryPids != null )
+
+ MetaTypeProvider getProvider()
{
- mti.removeFactoryMetaTypeProvider( factoryPids );
+ return provider;
}
- super.removedService( reference, service );
+
+ void addMetaTypeProvider( final MetaTypeInformationImpl mti )
+ {
+ if ( pids != null )
+ {
+ mti.addSingletonMetaTypeProvider( pids, provider );
+ }
+
+ if ( factoryPids != null )
+ {
+ mti.addFactoryMetaTypeProvider( factoryPids, provider );
+ }
+ }
+
+
+ void removeMetaTypeProvider( final MetaTypeInformationImpl mti )
+ {
+ if ( pids != null )
+ {
+ mti.removeSingletonMetaTypeProvider( pids );
+ }
+
+ if ( factoryPids != null )
+ {
+ mti.removeFactoryMetaTypeProvider( factoryPids );
+ }
+ }
+
+
+ void update( final MetaTypeInformationImpl mti, final ServiceReference reference )
+ {
+ String[] pids = ServiceMetaTypeInformation.getStringPlus( reference, MetaTypeProvider.METATYPE_PID );
+ String[] factoryPids = ServiceMetaTypeInformation.getStringPlus( reference,
+ MetaTypeProvider.METATYPE_FACTORY_PID );
+
+ if ( !Arrays.equals( pids, this.pids ) || !Arrays.equals( factoryPids, this.factoryPids ) )
+ {
+ removeMetaTypeProvider( mti );
+ this.pids = pids;
+ this.factoryPids = factoryPids;
+ addMetaTypeProvider( mti );
+ }
+ }
}
}
diff --git a/metatype/src/main/java/org/apache/felix/metatype/internal/ServiceMetaTypeInformation.java b/metatype/src/main/java/org/apache/felix/metatype/internal/ServiceMetaTypeInformation.java
index e573cc6..3a6cdf0 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/internal/ServiceMetaTypeInformation.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/internal/ServiceMetaTypeInformation.java
@@ -302,6 +302,11 @@
res = null;
}
+ if ( res != null )
+ {
+ Arrays.sort( res );
+ }
+
return res;
}