protected EntityManager getEntityManager() { return getManagementContext().getEntityManager(); }
@Override public Collection<Entity> getEntities() { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntities(); } else { return Collections.emptyList(); } }
@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."); } }
/** * 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 Iterable<Entity> getEntitiesInApplication(Application application) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntitiesInApplication(application); } else { return Collections.emptyList(); } }
@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."); } }
@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; }
/** * 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; } }
protected final <T extends Entity> T createEntity(EntitySpec<T> spec) { checkDuringManage(); EntityManager entityManager = managementContext.getEntityManager(); return entityManager.createEntity(spec); }
@SuppressWarnings("unchecked") protected synchronized Collection<T> find() { if (entities!=null) return entities.values(); if (referrer == null) throw new IllegalStateException("EntityReference should have been initialised with a reference parent"); Map<String,T> result = new LinkedHashMap<String,T>(); for (String it : entityRefs) { Entity e = ((EntityInternal)referrer).getManagementContext().getEntityManager().getEntity(it); if (e==null) { LOG.warn("unable to find {}, referred to by {}", it, referrer); } else { result.put(e.getId(), (T)e); } } entities = result; return entities.values(); } }
@Override public void run() { ((EntityInternal)application).destroy(); mgmt.getEntityManager().unmanage(application); } });
@Override public void run() { ((EntityInternal)application).destroy(); mgmt.getEntityManager().unmanage(application); } });
@Override public Entity getEntity(String id) { if (isInitialManagementContextReal()) { return initialManagementContext.getEntityManager().getEntity(id); } else { return null; } }
/** * Starts managing the given (unmanaged) app, setting the given brooklyn properties on the new * management context. * * @see #startManagement(Entity) */ public static ManagementContext startManagement(Application app, BrooklynProperties props) { if (isManaged(app)) { throw new IllegalStateException("Application "+app+" is already managed, so can't set brooklyn properties"); } ManagementContext mgmt = new LocalManagementContext(props); mgmt.getEntityManager().manage(app); return mgmt; }
/** looks for the given application instance, first by ID then by name * * @throws 404 if not found */ public Application getApplication(String application) { Entity e = mgmt.getEntityManager().getEntity(application); if (e!=null && e instanceof Application) return (Application)e; for (Application app: mgmt.getApplications()) { if (app.getId().equals(application)) return app; if (application.equalsIgnoreCase(app.getDisplayName())) return app; } throw notFound("Application '%s' not found", application); }
/** looks for the given application instance, first by ID then by name * * @throws 404 if not found */ public Application getApplication(String application) { Entity e = mgmt.getEntityManager().getEntity(application); if (e!=null && e instanceof Application) return (Application)e; for (Application app: mgmt.getApplications()) { if (app.getId().equals(application)) return app; if (application.equalsIgnoreCase(app.getDisplayName())) return app; } throw notFound("Application '%s' not found", application); }
/** * 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; } }
public static void unmanage(Entity entity) { if (((EntityInternal)entity).getManagementSupport().isDeployed()) { ((EntityInternal)entity).getManagementContext().getEntityManager().unmanage(entity); } }
@Override public void removeRegion(String id) { Entity entity = getManagementContext().getEntityManager().getEntity(id); Preconditions.checkNotNull(entity, "No entity found for %s", id); Preconditions.checkArgument(this.equals(entity.getParent()), "Wrong parent (%s) for %s", entity.getParent(), entity); Collection<Location> childLocations = entity.getLocations(); if (entity instanceof Startable) { try { Entities.invokeEffector(this, entity, Startable.STOP).get(); } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway"); log.debug("Error stopping "+entity+" ("+e+"); proceeding to remove it anyway", e); } } removeChild(entity); removeLocations(childLocations); }
public JsonNode fetch(String items) { Map<String,JsonNode> resultM = MutableMap.<String, JsonNode>of(); for (Application application : mgmt().getApplications()) resultM.put(application.getId(), fromEntity(application)); if (items!=null) { String[] itemsO = items.split(","); for (String item: itemsO) { Entity itemE = mgmt().getEntityManager().getEntity( item.trim() ); while (itemE != null && itemE.getParent()!=null) { resultM.put(itemE.getId(), fromEntity(itemE)); itemE= itemE.getParent(); } } } ArrayNode result = mapper.createArrayNode(); for (JsonNode n: resultM.values()) result.add(n); return result; }