@Override protected void finalize() throws Throwable { super.finalize(); if (!getManagementSupport().wasDeployed()) LOG.warn("Entity "+this+" was never deployed -- explicit call to manage(Entity) required."); } }
@Override public synchronized ExecutionContext getExecutionContext() { return getManagementSupport().getExecutionContext(); }
@Override public synchronized ManagementContext getManagementContext() { return getManagementSupport().getManagementContext(); }
@Override public synchronized SubscriptionContext getSubscriptionContext() { return getManagementSupport().getSubscriptionContext(); }
@SuppressWarnings("unchecked") private <T> T setConfigInternal(ConfigKey<T> key, Object val) { if (!inConstruction && getManagementSupport().isDeployed()) { // previously we threw, then warned, but it is still quite common; // so long as callers don't expect miracles, it should be fine. // i (Alex) think the way to be stricter about this (if that becomes needed) // would be to introduce a 'mutable' field on config keys LOG.debug("configuration being made to {} after deployment: {} = {}; change may not be visible in other contexts", new Object[] { this, key, val }); } return (T) configsInternal.setConfig(key, val); }
@Override public void clearLocations() { synchronized (locations) { locations.set(ImmutableList.<Location>of()); } getManagementSupport().getEntityChangeListener().onLocationsChanged(); }
@Override public boolean removeChild(Entity child) { synchronized (children) { boolean changed = children.remove(child); child.clearParent(); if (changed) { getManagementSupport().getEntityChangeListener().onChildrenChanged(); } return changed; } }
@Override public void addLocations(Collection<? extends Location> newLocations) { synchronized (locations) { List<Location> oldLocations = locations.get(); Set<Location> truelyNewLocations = Sets.newLinkedHashSet(newLocations); truelyNewLocations.removeAll(oldLocations); if (truelyNewLocations.size() > 0) { locations.set(ImmutableList.<Location>builder().addAll(oldLocations).addAll(truelyNewLocations).build()); } } if (getManagementSupport().isDeployed()) { for (Location newLocation : newLocations) { // Location is now reachable, so manage it // TODO will not be required in future releases when creating locations always goes through LocationManager.createLocation(LocationSpec). Entities.manage(newLocation, getManagementContext()); } } getManagementSupport().getEntityChangeListener().onLocationsChanged(); }
/** * Adds the given entity as a child of this parent <em>and</em> sets this entity as the parent of the child; * returns argument passed in, for convenience. */ @Override public <T extends Entity> T addChild(T child) { checkNotNull(child, "child must not be null (for entity %s)", this); synchronized (children) { if (Entities.isAncestor(this, child)) throw new IllegalStateException("loop detected trying to add child "+child+" to "+this+"; it is already an ancestor"); child.setParent(getProxyIfAvailable()); children.add(child); getManagementSupport().getEntityChangeListener().onChildrenChanged(); } return child; }
@Override public boolean removeAllPolicies() { boolean changed = false; for (Policy policy : policies) { removePolicy(policy); changed = true; } if (changed) { getManagementSupport().getEntityChangeListener().onPoliciesChanged(); } return changed; }
@Override public <T> T setAttributeWithoutPublishing(AttributeSensor<T> attribute, T val) { T result = attributesInternal.updateWithoutPublishing(attribute, val); if (result == null) { // could be this is a new sensor entityType.addSensorIfAbsentWithoutPublishing(attribute); } getManagementSupport().getEntityChangeListener().onAttributeChanged(attribute); return result; }
@Override public void addPolicy(Policy policy) { policies.add((AbstractPolicy)policy); ((AbstractPolicy)policy).setEntity(this); getManagementSupport().getEntityChangeListener().onPoliciesChanged(); emit(AbstractEntity.POLICY_ADDED, new PolicyDescriptor(policy)); }
@Override public boolean removePolicy(Policy policy) { ((AbstractPolicy)policy).destroy(); boolean changed = policies.remove(policy); if (changed) { getManagementSupport().getEntityChangeListener().onPoliciesChanged(); emit(AbstractEntity.POLICY_REMOVED, new PolicyDescriptor(policy)); } return changed; }
@Override public AbstractEntity configure(Map flags) { if (!inConstruction && getManagementSupport().isDeployed()) { LOG.warn("bulk/flag configuration being made to {} after deployment: may not be supported in future versions ({})", new Object[] { this, flags });
@Override public <T> T setAttribute(AttributeSensor<T> attribute, T val) { T result = attributesInternal.update(attribute, val); if (result == null) { // could be this is a new sensor entityType.addSensorIfAbsent(attribute); } getManagementSupport().getEntityChangeListener().onAttributeChanged(attribute); return result; }
/** * Returns the application, looking it up if not yet known (registering if necessary) */ @Override public Application getApplication() { if (application != null) return application; Entity parent = getParent(); Application app = (parent != null) ? parent.getApplication() : null; if (app != null) { if (getManagementSupport().isFullyManaged()) // only do this once fully managed, in case root app becomes parented setApplication(app); } return app; }
@Override public void removeLocations(Collection<? extends Location> removedLocations) { synchronized (locations) { List<Location> oldLocations = locations.get(); locations.set(MutableList.<Location>builder().addAll(oldLocations).removeAll(removedLocations).buildImmutable()); } // TODO Not calling `Entities.unmanage(removedLocation)` because this location might be shared with other entities. // Relying on abstractLocation.removeChildLocation unmanaging it, but not ideal as top-level locations will stick // around forever, even if not referenced. // Same goes for AbstractEntity#clearLocations(). getManagementSupport().getEntityChangeListener().onLocationsChanged(); }
public void setManagementContext(ManagementContextInternal managementContext) { getManagementSupport().setManagementContext(managementContext); entityType.setName(getEntityTypeName()); if (displayNameAutoGenerated) displayName.set(getEntityType().getSimpleName()+":"+id.substring(0, 4));