/** @see SubscriptionContext#subscribe(Entity, Sensor, SensorEventListener) */ protected <T> SubscriptionHandle subscribe(Entity producer, Sensor<T> sensor, SensorEventListener<? super T> listener) { if (!check(entity)) return null; return getSubscriptionTracker().subscribe(producer, sensor, listener); }
/** will set fields from flags, and put the remaining ones into the 'leftovers' map. * can be subclassed for custom initialization but note the following. * <p> * if you require fields to be initialized you must do that in this method. You must * *not* rely on field initializers because they may not run until *after* this method * (this method is invoked by the constructor in this class, so initializers * in subclasses will not have run when this overridden method is invoked.) */ protected void configure() { configure(Collections.emptyMap()); }
@SuppressWarnings("unchecked") @Override public <T> T setConfig(ConfigKey<T> key, T val) { if (entity != null && isRunning()) { doReconfigureConfig(key, val); } return (T) configsInternal.setConfig(key, val); }
/** * Unsubscribes the given producer. * * @see SubscriptionContext#unsubscribe(SubscriptionHandle) */ protected boolean unsubscribe(Entity producer) { if (destroyed.get()) return false; return getSubscriptionTracker().unsubscribe(producer); }
@Override public boolean isRunning() { return !isDestroyed(); }
@Override public String toString() { return Objects.toStringHelper(getClass()) .add("name", name) .add("running", isRunning()) .toString(); } }
@Override public void destroy(){ suspend(); super.destroy(); }
@SuppressWarnings("unchecked") @Override public <T> T getConfig(ConfigKey<T> key, T defaultValue) { // FIXME What about inherited task in config?! // alex says: think that should work, no? // FIXME What if someone calls getConfig on a task, before setting parent app? // alex says: not supported (throw exception, or return the task) // In case this entity class has overridden the given key (e.g. to set default), then retrieve this entity's key // TODO If ask for a config value that's not in our configKeys, should we really continue with rest of method and return key.getDefaultValue? // e.g. SshBasedJavaAppSetup calls setAttribute(JMX_USER), which calls getConfig(JMX_USER) // but that example doesn't have a default... ConfigKey<T> ownKey = adjunct!=null ? (ConfigKey<T>)elvis(adjunct.getAdjunctType().getConfigKey(key.getName()), key) : key; // Don't use groovy truth: if the set value is e.g. 0, then would ignore set value and return default! if (ownKey instanceof ConfigKeySelfExtracting) { if (((ConfigKeySelfExtracting<T>)ownKey).isSet(ownConfig)) { // FIXME Should we support config from futures? How to get execution context before setEntity? EntityLocal entity = adjunct.entity; ExecutionContext exec = (entity != null) ? ((EntityInternal)entity).getExecutionContext() : null; return ((ConfigKeySelfExtracting<T>)ownKey).extractValue(ownConfig, exec); } } else { LOG.warn("Config key {} of {} is not a ConfigKeySelfExtracting; cannot retrieve value; returning default", ownKey, this); } return TypeCoercions.coerce((defaultValue != null) ? defaultValue : ownKey.getDefaultValue(), key.getTypeToken()); }
/** * Unsubscribes the given producer. * * @see SubscriptionContext#unsubscribe(SubscriptionHandle) */ protected boolean unsubscribe(Entity producer, SubscriptionHandle handle) { if (destroyed.get()) return false; return getSubscriptionTracker().unsubscribe(producer, handle); }
/** @see SubscriptionContext#subscribe(Entity, Sensor, SensorEventListener) */ protected <T> SubscriptionHandle subscribeToChildren(Entity producerParent, Sensor<T> sensor, SensorEventListener<? super T> listener) { if (!check(producerParent)) return null; return getSubscriptionTracker().subscribeToChildren(producerParent, sensor, listener); }
/** * @return a list of all subscription handles */ protected Collection<SubscriptionHandle> getAllSubscriptions() { SubscriptionTracker tracker = getSubscriptionTracker(); return (tracker != null) ? tracker.getAllSubscriptions() : Collections.<SubscriptionHandle>emptyList(); }
public AbstractEntityAdjunct(Map flags) { inConstruction = true; _legacyConstruction = !InternalPolicyFactory.FactoryConstructionTracker.isConstructing(); if (!_legacyConstruction && flags!=null && !flags.isEmpty()) { log.debug("Using direct construction for "+getClass().getName()+" because properties were specified ("+flags+")"); _legacyConstruction = true; } if (_legacyConstruction) { log.debug("Using direct construction for "+getClass().getName()+"; calling configure(Map) immediately"); configure(flags); boolean deferConstructionChecks = (flags.containsKey("deferConstructionChecks") && TypeCoercions.coerce(flags.get("deferConstructionChecks"), Boolean.class)); if (!deferConstructionChecks) { FlagUtils.checkRequiredFields(this); } } inConstruction = false; }
/** @see SubscriptionContext#subscribe(Entity, Sensor, SensorEventListener) */ protected <T> SubscriptionHandle subscribeToMembers(Group producerGroup, Sensor<T> sensor, SensorEventListener<? super T> listener) { if (!check(producerGroup)) return null; return getSubscriptionTracker().subscribeToMembers(producerGroup, sensor, listener); }
/** * Unsubscribes and clears all managed subscriptions; is called by the owning entity when a policy is removed * and should always be called by any subclasses overriding this method */ public void destroy() { destroyed.set(true); SubscriptionTracker tracker = getSubscriptionTracker(); if (tracker != null) tracker.unsubscribeAll(); }