@Override public <T extends Entity> T createEntity(EntitySpec<T> spec) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().createEntity(spec); } else { throw new IllegalStateException("Non-deployment context "+this+" (with no initial management context supplied) is not valid for this operation."); } }
@Deprecated @Override public Entity getEntity(String id) { return getEntityManager().getEntity(id); }
@Deprecated @Override public void unmanage(Entity e) { getEntityManager().unmanage(e); }
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]); } }
@Override public void manage(Entity e) { getEntityManager().manage(e); }
@Deprecated @Override public synchronized Collection<Entity> getEntities() { return getEntityManager().getEntities(); }
@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."); } }
@Deprecated @Override public boolean isManaged(Entity e) { return getEntityManager().isManaged(e); }
@Override public Iterable<Entity> getEntitiesInApplication(Application application) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntitiesInApplication(application); } else { return Collections.emptyList(); } }
@Deprecated @Override public void manage(Entity e) { getEntityManager().manage(e); }
@Override public Collection<Entity> getEntities() { return getEntityManager().getEntities(); }
/** * 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; } }
@Override public boolean isManaged(Entity entity) { return getEntityManager().isManaged(entity); }
@Override public Entity getEntity(String id) { return getEntityManager().getEntity(id); }
protected final <T extends Entity> T createEntity(EntitySpec<T> spec) { checkDuringManage(); EntityManager entityManager = managementContext.getEntityManager(); return entityManager.createEntity(spec); }
@Override public void unmanage(Entity e) { getEntityManager().unmanage(e); }
/** * Starts managing the given (unmanaged) app, using the given management context. * * @see #startManagement(Entity) */ public static ManagementContext startManagement(Application app, ManagementContext mgmt) { if (isManaged(app)) { throw new IllegalStateException("Application "+app+" is already managed, so can't set brooklyn properties"); } mgmt.getEntityManager().manage(app); return mgmt; }
@Override public Collection<Entity> getEntities() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntities(); } else { return Collections.emptyList(); } }
/** * 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; } }
@SuppressWarnings("unchecked") protected synchronized T find() { if (entity != null) return entity; if (referrer == null) throw new IllegalStateException("EntityReference "+id+" should have been initialised with a reference parent"); entity = (T) ((EntityInternal)referrer).getManagementContext().getEntityManager().getEntity(id); return entity; }