@Override public <T extends Entity> EntityTypeRegistry registerImplementation(Class<T> type, Class<? extends T> implClazz) { checkNotNull(type, "type"); checkNotNull(implClazz, "implClazz"); checkIsImplementation(type, implClazz); checkIsNewStyleImplementation(implClazz); synchronized (mutex) { Class<?> existingType = registry.inverse().get(implClazz); if (existingType != null && !type.equals(existingType)) { throw new IllegalArgumentException("Implementation "+implClazz+" already registered against type "+existingType+"; cannot also register against "+type); } LOG.debug("Implementation {} registered against type {}", implClazz, type); registry.put(type, implClazz); cache.forcePut(type, implClazz); } return this; }
@Test public void testGetEntityTypeOfLooksUpRegistry() { registry.registerImplementation(MyEntity.class, MyEntityImpl2.class); assertEquals(registry.getEntityTypeOf(MyEntityImpl2.class), MyEntity.class); }
@Override public <T extends Entity> Class<? extends T> getImplementedBy(Class<T> type) { synchronized (mutex) { Class<?> result = cache.get(type); if (result != null) { if (LOG.isTraceEnabled()) LOG.trace("Implementation {} returned for type {}", result, type); return (Class<? extends T>) result; } result = getFromAnnotation(type); if (result == null) { if (!type.isInterface() && ((type.getModifiers() & Modifier.ABSTRACT)==0)) { // warning delivered later, in InternalEntityFactory result = type; } else { throw new IllegalArgumentException("Interface "+type+" is not annotated with @"+ImplementedBy.class.getSimpleName()+", and no implementation is registered"); } } if (LOG.isTraceEnabled()) LOG.trace("Implementation {} returned for type {}", result, type); cache.put(type, result); return (Class<? extends T>) result; } }
@Test public void testRegisterAllowsOverridingKey() { registry.registerImplementation(MyEntity.class, MyEntityImpl.class); registry.registerImplementation(MyEntity.class, MyEntityImpl2.class); assertEquals(registry.getImplementedBy(MyEntity.class), MyEntityImpl2.class); }
@BeforeMethod(alwaysRun=true) public void setUp() { registry = new BasicEntityTypeRegistry(); }
@Test public void testGetEntityTypeOfLooksUpAnnotationOnIndirectlyImplementedClasses() { assertEquals(registry.getEntityTypeOf(MyIndirectEntityImpl.class), MyIndirectEntity.class); }
@Test public void testGetImplementionLooksUpAnnotations() { assertEquals(registry.getImplementedBy(MyEntity.class), MyEntityImpl.class); }
@Override public <T extends Entity> Class<? super T> getEntityTypeOf(Class<T> implClazz) { synchronized (mutex) { Class<?> result = cache.inverse().get(implClazz); if (result != null) { return (Class<? super T>) result; } result = getInterfaceWithAnnotationMatching(implClazz); cache.put(implClazz, result); return (Class<? super T>) result; } }
private <T extends Entity> Class<? extends T> getFromAnnotation(Class<T> type) { ImplementedBy annotation = type.getAnnotation(org.apache.brooklyn.api.entity.ImplementedBy.class); if (annotation == null) return null; Class<? extends Entity> value = annotation.value(); checkIsImplementation(type, value); return (Class<? extends T>) value; }
@Test public void testRegisterForbidsDuplicateValues() { registry.registerImplementation(MyEntity.class, MyEntityImpl2.class); try { registry.registerImplementation(MyEntity2.class, MyEntityImpl2.class); } catch (IllegalArgumentException e) { if (!e.toString().contains("already registered against type")) throw e; } }
@Test public void testGetImplementionUsesRegistryFirst() { registry.registerImplementation(MyEntity.class, MyEntityImpl2.class); assertEquals(registry.getImplementedBy(MyEntity.class), MyEntityImpl2.class); }
public LocalEntityManager(LocalManagementContext managementContext) { this.managementContext = checkNotNull(managementContext, "managementContext"); this.storage = managementContext.getStorage(); this.entityTypeRegistry = new BasicEntityTypeRegistry(); this.policyFactory = new InternalPolicyFactory(managementContext); this.entityFactory = new InternalEntityFactory(managementContext, entityTypeRegistry, policyFactory); entityTypes = storage.getMap("entities"); applicationIds = SetFromLiveMap.create(storage.<String,Boolean>getMap("applications")); }
@Test public void testGetEntityTypeOfLooksUpAnnotation() { assertEquals(registry.getEntityTypeOf(MyEntityImpl.class), MyEntity.class); }
@Test public void testGetImplementionThrowsIfNoRegistryOrAnnotation() { try { Class<?> result = registry.getImplementedBy(MyEntityWithoutAnnotation.class); fail("result="+result); } catch (IllegalArgumentException e) { if (!e.toString().contains("MyEntityWithoutAnnotation is not annotated")) throw e; } }
@Test public void testGetEntityTypeOfThrowsIfNoRegistryOrAnnotation() { try { Class<?> result = registry.getEntityTypeOf(MyEntityImpl2.class); fail("result="+result); } catch (IllegalArgumentException e) { if (!e.toString().matches(".*Interfaces of .* not annotated.*")) throw e; } }