import com.google.common.base.Optional;
import org.opendaylight.controller.config.persist.api.Persister;
import org.opendaylight.controller.config.persist.api.storage.StorageAdapter;
-import org.osgi.framework.BundleContext;
+import org.opendaylight.controller.config.persist.api.storage.StorageAdapter.PropertiesProvider;
+import org.opendaylight.controller.netconf.persist.impl.osgi.ConfigPersisterActivator;
import java.io.IOException;
*/
public final class PersisterImpl implements Persister {
- public static final String STORAGE_ADAPTER_CLASS_PROP = "netconf.config.persister.storageAdapterClass";
+
private final StorageAdapter storage;
public PersisterImpl(StorageAdapter storage) {
this.storage = storage;
}
- public static Optional<PersisterImpl> createFromProperties(BundleContext bundleContext) {
- String storageAdapterClass = bundleContext.getProperty(STORAGE_ADAPTER_CLASS_PROP);
+ public static PersisterImpl createFromProperties(PropertiesProvider propertiesProvider) {
+ String storageAdapterClass = propertiesProvider.getProperty(ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX);
StorageAdapter storage;
if (storageAdapterClass == null || storageAdapterClass.equals("")) {
- return Optional.absent();
+ throw new IllegalStateException("No persister is defined in " +
+ propertiesProvider.getFullKeyForReporting(ConfigPersisterActivator.STORAGE_ADAPTER_CLASS_PROP_SUFFIX)
+ + " property. For noop persister use " + NoOpStorageAdapter.class.getCanonicalName()
+ + " . Persister is not operational");
}
try {
- storage = StorageAdapter.class.cast(resolveClass(storageAdapterClass, StorageAdapter.class).newInstance());
- storage.setProperties(bundleContext);
+ Class<?> clazz = Class.forName(storageAdapterClass);
+ boolean implementsCorrectIfc = StorageAdapter.class.isAssignableFrom(clazz);
+ if (implementsCorrectIfc == false) {
+ throw new IllegalArgumentException("Storage adapter " + clazz + " does not implement " + StorageAdapter.class);
+ }
+ storage = StorageAdapter.class.cast(clazz.newInstance());
+
+ storage.setProperties(propertiesProvider);
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new IllegalArgumentException("Unable to instantiate storage adapter from " + storageAdapterClass, e);
}
- return Optional.of(new PersisterImpl(storage));
- }
-
- private static Class<?> resolveClass(String storageAdapterClass, Class<?> baseType) throws ClassNotFoundException {
- Class<?> clazz = Class.forName(storageAdapterClass);
- if (!isImplemented(baseType, clazz))
- throw new IllegalArgumentException("Storage adapter " + clazz + " has to implement " + baseType);
- return clazz;
- }
-
- private static boolean isImplemented(Class<?> expectedIface, Class<?> byClazz) {
- for (Class<?> iface : byClazz.getInterfaces()) {
- if (iface.equals(expectedIface))
- return true;
- }
- return false;
+ return new PersisterImpl(storage);
}
@Override