@Override public EntityTypeRegistry getEntityTypeRegistry() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntityTypeRegistry(); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
/** * Gets the entity type name, to be returned by {@code getEntityType().getName()}. * To be called by brooklyn internals only. * Can be overridden to customize the name. */ protected String getEntityTypeName() { try { Class<?> typeClazz = getManagementContext().getEntityManager().getEntityTypeRegistry().getEntityTypeOf(getClass()); String typeName = typeClazz.getCanonicalName(); if (typeName == null) typeName = typeClazz.getName(); return typeName; } catch (IllegalArgumentException e) { String typeName = getClass().getCanonicalName(); if (typeName == null) typeName = getClass().getName(); LOG.debug("Entity type interface not found for entity "+this+"; instead using "+typeName+" as entity type name"); return typeName; } }
/** * Returns true if this is a "new-style" entity (i.e. where not expected to call the constructor to instantiate it). * That means it is an entity with a no-arg constructor, and where there is a mapped for an entity type interface. * @param managementContext * @param clazz */ @SuppressWarnings({ "unchecked", "rawtypes" }) public static boolean isNewStyleEntity(ManagementContext managementContext, Class<?> clazz) { try { return isNewStyleEntity(clazz) && managementContext.getEntityManager().getEntityTypeRegistry().getEntityTypeOf((Class)clazz) != null; } catch (IllegalArgumentException e) { return false; } }
private Entity newEntity(EntityMemento memento, Reflections reflections) { String entityId = memento.getId(); String entityType = checkNotNull(memento.getType(), "entityType of "+entityId); Class<?> entityClazz = reflections.loadClass(entityType); Map<String,Object> flags = Maps.newLinkedHashMap(); flags.put("id", entityId); if (AbstractApplication.class.isAssignableFrom(entityClazz)) flags.put("mgmt", managementContext); if (InternalEntityFactory.isNewStyleEntity(managementContext, entityClazz)) { Class<?> entityInterface = managementContext.getEntityManager().getEntityTypeRegistry().getEntityTypeOf((Class)entityClazz); EntitySpec<?> entitySpec = EntitySpec.create((Class)entityInterface).impl((Class)entityClazz).configure("id", entityId); return managementContext.getEntityManager().createEntity(entitySpec); } else { // There are several possibilities for the constructor; find one that works. // Prefer passing in the flags because required for Application to set the management context // TODO Feels very hacky! return (Entity) invokeConstructor(reflections, entityClazz, new Object[] {flags}, new Object[] {flags, null}, new Object[] {null}, new Object[0]); } }