DefaultCapabilityRegistry extends ComponentSupport implements CapabilityRegistry, EventAware, EventAware.Asynchronous { public void load() { final Map<CapabilityIdentity, CapabilityStorageItem> items = capabilityStorage.getAll(); for (final Map.Entry<CapabilityIdentity, CapabilityStorageItem> entry : items.entrySet()) { CapabilityIdentity id = entry.getKey(); CapabilityStorageItem item = entry.getValue(); log.debug( "Loading capability '{}' of type '{}' with properties '{}'", id, item.getType(), item.getProperties() ); final CapabilityDescriptor descriptor = capabilityDescriptorRegistry.get(capabilityType(item.getType())); if (descriptor == null) { log.warn( "Capabilities persistent storage contains a capability of unknown type {} with" + " id {}. This capability will not be loaded", item.getType(), id ); continue; } Map<String, String> properties = decryptValuesIfNeeded(descriptor, item.getProperties()); if (descriptor.version() != item.getVersion()) { log.debug( "Converting capability '{}' properties from version '{}' to version '{}'", id, item.getVersion(), descriptor.version() ); try { properties = descriptor.convert(properties, item.getVersion()); if (properties == null) { properties = Collections.emptyMap(); } if (log.isDebugEnabled()) { log.debug( "Converted capability '{}' properties '{}' (version '{}') to '{}' (version '{}')", id, item.getProperties(), item.getVersion(), encryptValuesIfNeeded(descriptor, properties), descriptor.version() ); } } catch (Exception e) { log.error( "Failed converting capability '{}' properties '{}' from version '{}' to version '{}'." + " Capability will not be loaded", id, item.getProperties(), item.getVersion(), descriptor.version(), e ); continue; } capabilityStorage.update(id, capabilityStorage.newStorageItem( descriptor.version(), item.getType(), item.isEnabled(), item.getNotes(), properties) ); } DefaultCapabilityReference reference = references.get(id); if (reference != null) { doUpdate(reference, item, properties); continue; } reference = create(id, capabilityType(item.getType()), descriptor); reference.setNotes(item.getNotes()); reference.load(properties); try { reference.descriptor().validate(id, properties, ValidationMode.LOAD); } catch (ValidationException e) { log.warn("Capability '{}' of type '{}' with properties '{}' is invalid", id, item.getType(), item.getProperties(), e); reference.setFailure("Load", e); } if (item.isEnabled()) { reference.enable(); reference.activate(); } } eventManager.post(new AfterLoad(this)); } @Inject  DefaultCapabilityRegistry(final CapabilityStorage capabilityStorage,
                            final CapabilityFactoryRegistry capabilityFactoryRegistry,
                            final CapabilityDescriptorRegistry capabilityDescriptorRegistry,
                            final EventManager eventManager,
                            final ActivationConditionHandlerFactory activationConditionHandlerFactory,
                            final ValidityConditionHandlerFactory validityConditionHandlerFactory,
                            final PasswordHelper passwordHelper,
                            final Provider<Validator> validatorProvider); @Override CapabilityReference add(final CapabilityType type,
                                 final boolean enabled,
                                 @Nullable final String notes,
                                 @Nullable final Map<String, String> properties); @Subscribe void on(final CapabilityStorageItemCreatedEvent event); @Override CapabilityReference update(final CapabilityIdentity id,
                                    final boolean enabled,
                                    @Nullable final String notes,
                                    @Nullable final Map<String, String> properties); @Subscribe void on(final CapabilityStorageItemUpdatedEvent event); @Override CapabilityReference remove(final CapabilityIdentity id); @Subscribe void on(final CapabilityStorageItemDeletedEvent event); @Override CapabilityReference enable(final CapabilityIdentity id); @Override CapabilityReference disable(final CapabilityIdentity id); @Override DefaultCapabilityReference get(final CapabilityIdentity id); @Override Collection<DefaultCapabilityReference> get(final Predicate<CapabilityReference> filter); @Override Collection<DefaultCapabilityReference> getAll(); void load();  }
@Test public void load() throws Exception { final Map<String, String> oldProps = Maps.newHashMap(); oldProps.put("p1", "v1"); oldProps.put("p2", "v2"); final CapabilityStorageItem item = new OrientCapabilityStorageItem( 0, CAPABILITY_TYPE.toString(), true, null, oldProps ); CapabilityIdentity fooId = capabilityIdentity("foo"); when(capabilityStorage.getAll()).thenReturn(ImmutableMap.of(fooId, item)); final CapabilityDescriptor descriptor = mock(CapabilityDescriptor.class); when(capabilityDescriptorRegistry.get(CAPABILITY_TYPE)).thenReturn(descriptor); when(descriptor.version()).thenReturn(0); underTest.load(); verify(capabilityStorage).getAll(); verify(descriptor).version(); verify(descriptor).formFields(); verify(descriptor).validate(fooId, oldProps, ValidationMode.LOAD); verifyNoMoreInteractions(descriptor, capabilityStorage); }