You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@felix.apache.org by cz...@apache.org on 2017/09/21 05:21:15 UTC
svn commit: r1809119 - in /felix/trunk/osgi-r7/configadmin: ./
src/main/java/org/apache/felix/cm/file/
src/main/java/org/apache/felix/cm/impl/
src/main/java/org/apache/felix/cm/impl/persistence/
src/test/java/org/apache/felix/cm/impl/
Author: cziegeler
Date: Thu Sep 21 05:21:14 2017
New Revision: 1809119
URL: http://svn.apache.org/viewvc?rev=1809119&view=rev
Log:
FELIX-5693 : Improve persistent manager handling
Added:
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java (with props)
Removed:
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/LegacyPersistenceManagerTracker.java
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerProvider.java
felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/MockConfigurationManager.java
Modified:
felix/trunk/osgi-r7/configadmin/pom.xml
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/package-info.java
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/Activator.java
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerTracker.java
felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationAdapterTest.java
felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java
Modified: felix/trunk/osgi-r7/configadmin/pom.xml
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/pom.xml?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/pom.xml (original)
+++ felix/trunk/osgi-r7/configadmin/pom.xml Thu Sep 21 05:21:14 2017
@@ -68,7 +68,7 @@
in the IDE launcher.
-->
<properties>
- <felix.java.version>6</felix.java.version>
+ <felix.java.version>7</felix.java.version>
<bundle.build.name>
${basedir}/target
</bundle.build.name>
Modified: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/FilePersistenceManager.java Thu Sep 21 05:21:14 2017
@@ -112,6 +112,12 @@ public class FilePersistenceManager impl
public static final String DEFAULT_CONFIG_DIR = "config";
/**
+ * The name of this persistence manager when registered in the service registry.
+ * (value is "file").
+ */
+ public static final String DEFAULT_PERSISTENCE_MANAGER_NAME = "file";
+
+ /**
* The extension of the configuration files.
*/
private static final String FILE_EXT = ".config";
@@ -804,7 +810,7 @@ public class FilePersistenceManager impl
DictionaryEnumeration()
{
- dirStack = new Stack<File>();
+ dirStack = new Stack<>();
fileList = null;
idx = 0;
Modified: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/package-info.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/package-info.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/package-info.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/file/package-info.java Thu Sep 21 05:21:14 2017
@@ -17,7 +17,7 @@
* under the License.
*/
-@org.osgi.annotation.versioning.Version("1.0.0")
+@org.osgi.annotation.versioning.Version("1.1.0")
package org.apache.felix.cm.file;
Modified: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/Activator.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/Activator.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/Activator.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/Activator.java Thu Sep 21 05:21:14 2017
@@ -18,24 +18,19 @@
*/
package org.apache.felix.cm.impl;
-import java.io.IOException;
import java.util.Dictionary;
import java.util.Hashtable;
-import java.util.SortedMap;
-import java.util.TreeMap;
import org.apache.felix.cm.PersistenceManager;
import org.apache.felix.cm.file.FilePersistenceManager;
-import org.apache.felix.cm.impl.persistence.LegacyPersistenceManagerTracker;
+import org.apache.felix.cm.impl.persistence.PersistenceManagerTracker;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
+import org.osgi.framework.BundleException;
import org.osgi.framework.Constants;
-import org.osgi.framework.ServiceReference;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceRegistration;
-import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.log.LogService;
-import org.osgi.util.tracker.ServiceTracker;
-import org.osgi.util.tracker.ServiceTrackerCustomizer;
/**
* Activator for the configuration admin implementation.
@@ -73,127 +68,38 @@ public class Activator implements Bundle
*/
private static final String CM_CONFIG_PM = "felix.cm.pm";
- private volatile ConfigurationManager manager;
+ private volatile PersistenceManagerTracker tracker;
// the service registration of the default file persistence manager
private volatile ServiceRegistration<PersistenceManager> filepmRegistration;
- // service tracker for optional coordinator
- @SuppressWarnings("rawtypes")
- private volatile ServiceTracker coordinatorTracker;
-
- private volatile LegacyPersistenceManagerTracker legacyProvider;
-
- private PersistenceManager registerFilePersistenceManager(final BundleContext bundleContext) {
- try
- {
- final FilePersistenceManager fpm = new FilePersistenceManager( bundleContext,
- bundleContext.getProperty( CM_CONFIG_DIR ) );
- final Dictionary<String, Object> props = new Hashtable<String, Object>();
- props.put( Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager" );
- props.put( Constants.SERVICE_VENDOR, "The Apache Software Foundation" );
- props.put( Constants.SERVICE_RANKING, new Integer( Integer.MIN_VALUE ) );
- props.put( PersistenceManager.PROPERTY_NAME, "file");
- filepmRegistration = bundleContext.registerService( PersistenceManager.class, fpm, props );
-
- return fpm;
- }
- catch ( IllegalArgumentException iae )
- {
- Log.logger.log( LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae );
- }
- return null;
- }
-
- @SuppressWarnings({ "unchecked", "rawtypes" })
@Override
- public void start( final BundleContext bundleContext )
+ public void start( final BundleContext bundleContext ) throws BundleException
{
// setup log
Log.logger.start(bundleContext);
// register default file persistence manager
- final PersistenceManager fpm = registerFilePersistenceManager(bundleContext);
-
- this.manager = new ConfigurationManager();
- // start coordinator tracker
- coordinatorTracker = new ServiceTracker(bundleContext, "org.osgi.service.coordinator.Coordinator",
- new ServiceTrackerCustomizer()
- {
- private final SortedMap<ServiceReference, Object> sortedServices = new TreeMap<ServiceReference, Object>();
-
- @Override
- public Object addingService(final ServiceReference reference)
- {
- final Object srv = bundleContext.getService(reference);
- if ( srv != null )
- {
- synchronized ( this.sortedServices )
- {
- sortedServices.put(reference, srv);
- manager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
- }
- }
- return srv;
- }
-
- @Override
- public void modifiedService(final ServiceReference reference, final Object srv) {
- synchronized ( this.sortedServices )
- {
- // update the map, service ranking might have changed
- sortedServices.remove(reference);
- sortedServices.put(reference, srv);
- manager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
- }
- }
-
- @Override
- public void removedService(final ServiceReference reference, final Object service) {
- synchronized ( this.sortedServices )
- {
- sortedServices.remove(reference);
- if ( sortedServices.isEmpty() )
- {
- manager.setCoordinator(null);
- }
- else
- {
- manager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
- }
- }
- bundleContext.ungetService(reference);
- }
- });
- coordinatorTracker.open();
-
- DynamicBindings dynamicBindings = null;
- final String configuredPM = bundleContext.getProperty(CM_CONFIG_PM);
- if ( configuredPM == null || configuredPM.isEmpty() )
- {
- // set up the location (might throw IllegalArgumentException)
- try
- {
- // setup dynamic configuration bindings
- dynamicBindings = new DynamicBindings( bundleContext, fpm );
- }
- catch ( IOException ioe )
- {
- Log.logger.log( LogService.LOG_ERROR, "Failure setting up dynamic configuration bindings", ioe );
- }
-
- legacyProvider = new LegacyPersistenceManagerTracker();
- legacyProvider.start(bundleContext);
- }
- else
+ final PersistenceManager defaultPM = this.registerFilePersistenceManager(bundleContext);
+ if ( defaultPM == null )
{
-// TODO - setup tracker and only start if PM is available
+ throw new BundleException("Unable to register default persistence manager.");
}
- // start configuration manager implementation
- final ServiceReference<ConfigurationAdmin> ref = this.manager.start(dynamicBindings, legacyProvider, bundleContext);
- // update log
- Log.logger.set(ref);
+ String configuredPM = bundleContext.getProperty(CM_CONFIG_PM);
+ if ( configuredPM != null && configuredPM.isEmpty() )
+ {
+ configuredPM = null;
+ }
+ try
+ {
+ this.tracker = new PersistenceManagerTracker(bundleContext, defaultPM, configuredPM);
+ }
+ catch ( InvalidSyntaxException iae )
+ {
+ Log.logger.log( LogService.LOG_ERROR, "Cannot create the persistence manager tracker", iae );
+ throw new BundleException(iae.getMessage(), iae);
+ }
}
@@ -203,31 +109,46 @@ public class Activator implements Bundle
// stop logger
Log.logger.stop();
- // stop configuration manager implementation
- if ( this.manager != null )
+ // stop tracker and configuration manager implementation
+ if ( this.tracker != null )
{
- this.manager.stop(bundleContext);
- this.manager = null;
+ this.tracker.stop();
+ this.tracker = null;
}
- if ( this.legacyProvider != null )
+ // shutdown the file persistence manager and unregister
+ this.unregisterFilePersistenceManager();
+ }
+
+ private PersistenceManager registerFilePersistenceManager(final BundleContext bundleContext)
+ {
+ try
{
- legacyProvider.stop();
- legacyProvider = null;
- }
+ final FilePersistenceManager fpm = new FilePersistenceManager( bundleContext,
+ bundleContext.getProperty( CM_CONFIG_DIR ) );
+ final Dictionary<String, Object> props = new Hashtable<>();
+ props.put( Constants.SERVICE_DESCRIPTION, "Platform Filesystem Persistence Manager" );
+ props.put( Constants.SERVICE_VENDOR, "The Apache Software Foundation" );
+ props.put( Constants.SERVICE_RANKING, new Integer( Integer.MIN_VALUE ) );
+ props.put( PersistenceManager.PROPERTY_NAME, FilePersistenceManager.DEFAULT_PERSISTENCE_MANAGER_NAME);
+ filepmRegistration = bundleContext.registerService( PersistenceManager.class, fpm, props );
+
+ return fpm;
- // stop coordinator tracker
- if ( this.coordinatorTracker != null )
+ }
+ catch ( final IllegalArgumentException iae )
{
- this.coordinatorTracker.close();
- this.coordinatorTracker = null;
+ Log.logger.log( LogService.LOG_ERROR, "Cannot create the FilePersistenceManager", iae );
}
+ return null;
+ }
- // shutdown the file persistence manager
- if ( filepmRegistration != null )
+ private void unregisterFilePersistenceManager()
+ {
+ if ( this.filepmRegistration != null )
{
- filepmRegistration.unregister();
- filepmRegistration = null;
+ this.filepmRegistration.unregister();
+ this.filepmRegistration = null;
}
}
}
Modified: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/ConfigurationManager.java Thu Sep 21 05:21:14 2017
@@ -40,7 +40,6 @@ import org.apache.felix.cm.impl.helper.M
import org.apache.felix.cm.impl.helper.TargetedPID;
import org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy;
import org.apache.felix.cm.impl.persistence.ExtPersistenceManager;
-import org.apache.felix.cm.impl.persistence.PersistenceManagerProvider;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleEvent;
@@ -66,8 +65,6 @@ import org.osgi.util.tracker.ServiceTrac
* implementation of the Configuration Admin Service Specification. As such it
* has the following tasks:
* <ul>
- * <li>It is a <code>BundleActivator</code> which is called when the bundle
- * is started and stopped.
* <li>It is a <code>BundleListener</code> which gets informed when the
* states of bundles change. Mostly this is needed to unbind any bound
* configuration in case a bundle is uninstalled.
@@ -91,7 +88,7 @@ public class ConfigurationManager implem
private static Random numberGenerator;
// the BundleContext of the Configuration Admin Service bundle
- BundleContext bundleContext;
+ private final BundleContext bundleContext;
// the service registration of the configuration admin
private volatile ServiceRegistration<ConfigurationAdmin> configurationAdminRegistration;
@@ -115,16 +112,16 @@ public class ConfigurationManager implem
private UpdateThread eventThread;
/**
- * The persistence manager provider
+ * The persistence manager
*/
- private PersistenceManagerProvider persistenceManagerProvider;
+ private final ExtPersistenceManager persistenceManager;
// the cache of Factory instances mapped by their factory PID
- private final HashMap<String, Factory> factories = new HashMap<String, Factory>();
+ private final HashMap<String, Factory> factories = new HashMap<>();
// the cache of Configuration instances mapped by their PID
// have this always set to prevent NPE on bundle shutdown
- private final HashMap<String, ConfigurationImpl> configurations = new HashMap<String, ConfigurationImpl>();
+ private final HashMap<String, ConfigurationImpl> configurations = new HashMap<>();
/**
* The map of dynamic configuration bindings. This maps the
@@ -137,7 +134,7 @@ public class ConfigurationManager implem
* <p>
* The map is written to persistence on each change.
*/
- private DynamicBindings dynamicBindings;
+ private final DynamicBindings dynamicBindings;
// flag indicating whether BundleChange events should be consumed (FELIX-979)
private volatile boolean handleBundleEvents;
@@ -148,19 +145,22 @@ public class ConfigurationManager implem
// Coordinator service if available
private volatile Object coordinator;
- public ServiceReference<ConfigurationAdmin> start(
- final DynamicBindings dynBin,
- final PersistenceManagerProvider provider,
- final BundleContext bundleContext )
+ public ConfigurationManager(final ExtPersistenceManager persistenceManager,
+ final BundleContext bundleContext)
+ throws IOException
{
// set up some fields
this.bundleContext = bundleContext;
- this.dynamicBindings = dynBin;
+ this.dynamicBindings = new DynamicBindings( bundleContext, persistenceManager );
+ this.persistenceManager = persistenceManager;
+ }
+ public ServiceReference<ConfigurationAdmin> start()
+ {
// configurationlistener support
- configurationListenerTracker = new ServiceTracker<ConfigurationListener, ConfigurationListener>( bundleContext, ConfigurationListener.class, null );
+ configurationListenerTracker = new ServiceTracker<>( bundleContext, ConfigurationListener.class, null );
configurationListenerTracker.open();
- syncConfigurationListenerTracker = new ServiceTracker<SynchronousConfigurationListener, SynchronousConfigurationListener>( bundleContext,
+ syncConfigurationListenerTracker = new ServiceTracker<>( bundleContext,
SynchronousConfigurationListener.class, null );
syncConfigurationListenerTracker.open();
@@ -174,16 +174,13 @@ public class ConfigurationManager implem
handleBundleEvents = true;
bundleContext.addBundleListener( this );
- // get all persistence managers to begin with
- this.persistenceManagerProvider = provider;
-
// consider alive now (before clients use Configuration Admin
// service registered in the next step)
isActive = true;
// create and register configuration admin - start after PM tracker ...
ConfigurationAdminFactory caf = new ConfigurationAdminFactory( this );
- Dictionary<String, Object> props = new Hashtable<String, Object>();
+ Dictionary<String, Object> props = new Hashtable<>();
props.put( Constants.SERVICE_PID, "org.apache.felix.cm.ConfigurationAdmin" );
props.put( Constants.SERVICE_DESCRIPTION, "Configuration Admin Service Specification 1.6 Implementation" );
props.put( Constants.SERVICE_VENDOR, "The Apache Software Foundation" );
@@ -202,7 +199,7 @@ public class ConfigurationManager implem
}
- public void stop( BundleContext bundleContext )
+ public void stop( )
{
// stop handling bundle events immediately
@@ -262,8 +259,6 @@ public class ConfigurationManager implem
{
factories.clear();
}
-
- this.bundleContext = null;
}
@@ -480,20 +475,16 @@ public class ConfigurationManager implem
return config;
}
- PersistenceManager[] pmList = this.persistenceManagerProvider.getPersistenceManagers();
- for ( int i = 0; i < pmList.length; i++ )
+ if ( this.persistenceManager.exists( pid ) )
{
- if ( pmList[i].exists( pid ) )
- {
- Dictionary props = pmList[i].load( pid );
- config = new ConfigurationImpl( this, pmList[i], props );
- Log.logger.log( LogService.LOG_DEBUG, "Found existing configuration {0} bound to {1}", new Object[]
- { pid, config.getBundleLocation() } );
- return cacheConfiguration( config );
- }
+ final Dictionary props =this.persistenceManager.load( pid );
+ config = new ConfigurationImpl( this, this.persistenceManager, props );
+ Log.logger.log( LogService.LOG_DEBUG, "Found existing configuration {0} bound to {1}", new Object[]
+ { pid, config.getBundleLocation() } );
+ return cacheConfiguration( config );
}
- // neither the cache nor any persistence manager has configuration
+ // neither the cache nor the persistence manager has configuration
return null;
}
@@ -542,60 +533,56 @@ public class ConfigurationManager implem
Log.logger.log( LogService.LOG_DEBUG, "Listing configurations matching {0}", new Object[]
{ filterString } );
- List<ConfigurationImpl> configList = new ArrayList<ConfigurationImpl>();
+ List<ConfigurationImpl> configList = new ArrayList<>();
- ExtPersistenceManager[] pmList = this.persistenceManagerProvider.getPersistenceManagers();
- for ( int i = 0; i < pmList.length; i++ )
+ Collection<Dictionary> configs = this.persistenceManager.getDictionaries(filter );
+ for(final Dictionary config : configs)
{
- Collection<Dictionary> configs = pmList[i].getDictionaries(filter );
- for(final Dictionary config : configs)
+ // ignore non-Configuration dictionaries
+ final String pid = ( String ) config.get( Constants.SERVICE_PID );
+ if ( pid == null )
{
- // ignore non-Configuration dictionaries
- final String pid = ( String ) config.get( Constants.SERVICE_PID );
- if ( pid == null )
- {
- continue;
- }
+ continue;
+ }
- // CM 1.4 / 104.13.2.3 Permission required
- if ( !configurationAdmin.hasPermission( this,
- ( String ) config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ) )
- {
- Log.logger.log(
- LogService.LOG_DEBUG,
- "Omitting configuration {0}: No permission for bundle {1} on configuration bound to {2}",
- new Object[]
- { pid, configurationAdmin.getBundle().getLocation(),
- config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) } );
- continue;
- }
+ // CM 1.4 / 104.13.2.3 Permission required
+ if ( !configurationAdmin.hasPermission( this,
+ ( String ) config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) ) )
+ {
+ Log.logger.log(
+ LogService.LOG_DEBUG,
+ "Omitting configuration {0}: No permission for bundle {1} on configuration bound to {2}",
+ new Object[]
+ { pid, configurationAdmin.getBundle().getLocation(),
+ config.get( ConfigurationAdmin.SERVICE_BUNDLELOCATION ) } );
+ continue;
+ }
- // ensure the service.pid and returned a cached config if available
- ConfigurationImpl cfg = null;
- if ( pmList[i] instanceof CachingPersistenceManagerProxy)
- {
- cfg = getCachedConfiguration( pid );
- if (cfg == null) {
- cfg = new ConfigurationImpl(this, pmList[i], config);
- // add the to configurations cache if it wasn't in the cache
- cacheConfiguration(cfg);
- }
- } else {
- cfg = new ConfigurationImpl( this, pmList[i], config );
+ // ensure the service.pid and returned a cached config if available
+ ConfigurationImpl cfg = null;
+ if ( this.persistenceManager instanceof CachingPersistenceManagerProxy)
+ {
+ cfg = getCachedConfiguration( pid );
+ if (cfg == null) {
+ cfg = new ConfigurationImpl(this, this.persistenceManager, config);
+ // add the to configurations cache if it wasn't in the cache
+ cacheConfiguration(cfg);
}
+ } else {
+ cfg = new ConfigurationImpl( this, this.persistenceManager, config );
+ }
- // FELIX-611: Ignore configuration objects without props
- if ( !cfg.isNew() )
- {
- Log.logger.log( LogService.LOG_DEBUG, "Adding configuration {0}", new Object[]
- { pid } );
- configList.add( cfg );
- }
- else
- {
- Log.logger.log( LogService.LOG_DEBUG, "Omitting configuration {0}: Is new", new Object[]
- { pid } );
- }
+ // FELIX-611: Ignore configuration objects without props
+ if ( !cfg.isNew() )
+ {
+ Log.logger.log( LogService.LOG_DEBUG, "Adding configuration {0}", new Object[]
+ { pid } );
+ configList.add( cfg );
+ }
+ else
+ {
+ Log.logger.log( LogService.LOG_DEBUG, "Omitting configuration {0}: Is new", new Object[]
+ { pid } );
}
}
@@ -834,7 +821,7 @@ public class ConfigurationManager implem
{
Log.logger.log( LogService.LOG_DEBUG, "createConfiguration({0}, {1}, {2})", new Object[]
{ pid, factoryPid, bundleLocation } );
- return new ConfigurationImpl( this, this.persistenceManagerProvider.getPersistenceManagers()[0], pid, factoryPid, bundleLocation );
+ return new ConfigurationImpl( this, this.persistenceManager, pid, factoryPid, bundleLocation );
}
@@ -855,7 +842,7 @@ public class ConfigurationManager implem
*/
List<Factory> getTargetedFactories( final String rawFactoryPid, final ServiceReference target ) throws IOException
{
- LinkedList<Factory> factories = new LinkedList<Factory>();
+ LinkedList<Factory> factories = new LinkedList<>();
final Bundle serviceBundle = target.getBundle();
if ( serviceBundle != null )
@@ -931,15 +918,11 @@ public class ConfigurationManager implem
}
// try to load factory from persistence
- PersistenceManager[] pmList = this.persistenceManagerProvider.getPersistenceManagers();
- for ( int i = 0; i < pmList.length; i++ )
+ if ( Factory.exists( this.persistenceManager, factoryPid ) )
{
- if ( Factory.exists( pmList[i], factoryPid ) )
- {
- factory = Factory.load( this, pmList[i], factoryPid );
- cacheFactory( factory );
- return factory;
- }
+ factory = Factory.load( this, this.persistenceManager, factoryPid );
+ cacheFactory( factory );
+ return factory;
}
// no existing factory
@@ -952,7 +935,7 @@ public class ConfigurationManager implem
*/
Factory createFactory( String factoryPid )
{
- Factory factory = new Factory( this, this.persistenceManagerProvider.getPersistenceManagers()[0], factoryPid );
+ Factory factory = new Factory( this, this.persistenceManager, factoryPid );
cacheFactory( factory );
return factory;
}
Modified: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerTracker.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerTracker.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerTracker.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/PersistenceManagerTracker.java Thu Sep 21 05:21:14 2017
@@ -19,84 +19,328 @@
package org.apache.felix.cm.impl.persistence;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.SortedMap;
+import java.util.TreeMap;
+
+import org.apache.felix.cm.NotCachablePersistenceManager;
import org.apache.felix.cm.PersistenceManager;
-import org.apache.felix.cm.impl.RankingComparator;
+import org.apache.felix.cm.impl.ConfigurationManager;
+import org.apache.felix.cm.impl.Log;
import org.osgi.framework.BundleContext;
import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
import org.osgi.util.tracker.ServiceTrackerCustomizer;
-
-public class PersistenceManagerTracker implements PersistenceManagerProvider, ServiceTrackerCustomizer<PersistenceManager, PersistenceManager>
+/**
+ * This tracker tracks registered persistence managers and
+ * if the required PM becomes available, configuration admin
+ * is registered.
+ * Service ranking of registered persistence managers
+ * is respected.
+ */
+public class PersistenceManagerTracker
+ implements ServiceTrackerCustomizer<PersistenceManager, PersistenceManagerTracker.Holder>
{
- // PersistenceManager services
- private ServiceTracker<PersistenceManager, PersistenceManager> persistenceManagerTracker;
+ /** Tracker for the persistence manager. */
+ private final ServiceTracker<PersistenceManager, Holder> persistenceManagerTracker;
- /**
- * The actual list of {@link PersistenceManager persistence managers} to use
- * when looking for configuration data. This list is built from the
- * {@link #persistenceManagerMap}, which is ordered according to the
- * {@link RankingComparator}.
- */
- private ExtPersistenceManager[] persistenceManagers;
+ private final List<Holder> holders = new ArrayList<>();
+
+ private final WorkerQueue workerQueue;
private final BundleContext bundleContext;
- public PersistenceManagerTracker(final BundleContext bundleContext, final String pmName )
+ private volatile ConfigurationManager configurationManager;
+
+ // service tracker for optional coordinator
+ private volatile ServiceTracker<Object, Object> coordinatorTracker;
+
+ public PersistenceManagerTracker(final BundleContext bundleContext,
+ final PersistenceManager defaultPM,
+ final String pmName )
+ throws InvalidSyntaxException
{
this.bundleContext = bundleContext;
- persistenceManagerTracker = new ServiceTracker<PersistenceManager, PersistenceManager>( bundleContext,
- "(&(" + Constants.OBJECTCLASS + "=" + PersistenceManager.class.getName() + ")(name=" + pmName + "))",
- this );
- persistenceManagerTracker.open();
+ if ( pmName != null )
+ {
+ Log.logger.log(LogService.LOG_DEBUG, "Using persistence manager {0}", new Object[] {pmName});
+ this.workerQueue = new WorkerQueue();
+ this.persistenceManagerTracker = new ServiceTracker<>( bundleContext,
+ bundleContext.createFilter("(&(" + Constants.OBJECTCLASS + "=" + PersistenceManager.class.getName() + ")(name=" + pmName + "))"),
+ this );
+ this.persistenceManagerTracker.open();
+ }
+ else
+ {
+ Log.logger.log(LogService.LOG_DEBUG, "Using default persistence manager", (Object[])null);
+ this.workerQueue = null;
+ this.persistenceManagerTracker = null;
+ this.activate(this.createPersistenceManagerProxy(defaultPM));
+ }
}
-
+ /**
+ * Stop the tracker, stop configuration admin
+ */
public void stop( )
{
- persistenceManagerTracker.close();
+ if ( this.persistenceManagerTracker != null )
+ {
+ this.workerQueue.stop();
+ this.persistenceManagerTracker.close();
+ }
+ else
+ {
+ this.deactivate();
+ }
+ }
+
+ private void activate(final ExtPersistenceManager pm)
+ {
+ try
+ {
+ configurationManager = new ConfigurationManager(pm, bundleContext);
+ // start coordinator tracker
+ this.startCoordinatorTracker();
+
+ final ServiceReference<ConfigurationAdmin> ref = configurationManager.start();
+ // update log
+ Log.logger.set(ref);
+
+ }
+ catch (final IOException ioe )
+ {
+ Log.logger.log( LogService.LOG_ERROR, "Failure setting up dynamic configuration bindings", ioe );
+ }
+ }
+
+ private void deactivate()
+ {
+ this.configurationManager.stop();
+ this.configurationManager = null;
+ this.stopCoordinatorTracker();
+ // update log
+ Log.logger.set(null);
}
+ private ExtPersistenceManager createPersistenceManagerProxy(final PersistenceManager pm)
+ {
+ final ExtPersistenceManager extPM;
+ if ( pm instanceof NotCachablePersistenceManager )
+ {
+ extPM = new PersistenceManagerProxy( pm );
+ }
+ else
+ {
+ extPM = new CachingPersistenceManagerProxy( pm );
+ }
+ return extPM;
+ }
@Override
- public PersistenceManager addingService(final ServiceReference<PersistenceManager> reference)
+ public Holder addingService(final ServiceReference<PersistenceManager> reference)
{
- if ( persistenceManagers == null )
+ final PersistenceManager pm = this.bundleContext.getService(reference);
+ if ( pm != null )
{
- final PersistenceManager pm = this.bundleContext.getService(reference);
- if ( pm != null )
+ final ExtPersistenceManager extPM = createPersistenceManagerProxy(pm);
+ final Holder holder = new Holder(reference, extPM);
+
+ synchronized ( this.holders )
{
- // activate
- return pm;
+ final Holder oldHolder = this.holders.isEmpty() ? null : this.holders.get(0);
+ this.holders.add(holder);
+ Collections.sort(holders);
+ if ( holders.get(0) == holder )
+ {
+ this.workerQueue.enqueue(new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ if ( oldHolder != null )
+ {
+ deactivate();
+ }
+ activate(holder.getPersistenceManager());
+ }
+ });
+ }
}
+ return holder;
}
return null;
}
@Override
- public void modifiedService(final ServiceReference<PersistenceManager> reference, final PersistenceManager service)
+ public void modifiedService(final ServiceReference<PersistenceManager> reference, final Holder holder)
{
- // nothing to do
+ // find the old holder, remove, add new holder, sort
+ synchronized ( this.holders )
+ {
+ final Holder oldHolder = this.holders.isEmpty() ? null : this.holders.get(0);
+
+ this.holders.remove(holder);
+ this.holders.add(new Holder(reference, holder.getPersistenceManager()));
+ Collections.sort(this.holders);
+ if ( holders.get(0) == holder && oldHolder.compareTo(holder) != 0 )
+ {
+ this.workerQueue.enqueue(new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ deactivate();
+ activate(holder.getPersistenceManager());
+ }
+ });
+ }
+ }
+
}
@Override
- public void removedService(final ServiceReference<PersistenceManager> reference, final PersistenceManager service)
+ public void removedService(final ServiceReference<PersistenceManager> reference,
+ final Holder holder)
{
- if ( persistenceManagers != null && persistenceManagers[0].getDelegatee() == service )
+ synchronized ( this.holders )
{
- // deactivate
- persistenceManagers = null;
+ final boolean deactivate = holders.get(0) == holder;
+ this.holders.remove(holder);
+ if ( deactivate )
+ {
+ this.workerQueue.enqueue(new Runnable()
+ {
+
+ @Override
+ public void run()
+ {
+ deactivate();
+ if ( !holders.isEmpty() )
+ {
+ activate(holders.get(0).getPersistenceManager());
+ }
+ }
+ });
+ }
}
}
+ public static final class Holder implements Comparable<Holder>
+ {
+ private final ServiceReference<PersistenceManager> reference;
- @Override
- public ExtPersistenceManager[] getPersistenceManagers()
+ private final ExtPersistenceManager manager;
+
+ public Holder(final ServiceReference<PersistenceManager> ref, final ExtPersistenceManager epm)
+ {
+ this.reference = ref;
+ this.manager = epm;
+ }
+
+ public ExtPersistenceManager getPersistenceManager()
+ {
+ return this.manager;
+ }
+
+ @Override
+ public int compareTo(final Holder o)
+ {
+ // sort, highest first
+ return -reference.compareTo(o.reference);
+ }
+
+ @Override
+ public int hashCode()
+ {
+ return this.reference.hashCode();
+ }
+
+ @Override
+ public boolean equals(final Object obj)
+ {
+ if (this == obj)
+ {
+ return true;
+ }
+ if (obj == null || getClass() != obj.getClass())
+ {
+ return false;
+ }
+ final Holder other = (Holder) obj;
+ return this.reference.equals(other.reference);
+ }
+ }
+
+ private void startCoordinatorTracker()
+ {
+ this.coordinatorTracker = new ServiceTracker<>(bundleContext, "org.osgi.service.coordinator.Coordinator",
+ new ServiceTrackerCustomizer<Object, Object>()
+ {
+ private final SortedMap<ServiceReference<Object>, Object> sortedServices = new TreeMap<>();
+
+ @Override
+ public Object addingService(final ServiceReference<Object> reference)
+ {
+ final Object srv = bundleContext.getService(reference);
+ if ( srv != null )
+ {
+ synchronized ( this.sortedServices )
+ {
+ sortedServices.put(reference, srv);
+ configurationManager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
+ }
+ }
+ return srv;
+ }
+
+ @Override
+ public void modifiedService(final ServiceReference<Object> reference, final Object srv) {
+ synchronized ( this.sortedServices )
+ {
+ // update the map, service ranking might have changed
+ sortedServices.remove(reference);
+ sortedServices.put(reference, srv);
+ configurationManager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
+ }
+ }
+
+ @Override
+ public void removedService(final ServiceReference<Object> reference, final Object service) {
+ synchronized ( this.sortedServices )
+ {
+ sortedServices.remove(reference);
+ if ( sortedServices.isEmpty() )
+ {
+ configurationManager.setCoordinator(null);
+ }
+ else
+ {
+ configurationManager.setCoordinator(sortedServices.get(sortedServices.lastKey()));
+ }
+ }
+ bundleContext.ungetService(reference);
+ }
+ });
+ coordinatorTracker.open();
+ }
+
+ private void stopCoordinatorTracker()
{
- return persistenceManagers;
+ this.coordinatorTracker.close();
+ this.coordinatorTracker = null;
}
}
Added: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java?rev=1809119&view=auto
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java (added)
+++ felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java Thu Sep 21 05:21:14 2017
@@ -0,0 +1,85 @@
+/*
+ * 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.felix.cm.impl.persistence;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ThreadFactory;
+
+import org.apache.felix.cm.impl.Log;
+import org.osgi.service.log.LogService;
+
+public class WorkerQueue implements Runnable {
+
+ private final ThreadFactory threadFactory;
+
+ private final List<Runnable> tasks = new ArrayList<Runnable>();
+
+ private volatile Thread backgroundThread;
+
+ private volatile boolean stopped = false;
+
+ public WorkerQueue() {
+ this.threadFactory = Executors.defaultThreadFactory();
+ }
+
+ public void stop() {
+ synchronized ( this.tasks ) {
+ this.stopped = true;
+ }
+ }
+
+ public void enqueue(final Runnable r) {
+ synchronized ( this.tasks ) {
+ if ( !this.stopped ) {
+ this.tasks.add(r);
+ if ( this.backgroundThread == null ) {
+ this.backgroundThread = this.threadFactory.newThread(this);
+ this.backgroundThread.setDaemon(true);
+ this.backgroundThread.setName("Apache Felix Configuration Admin Activator Thread");
+ this.backgroundThread.start();
+ }
+ }
+ }
+ }
+
+ @Override
+ public void run() {
+ Runnable r;
+ do {
+ r = null;
+ synchronized ( this.tasks ) {
+ if ( !this.stopped && !this.tasks.isEmpty() ) {
+ r = this.tasks.remove(0);
+ } else {
+ this.backgroundThread = null;
+ }
+ }
+ if ( r != null ) {
+ try {
+ r.run();
+ } catch ( final Throwable t) {
+ // just to be sure our loop never dies
+ Log.logger.log(LogService.LOG_ERROR, "Error processing task", t);
+ }
+ }
+ } while ( r != null );
+ }
+}
Propchange: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: felix/trunk/osgi-r7/configadmin/src/main/java/org/apache/felix/cm/impl/persistence/WorkerQueue.java
------------------------------------------------------------------------------
svn:keywords = author date id revision rev url
Modified: felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationAdapterTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationAdapterTest.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationAdapterTest.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationAdapterTest.java Thu Sep 21 05:21:14 2017
@@ -19,6 +19,11 @@
package org.apache.felix.cm.impl;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
@@ -26,15 +31,15 @@ import java.util.Collection;
import java.util.Dictionary;
import java.util.Hashtable;
-import junit.framework.TestCase;
-
import org.apache.felix.cm.MockPersistenceManager;
import org.apache.felix.cm.PersistenceManager;
+import org.junit.Test;
+import org.mockito.Mockito;
import org.osgi.framework.Constants;
import org.osgi.service.cm.Configuration;
-public class ConfigurationAdapterTest extends TestCase
+public class ConfigurationAdapterTest
{
private static final String SCALAR = "scalar";
@@ -51,13 +56,6 @@ public class ConfigurationAdapterTest ex
private static final String TEST_LOCATION = "test:location";
private final PersistenceManager pm = new MockPersistenceManager();
- private final MockConfigurationManager configMgr = new MockConfigurationManager()
- {
- boolean isActive()
- {
- return true;
- }
- };
{
ARRAY_VALUE = new String[]
@@ -69,12 +67,15 @@ public class ConfigurationAdapterTest ex
private Configuration getConfiguration() throws IOException
{
+ final ConfigurationManager configMgr = Mockito.mock(ConfigurationManager.class);
+ Mockito.when(configMgr.isActive()).thenReturn(true);
+
ConfigurationImpl cimpl = new ConfigurationImpl( configMgr, pm, TEST_PID, null, TEST_LOCATION );
return new ConfigurationAdapter( null, cimpl );
}
- public void testScalar() throws IOException
+ @Test public void testScalar() throws IOException
{
Configuration cimpl = getConfiguration();
Dictionary props = cimpl.getProperties();
@@ -92,7 +93,7 @@ public class ConfigurationAdapterTest ex
}
- public void testArray() throws IOException
+ @Test public void testArray() throws IOException
{
Configuration cimpl = getConfiguration();
@@ -127,7 +128,7 @@ public class ConfigurationAdapterTest ex
}
- public void testCollection() throws IOException
+ @Test public void testCollection() throws IOException
{
Configuration cimpl = getConfiguration();
Modified: felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java
URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java?rev=1809119&r1=1809118&r2=1809119&view=diff
==============================================================================
--- felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java (original)
+++ felix/trunk/osgi-r7/configadmin/src/test/java/org/apache/felix/cm/impl/ConfigurationManagerTest.java Thu Sep 21 05:21:14 2017
@@ -19,7 +19,11 @@
package org.apache.felix.cm.impl;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
import java.io.ByteArrayOutputStream;
+import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.util.Dictionary;
@@ -35,9 +39,10 @@ import org.apache.felix.cm.MockNotCachab
import org.apache.felix.cm.MockPersistenceManager;
import org.apache.felix.cm.PersistenceManager;
import org.apache.felix.cm.impl.persistence.CachingPersistenceManagerProxy;
-import org.apache.felix.cm.impl.persistence.ExtPersistenceManager;
-import org.apache.felix.cm.impl.persistence.LegacyPersistenceManagerTracker;
import org.apache.felix.cm.impl.persistence.PersistenceManagerProxy;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
import org.mockito.Mockito;
import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
@@ -48,9 +53,7 @@ import org.osgi.service.cm.SynchronousCo
import org.osgi.service.log.LogService;
import org.osgi.util.tracker.ServiceTracker;
-import junit.framework.TestCase;
-
-public class ConfigurationManagerTest extends TestCase
+public class ConfigurationManagerTest
{
private PrintStream replacedStdErr;
@@ -58,11 +61,9 @@ public class ConfigurationManagerTest ex
private ByteArrayOutputStream output;
- @Override
- protected void setUp() throws Exception
+ @Before
+ public void setUp() throws Exception
{
- super.setUp();
-
replacedStdErr = System.err;
output = new ByteArrayOutputStream();
@@ -71,43 +72,30 @@ public class ConfigurationManagerTest ex
}
- @Override
- protected void tearDown() throws Exception
+ @After
+ public void tearDown() throws Exception
{
System.setErr( replacedStdErr );
-
- super.tearDown();
}
- public void test_listConfigurations_cached() throws Exception
+ @Test public void test_listConfigurations_cached() throws Exception
{
String pid = "testDefaultPersistenceManager";
- ConfigurationManager configMgr = new ConfigurationManager();
- setServiceTrackerField( configMgr, "persistenceManagerTracker" );
-
- Field field1 = configMgr.getClass().getDeclaredField( "persistenceManagerProvider" );
- field1.setAccessible( true );
- final LegacyPersistenceManagerTracker lpmt = (LegacyPersistenceManagerTracker) field1.get(configMgr);
- Field field = lpmt.getClass().getDeclaredField( "persistenceManagers" );
- field.setAccessible( true );
-
- CachingPersistenceManagerProxy[] persistenceManagers = new CachingPersistenceManagerProxy [1];
PersistenceManager pm =new MockPersistenceManager();
- Dictionary dictionary = new Hashtable();
+ Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
dictionary.put( "property1", "value1" );
dictionary.put( Constants.SERVICE_PID, pid );
pm.store( pid, dictionary );
- persistenceManagers[0] = new CachingPersistenceManagerProxy(pm);
- field.set(lpmt, persistenceManagers);
+ ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
ConfigurationImpl[] conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
assertEquals(1, conf.length);
assertEquals(2, conf[0].getProperties(true).size());
- dictionary = new Hashtable();
+ dictionary = new Hashtable<String, Object>();
dictionary.put( "property1", "value2" );
pid = "testDefaultPersistenceManager";
dictionary.put( Constants.SERVICE_PID, pid );
@@ -121,35 +109,23 @@ public class ConfigurationManagerTest ex
assertEquals("value1", conf[0].getProperties(true).get("property1"));
}
- public void test_listConfigurations_notcached() throws Exception
+ @Test public void test_listConfigurations_notcached() throws Exception
{
String pid = "testDefaultPersistenceManager";
- ConfigurationManager configMgr = new ConfigurationManager();
- setServiceTrackerField( configMgr, "persistenceManagerTracker" );
-
- Field field1 = configMgr.getClass().getDeclaredField( "persistenceManagerProvider" );
- field1.setAccessible( true );
- final LegacyPersistenceManagerTracker lpmt = (LegacyPersistenceManagerTracker) field1.get(configMgr);
-
- Field field = lpmt.getClass().getDeclaredField( "persistenceManagers" );
- field.setAccessible( true );
-
- ExtPersistenceManager[] persistenceManagers = new ExtPersistenceManager[1];
PersistenceManager pm = new MockNotCachablePersistenceManager();
- Dictionary dictionary = new Hashtable();
+ Dictionary<String, Object> dictionary = new Hashtable<String, Object>();
dictionary.put( "property1", "value1" );
dictionary.put( Constants.SERVICE_PID, pid );
pm.store( pid, dictionary );
- persistenceManagers[0] = new PersistenceManagerProxy(pm);
- field.set(lpmt, persistenceManagers);
+ ConfigurationManager configMgr = new ConfigurationManager(new PersistenceManagerProxy(pm), null);
ConfigurationImpl[] conf = configMgr.listConfigurations(new ConfigurationAdminImpl(configMgr, null), null);
assertEquals(1, conf.length);
assertEquals(2, conf[0].getProperties(true).size());
- dictionary = new Hashtable();
+ dictionary = new Hashtable<String, Object>();
dictionary.put("property1", "valueNotCached");
pid = "testDefaultPersistenceManager";
dictionary.put( Constants.SERVICE_PID, pid );
@@ -163,7 +139,7 @@ public class ConfigurationManagerTest ex
assertEquals("valueNotCached", conf[0].getProperties(true).get("property1"));
}
- public void testLogNoLogService()
+ @Test public void testLogNoLogService() throws IOException
{
ConfigurationManager configMgr = createConfigurationManagerAndLog( null );
@@ -213,10 +189,9 @@ public class ConfigurationManagerTest ex
assertLog( configMgr, LogService.LOG_ERROR, "Error Test Message", null );
}
-
// this test always expects output since when using a LogService, the log
// level property is ignored
- public void testLogWithLogService()
+ @Test public void testLogWithLogService() throws IOException
{
LogService logService = new MockLogService();
ConfigurationManager configMgr = createConfigurationManagerAndLog( logService );
@@ -265,10 +240,10 @@ public class ConfigurationManagerTest ex
}
- public void testLogSetup()
+ @Test public void testLogSetup() throws IOException
{
final MockBundleContext bundleContext = new MockBundleContext();
- ConfigurationManager configMgr = createConfigurationManagerAndLog( null );
+ createConfigurationManagerAndLog( null );
// ensure the configuration data goes to target
bundleContext.setProperty( "felix.cm.dir", "target/config" );
@@ -297,7 +272,7 @@ public class ConfigurationManagerTest ex
}
- public void testEventsStartingBundle() throws Exception
+ @Test public void testEventsStartingBundle() throws Exception
{
final Set<String> result = new HashSet<String>();
@@ -330,7 +305,7 @@ public class ConfigurationManagerTest ex
ServiceRegistration mockReg = Mockito.mock( ServiceRegistration.class );
Mockito.when( mockReg.getReference() ).thenReturn( mockRef );
- ConfigurationManager configMgr = new ConfigurationManager();
+ ConfigurationManager configMgr = new ConfigurationManager(new PersistenceManagerProxy(new MockPersistenceManager()), null);
setServiceTrackerField( configMgr, "configurationListenerTracker" );
ServiceReference[] refs =
@@ -441,9 +416,43 @@ public class ConfigurationManagerTest ex
}
+ private static ServiceReference[] setServiceTrackerField( ConfigurationManager configMgr,
+ String fieldName, Object ... services ) throws Exception
+ {
+ final Map<ServiceReference, Object> refMap = new HashMap<ServiceReference, Object>();
+ for ( Object svc : services )
+ {
+ ServiceReference sref = Mockito.mock( ServiceReference.class );
+ Mockito.when( sref.getProperty( "objectClass" ) ).thenReturn(new String[] { "TestService" });
+ refMap.put( sref, svc );
+ }
+
+
+ Field field = configMgr.getClass().getDeclaredField( fieldName );
+ field.setAccessible( true );
+ field.set( configMgr, new ServiceTracker( new MockBundleContext(), "", null )
+ {
+ @Override
+ public ServiceReference[] getServiceReferences()
+ {
+ return refMap.keySet().toArray( new ServiceReference[0] );
+ }
+
+ @Override
+ public Object getService(ServiceReference reference)
+ {
+ return refMap.get( reference );
+ }
+ } );
+
+ return refMap.keySet().toArray(new ServiceReference[0]);
+ }
+
private static ConfigurationManager createConfigurationManagerAndLog( final LogService logService )
+ throws IOException
{
- ConfigurationManager configMgr = new ConfigurationManager();
+ final PersistenceManager pm = Mockito.mock(PersistenceManager.class);
+ ConfigurationManager configMgr = new ConfigurationManager(new CachingPersistenceManagerProxy(pm), null);
try
{
@@ -466,61 +475,4 @@ public class ConfigurationManagerTest ex
return configMgr;
}
-
- private static ServiceReference[] setServiceTrackerField( ConfigurationManager configMgr,
- String fieldName, Object ... services ) throws Exception
- {
- final Map<ServiceReference, Object> refMap = new HashMap<ServiceReference, Object>();
- for ( Object svc : services )
- {
- ServiceReference sref = Mockito.mock( ServiceReference.class );
- Mockito.when( sref.getProperty( "objectClass" ) ).thenReturn(new String[] { "TestService" });
- refMap.put( sref, svc );
- }
-
- if ( "persistenceManagerTracker".equals(fieldName) ) {
- final LegacyPersistenceManagerTracker lpmt = new LegacyPersistenceManagerTracker();
- Field field = lpmt.getClass().getDeclaredField( fieldName );
- field.setAccessible( true );
- field.set( lpmt, new ServiceTracker( new MockBundleContext(), "", null )
- {
- @Override
- public ServiceReference[] getServiceReferences()
- {
- return refMap.keySet().toArray( new ServiceReference[0] );
- }
-
- @Override
- public Object getService(ServiceReference reference)
- {
- return refMap.get( reference );
- }
- } );
-
- Field field2 = configMgr.getClass().getDeclaredField( "persistenceManagerProvider" );
- field2.setAccessible( true );
- field2.set( configMgr, lpmt );
- }
- else
- {
- Field field = configMgr.getClass().getDeclaredField( fieldName );
- field.setAccessible( true );
- field.set( configMgr, new ServiceTracker( new MockBundleContext(), "", null )
- {
- @Override
- public ServiceReference[] getServiceReferences()
- {
- return refMap.keySet().toArray( new ServiceReference[0] );
- }
-
- @Override
- public Object getService(ServiceReference reference)
- {
- return refMap.get( reference );
- }
- } );
-
- }
- return refMap.keySet().toArray(new ServiceReference[0]);
- }
}