Hi,

A CompositeDataStore in practice will generally have at least two other
data stores “inside” it (if there are less than two, there’s little point
in using CompositeDataStore).  I refer to these as “delegate” data stores
or just “delegates”.

In my prototype, the addition and removal of delegates was OSGi-lifecycle
aware:
- When the CompositeDataStoreService was being activated, any delegate data
stores implemented in already-active bundles would be created then.
- As additional bundles were activated, if a bundle contained the
implementation for a delegate it would be created then.
- As bundles were deactivated, if a bundle contained the implementation for
a delegate it would be deleted then.

My question then has to do with how to create the delegate data store.

I assumed that creating an instance of the Service class wouldn’t work
because I can envision scenarios where I would want multiple delegate data
stores of the same type - for example, two FileDataStores - and I am of the
understanding that I wouldn’t be able to create more than one of any type
of service.

So instead I simply constructed the delegate data stores using reflection.
Something like this:

private DataStore createDelegateDataStore(String className, Properties
properties, Bundle bundle) {
if (Bundle.ACTIVE == bundle.getState()) {
Class dataStoreClass = bundle.loadClass(className);
DataStore dataStore = (DataStore) dataStoreClass.newInstance();
Method setPropertiesMethod = dataStoreClass.getMethod(“setProperties”,
Properties.class);
setPropertiesMethod.invoke(dataStore, properties);
}
}

(Of course handling of exceptions like NoSuchMethodException,
ClassNotFoundException, etc are not included here for brevity.)

Is a reasonable way to do this or should some other approach be used?  What
would be the best way to go about doing this?

-MR

Reply via email to