http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java deleted file mode 100644 index e78a1af..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityAsserts.java +++ /dev/null @@ -1,226 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import com.google.common.annotations.Beta; -import com.google.common.base.Objects; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Maps; -import com.google.common.collect.Sets; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.Group; -import org.apache.brooklyn.api.mgmt.SubscriptionHandle; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.api.sensor.SensorEvent; -import org.apache.brooklyn.api.sensor.SensorEventListener; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.test.Asserts; -import org.apache.brooklyn.util.time.Duration; - -import java.util.Collection; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.Callable; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicReference; - -import static org.apache.brooklyn.test.Asserts.assertEquals; - -/** - * Convenience class containing assertions that may be made about entities. - */ -public class EntityAsserts { - - - public static <T> void assertAttributeEquals(Entity entity, AttributeSensor<T> attribute, T expected) { - assertEquals(entity.getAttribute(attribute), expected, "entity=" + entity + "; attribute=" + attribute); - } - - public static <T> void assertConfigEquals(Entity entity, ConfigKey<T> configKey, T expected) { - assertEquals(entity.getConfig(configKey), expected, "entity=" + entity + "; configKey=" + configKey); - } - - public static <T> void assertAttributeEqualsEventually(final Entity entity, final AttributeSensor<T> attribute, final T expected) { - assertAttributeEqualsEventually(Maps.newLinkedHashMap(), entity, attribute, expected); - } - - public static <T> void assertAttributeEqualsEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) { - // Not using assertAttributeEventually(predicate) so get nicer error message - Asserts.succeedsEventually((Map) flags, new Runnable() { - @Override - public void run() { - assertAttributeEquals(entity, attribute, expected); - } - }); - } - - public static <T> T assertAttributeEventuallyNonNull(final Entity entity, final AttributeSensor<T> attribute) { - return assertAttributeEventuallyNonNull(Maps.newLinkedHashMap(), entity, attribute); - } - - public static <T> T assertAttributeEventuallyNonNull(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute) { - return assertAttributeEventually(flags, entity, attribute, Predicates.notNull()); - } - - public static <T> T assertAttributeEventually(final Entity entity, final AttributeSensor<T> attribute, Predicate<? super T> predicate) { - return assertAttributeEventually(ImmutableMap.of(), entity, attribute, predicate); - } - - public static <T> T assertAttributeEventually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate) { - final AtomicReference<T> result = new AtomicReference<T>(); - Asserts.succeedsEventually((Map)flags, new Runnable() { - @Override public void run() { - T val = entity.getAttribute(attribute); - Asserts.assertTrue(predicate.apply(val), "val=" + val); - result.set(val); - }}); - return result.get(); - } - - public static <T> T assertAttribute(final Entity entity, final AttributeSensor<T> attribute, final Predicate<? super T> predicate) { - T val = entity.getAttribute(attribute); - Asserts.assertTrue(predicate.apply(val), "val=" + val); - return val; - } - - - public static <T extends Entity> void assertPredicateEventuallyTrue(final T entity, final Predicate<? super T> predicate) { - assertPredicateEventuallyTrue(Maps.newLinkedHashMap(), entity, predicate); - } - - public static <T extends Entity> void assertPredicateEventuallyTrue(Map<?,?> flags, final T entity, final Predicate<? super T> predicate) { - Asserts.succeedsEventually((Map)flags, new Runnable() { - @Override public void run() { - Asserts.assertTrue(predicate.apply(entity), "predicate unsatisfied"); - }}); - } - - public static <T> void assertAttributeEqualsContinually(final Entity entity, final AttributeSensor<T> attribute, final T expected) { - assertAttributeEqualsContinually(Maps.newLinkedHashMap(), entity, attribute, expected); - } - - public static <T> void assertAttributeEqualsContinually(Map<?,?> flags, final Entity entity, final AttributeSensor<T> attribute, final T expected) { - Asserts.succeedsContinually(flags, new Runnable() { - @Override public void run() { - assertAttributeEquals(entity, attribute, expected); - }}); - } - - public static void assertGroupSizeEqualsEventually(final Group group, int expected) { - assertGroupSizeEqualsEventually(ImmutableMap.of(), group, expected); - } - - public static void assertGroupSizeEqualsEventually(Map<?,?> flags, final Group group, final int expected) { - Asserts.succeedsEventually((Map)flags, new Runnable() { - @Override public void run() { - Collection<Entity> members = group.getMembers(); - assertEquals(members.size(), expected, "members=" + members); - }}); - } - - - /** - * Asserts that the entity's value for this attribute changes, by registering a subscription and checking the value. - * - * @param entity The entity whose attribute will be checked. - * @param attribute The attribute to check on the entity. - * - * @throws AssertionError if the assertion fails. - */ - public static void assertAttributeChangesEventually(final Entity entity, final AttributeSensor<?> attribute) { - final Object origValue = entity.getAttribute(attribute); - final AtomicBoolean changed = new AtomicBoolean(); - SubscriptionHandle handle = entity.subscriptions().subscribe(entity, attribute, new SensorEventListener<Object>() { - @Override public void onEvent(SensorEvent<Object> event) { - if (!Objects.equal(origValue, event.getValue())) { - changed.set(true); - } - }}); - try { - Asserts.succeedsEventually(new Runnable() { - @Override public void run() { - Asserts.assertTrue(changed.get(), entity + " -> " + attribute + " not changed"); - }}); - } finally { - entity.subscriptions().unsubscribe(entity, handle); - } - } - - - /** - * Assert that the given attribute of an entity does not take any of the disallowed values during a given period. - * - * This method relies on {@link Asserts#succeedsContinually(Runnable)}, therefore it loops comparing the value - * of the attribute to the disallowed values, rather than setting up a subscription. It may therefore miss a - * situation where the attribute temporarily takes a disallowed value. This method is therefore suited for use - * where the attribute will take on a value permanently, which may or may not be disallowed. - * - * @param entity The entity owning the attribute to check. - * @param attribute The attribute on the entity. - * @param disallowed The disallowed values for the entity. - * @param <T> Type of the sensor. - */ - @Beta @SafeVarargs - public static <T> void assertAttributeContinuallyNotEqualTo(final Entity entity, final AttributeSensor<T> attribute, T... disallowed) { - final Set<T> reject = Sets.newHashSet(disallowed); - Asserts.succeedsContinually(new Runnable() { - @Override - public void run() { - T val = entity.getAttribute(attribute); - Asserts.assertFalse(reject.contains(val), - "Attribute " + attribute + " on " + entity + " has disallowed value " + val); - } - }); - } - - /** - * Assert that the given attribute of an entity does not take any of the disallowed values during a given period. - * - * This method relies on {@link Asserts#succeedsContinually(Runnable)}, therefore it loops comparing the value - * of the attribute to the disallowed values, rather than setting up a subscription. It may therefore miss a - * situation where the attribute temporarily takes a disallowed value. This method is therefore suited for use - * where the attribute will take on a value permanently, which may or may not be disallowed. - * - * @param flags Flags controlling the loop, with keys: <ul> - * <li>timeout: a {@link Duration} specification String for the duration for which to test the - * assertion. Default 1 second.</li> - * <li>period: a {@link Duration} specification String for the interval at which to perform polls - * on the attribute value. Default 10ms.</li> - * </ul> - * @param entity The entity owning the attribute to check. - * @param attribute The attribute on the entity. - * @param disallowed The disallowed values for the entity. - * @param <T> Type of the sensor. - */ - @Beta @SafeVarargs - public static <T> void assertAttributeContinuallyNotEqualTo(final Map<?, ?> flags, final Entity entity, final AttributeSensor<T> attribute, T... disallowed) { - final Set<T> reject = Sets.newHashSet(disallowed); - Asserts.succeedsContinually(flags, new Runnable() { - @Override - public void run() { - T val = entity.getAttribute(attribute); - Asserts.assertFalse(reject.contains(val), - "Attribute " + attribute + " on " + entity + " has disallowed value " + val); - } - }); - } - -} \ No newline at end of file
http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityDynamicType.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityDynamicType.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityDynamicType.java deleted file mode 100644 index 69e8caa..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityDynamicType.java +++ /dev/null @@ -1,376 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.apache.brooklyn.api.effector.Effector; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntityType; -import org.apache.brooklyn.api.sensor.Sensor; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.config.ConfigKey.HasConfigKey; -import org.apache.brooklyn.core.effector.EffectorAndBody; -import org.apache.brooklyn.core.effector.EffectorBody; -import org.apache.brooklyn.core.effector.EffectorWithBody; -import org.apache.brooklyn.core.effector.Effectors; -import org.apache.brooklyn.core.effector.MethodEffector; -import org.apache.brooklyn.core.effector.EffectorTasks.EffectorBodyTaskFactory; -import org.apache.brooklyn.core.effector.EffectorTasks.EffectorTaskFactory; -import org.apache.brooklyn.core.objs.BrooklynDynamicType; -import org.apache.brooklyn.util.javalang.Reflections; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import com.google.common.annotations.Beta; -import com.google.common.base.Joiner; -import com.google.common.base.Throwables; -import com.google.common.collect.Maps; - -/** This is the actual type of an entity instance at runtime, - * which can change from the static {@link EntityType}, and can change over time; - * for this reason it does *not* implement EntityType, but - * callers can call {@link #getSnapshot()} to get a snapshot such instance - */ -public class EntityDynamicType extends BrooklynDynamicType<Entity, AbstractEntity> { - - private static final Logger LOG = LoggerFactory.getLogger(EntityDynamicType.class); - - /** - * Effectors on this entity, by name. - */ - // TODO support overloading; requires not using a map keyed off method name. - private final Map<String, Effector<?>> effectors = new ConcurrentHashMap<String, Effector<?>>(); - - /** - * Map of sensors on this entity, by name. - */ - private final ConcurrentMap<String,Sensor<?>> sensors = new ConcurrentHashMap<String, Sensor<?>>(); - - public EntityDynamicType(AbstractEntity entity) { - this(entity.getClass(), entity); - } - public EntityDynamicType(Class<? extends Entity> clazz) { - this(clazz, null); - } - private EntityDynamicType(Class<? extends Entity> clazz, AbstractEntity entity) { - super(clazz, entity); - String id = entity==null ? clazz.getName() : entity.getId(); - - effectors.putAll(findEffectors(clazz, null)); - if (LOG.isTraceEnabled()) - LOG.trace("Entity {} effectors: {}", id, Joiner.on(", ").join(effectors.keySet())); - - sensors.putAll(findSensors(clazz, null)); - if (LOG.isTraceEnabled()) - LOG.trace("Entity {} sensors: {}", id, Joiner.on(", ").join(sensors.keySet())); - - refreshSnapshot(); - } - - /** - * @deprecated since 0.7; unused code; instead use {@link #getBrooklynClass()} - */ - @Deprecated - public Class<? extends Entity> getEntityClass() { - return super.getBrooklynClass(); - } - - @Override - public EntityType getSnapshot() { - return (EntityType) super.getSnapshot(); - } - - // -------------------------------------------------- - - /** - * @return the effector with the given name, or null if not found - */ - public Effector<?> getEffector(String name) { - return effectors.get(name); - } - - /** - * Effectors available on this entity. - */ - public Map<String,Effector<?>> getEffectors() { - return Collections.unmodifiableMap(effectors); - } - - /** - * Adds the given {@link Effector} to this entity. - */ - @Beta - public void addEffector(Effector<?> newEffector) { - Effector<?> oldEffector = effectors.put(newEffector.getName(), newEffector); - invalidateSnapshot(); - if (oldEffector!=null) - instance.sensors().emit(AbstractEntity.EFFECTOR_CHANGED, newEffector.getName()); - else - instance.sensors().emit(AbstractEntity.EFFECTOR_ADDED, newEffector.getName()); - } - - /** - * Adds an effector with an explicit body to this entity. - */ - @Beta - public <T> void addEffector(Effector<T> effector, EffectorTaskFactory<T> body) { - addEffector(new EffectorAndBody<T>(effector, body)); - } - - /** - * Adds an effector with an explicit body to this entity. - */ - @Beta - public <T> void addEffector(Effector<T> effector, EffectorBody<T> body) { - addEffector(effector, new EffectorBodyTaskFactory<T>(body)); - } - - /** - * Removes the given {@link Effector} from this entity. - * <p> - * Note that if the argument is an instance of {@link EffectorWithBody} it will - * still be possible to invoke the effector on the entity by calling - * <code>entity.invoke(effector, argumentsMap)</code>. - */ - @Beta - public void removeEffector(Effector<?> effector) { - Effector<?> removed = effectors.remove(effector.getName()); - invalidateSnapshot(); - if (removed != null) { - instance.sensors().emit(AbstractEntity.EFFECTOR_REMOVED, removed.getName()); - } - } - - // -------------------------------------------------- - - /** - * Sensors available on this entity. - */ - public Map<String,Sensor<?>> getSensors() { - return Collections.unmodifiableMap(sensors); - } - - /** - * Convenience for finding named sensor. - */ - public Sensor<?> getSensor(String sensorName) { - return sensors.get(sensorName); - } - - /** - * Adds the given {@link Sensor} to this entity. - */ - public void addSensor(Sensor<?> newSensor) { - sensors.put(newSensor.getName(), newSensor); - invalidateSnapshot(); - instance.sensors().emit(AbstractEntity.SENSOR_ADDED, newSensor); - } - - /** - * Adds the given {@link Sensor}s to this entity. - */ - public void addSensors(Iterable<? extends Sensor<?>> newSensors) { - for (Sensor<?> sensor : newSensors) { - addSensor(sensor); - } - } - - public void addSensorIfAbsent(Sensor<?> newSensor) { - Sensor<?> prev = addSensorIfAbsentWithoutPublishing(newSensor); - if (prev == null) { - instance.sensors().emit(AbstractEntity.SENSOR_ADDED, newSensor); - } - } - - public Sensor<?> addSensorIfAbsentWithoutPublishing(Sensor<?> newSensor) { - Sensor<?> prev = sensors.putIfAbsent(newSensor.getName(), newSensor); - if (prev == null) { - invalidateSnapshot(); - } - return prev; - } - - /** - * Removes the named {@link Sensor} from this entity. - */ - public Sensor<?> removeSensor(String sensorName) { - Sensor<?> result = sensors.remove(sensorName); - if (result != null) { - invalidateSnapshot(); - instance.sensors().emit(AbstractEntity.SENSOR_REMOVED, result); - } - return result; - } - - /** - * Removes the named {@link Sensor} from this entity. - */ - public boolean removeSensor(Sensor<?> sensor) { - return (removeSensor(sensor.getName()) != null); - } - - // -------------------------------------------------- - - /** - * Adds the given {@link ConfigKey} to this entity. - */ - public void addConfigKey(ConfigKey<?> newKey) { - configKeys.put(newKey.getName(), new FieldAndValue<ConfigKey<?>>(null, newKey)); - invalidateSnapshot(); - instance.sensors().emit(AbstractEntity.CONFIG_KEY_ADDED, newKey); - } - - /** - * Adds the given {@link ConfigKey} to this entity. - */ - public void addConfigKeys(Iterable<ConfigKey<?>> newKeys) { - for (ConfigKey<?> newKey : newKeys) { - addConfigKey(newKey); - } - } - - /** - * Removes the named {@link ConfigKey} from this entity. - */ - public boolean removeConfigKey(ConfigKey<?> key) { - FieldAndValue<ConfigKey<?>> result = configKeys.remove(key.getName()); - if (result != null) { - invalidateSnapshot(); - ConfigKey<?> removedKey = result.value; - instance.sensors().emit(AbstractEntity.CONFIG_KEY_REMOVED, removedKey); - return true; - } else { - return false; - } - } - - // -------------------------------------------------- - - @Override - protected EntityTypeSnapshot newSnapshot() { - return new EntityTypeSnapshot(name, value(configKeys), sensors, effectors.values()); - } - - /** - * Finds the effectors defined on the entity's class, statics and optionally any non-static (discouraged). - */ - public static Map<String,Effector<?>> findEffectors(Class<? extends Entity> clazz, Entity optionalEntity) { - try { - Map<String,Effector<?>> result = Maps.newLinkedHashMap(); - Map<String,Field> fieldSources = Maps.newLinkedHashMap(); - Map<String,Method> methodSources = Maps.newLinkedHashMap(); - - for (Field f : Reflections.findPublicFieldsOrderedBySuper(clazz)) { - if (Effector.class.isAssignableFrom(f.getType())) { - if (!Modifier.isStatic(f.getModifiers())) { - // require it to be static or we have an instance - LOG.warn("Discouraged/deprecated use of non-static effector field "+f+" defined in " + (optionalEntity!=null ? optionalEntity : clazz)); - if (optionalEntity==null) continue; - } - Effector<?> eff = (Effector<?>) f.get(optionalEntity); - if (eff==null) { - LOG.warn("Effector "+f+" undefined for "+clazz+" ("+optionalEntity+")"); - continue; - } - Effector<?> overwritten = result.put(eff.getName(), eff); - Field overwrittenFieldSource = fieldSources.put(eff.getName(), f); - if (overwritten!=null && !Effectors.sameInstance(overwritten, eff)) { - LOG.trace("multiple definitions for effector {} on {}; preferring {} from {} to {} from {}", new Object[] { - eff.getName(), (optionalEntity != null ? optionalEntity : clazz), eff, f, overwritten, - overwrittenFieldSource}); - } - } - } - - for (Method m : Reflections.findPublicMethodsOrderedBySuper(clazz)) { - org.apache.brooklyn.core.annotation.Effector effectorAnnotation = m.getAnnotation(org.apache.brooklyn.core.annotation.Effector.class); - if (effectorAnnotation != null) { - if (Modifier.isStatic(m.getModifiers())) { - // require it to be static or we have an instance - LOG.warn("Discouraged/deprecated use of static annotated effector method "+m+" defined in " + (optionalEntity!=null ? optionalEntity : clazz)); - if (optionalEntity==null) continue; - } - - Effector<?> eff = MethodEffector.create(m); - Effector<?> overwritten = result.get(eff.getName()); - - if ((overwritten instanceof EffectorWithBody) && !(overwritten instanceof MethodEffector<?>)) { - // don't let annotations on methods override a static, unless that static is a MethodEffector - // TODO not perfect, but approx right; we should clarify whether we prefer statics or methods - } else { - result.put(eff.getName(), eff); - Method overwrittenMethodSource = methodSources.put(eff.getName(), m); - Field overwrittenFieldSource = fieldSources.remove(eff.getName()); - LOG.trace("multiple definitions for effector {} on {}; preferring {} from {} to {} from {}", new Object[] { - eff.getName(), (optionalEntity != null ? optionalEntity : clazz), eff, m, overwritten, - (overwrittenMethodSource != null ? overwrittenMethodSource : overwrittenFieldSource)}); - } - } - } - - return result; - } catch (IllegalAccessException e) { - throw Throwables.propagate(e); - } - } - - - /** - * Finds the sensors defined on the entity's class, statics and optionally any non-static (discouraged). - */ - public static Map<String,Sensor<?>> findSensors(Class<? extends Entity> clazz, Entity optionalEntity) { - try { - Map<String,Sensor<?>> result = Maps.newLinkedHashMap(); - Map<String,Field> sources = Maps.newLinkedHashMap(); - for (Field f : Reflections.findPublicFieldsOrderedBySuper((clazz))) { - if (Sensor.class.isAssignableFrom(f.getType())) { - if (!Modifier.isStatic(f.getModifiers())) { - // require it to be static or we have an instance - LOG.warn("Discouraged use of non-static sensor "+f+" defined in " + (optionalEntity!=null ? optionalEntity : clazz)); - if (optionalEntity==null) continue; - } - Sensor<?> sens = (Sensor<?>) f.get(optionalEntity); - Sensor<?> overwritten = result.put(sens.getName(), sens); - Field source = sources.put(sens.getName(), f); - if (overwritten!=null && overwritten != sens) { - if (sens instanceof HasConfigKey) { - // probably overriding defaults, just log low level (there will be add'l logging in config key section) - LOG.trace("multiple definitions for config sensor {} on {}; preferring {} from {} to {} from {}", new Object[] { - sens.getName(), optionalEntity!=null ? optionalEntity : clazz, sens, f, overwritten, source}); - } else { - LOG.warn("multiple definitions for sensor {} on {}; preferring {} from {} to {} from {}", new Object[] { - sens.getName(), optionalEntity!=null ? optionalEntity : clazz, sens, f, overwritten, source}); - } - } - } - } - - return result; - } catch (IllegalAccessException e) { - throw Throwables.propagate(e); - } - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java deleted file mode 100644 index c65a176..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityFunctions.java +++ /dev/null @@ -1,307 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import static com.google.common.base.Preconditions.checkNotNull; - -import java.util.Collection; -import java.util.Map; - -import org.apache.brooklyn.api.entity.Application; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.api.objs.Identifiable; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; -import org.apache.brooklyn.util.core.flags.TypeCoercions; -import org.apache.brooklyn.util.guava.Functionals; - -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.common.base.Suppliers; -import com.google.common.collect.Iterables; - -public class EntityFunctions { - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Function<Entity, T> attributeOld(final AttributeSensor<T> attribute) { - // TODO PERSISTENCE WORKAROUND - class GetEntityAttributeFunction implements Function<Entity, T> { - @Override public T apply(Entity input) { - return (input == null) ? null : input.getAttribute(attribute); - } - } - return new GetEntityAttributeFunction(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Function<Entity, T> configOld(final ConfigKey<T> key) { - // TODO PERSISTENCE WORKAROUND - class GetEntityConfigFunction implements Function<Entity, T> { - @Override public T apply(Entity input) { - return (input == null) ? null : input.getConfig(key); - } - } - return new GetEntityConfigFunction(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static Function<Entity, String> displayNameOld() { - // TODO PERSISTENCE WORKAROUND - class GetEntityDisplayName implements Function<Entity, String> { - @Override public String apply(Entity input) { - return (input == null) ? null : input.getDisplayName(); - } - } - return new GetEntityDisplayName(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static Function<Identifiable, String> idOld() { - // TODO PERSISTENCE WORKAROUND - class GetIdFunction implements Function<Identifiable, String> { - @Override public String apply(Identifiable input) { - return (input == null) ? null : input.getId(); - } - } - return new GetIdFunction(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static Function<Entity,Void> settingSensorsConstantOld(final Map<AttributeSensor<?>,Object> values) { - // TODO PERSISTENCE WORKAROUND - class SettingSensorsConstantFunction implements Function<Entity, Void> { - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override public Void apply(Entity input) { - for (Map.Entry<AttributeSensor<?>,Object> entry : values.entrySet()) { - AttributeSensor sensor = (AttributeSensor)entry.getKey(); - Object value = entry.getValue(); - if (value==Entities.UNCHANGED) { - // nothing - } else if (value==Entities.REMOVE) { - ((EntityInternal)input).removeAttribute(sensor); - } else { - value = TypeCoercions.coerce(value, sensor.getTypeToken()); - ((EntityInternal)input).sensors().set(sensor, value); - } - } - return null; - } - } - return new SettingSensorsConstantFunction(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <K,V> Function<Entity, Void> updatingSensorMapEntryOld(final AttributeSensor<Map<K,V>> mapSensor, final K key, final Supplier<? extends V> valueSupplier) { - // TODO PERSISTENCE WORKAROUND - class UpdatingSensorMapEntryFunction implements Function<Entity, Void> { - @Override public Void apply(Entity input) { - ServiceStateLogic.updateMapSensorEntry((EntityLocal)input, mapSensor, key, valueSupplier.get()); - return null; - } - } - return new UpdatingSensorMapEntryFunction(); - } - - /** @deprecated since 0.9.0 kept only to allow conversion of non-static inner classes */ - @SuppressWarnings("unused") @Deprecated - private static Supplier<Collection<Application>> applicationsOld(final ManagementContext mgmt) { - // TODO PERSISTENCE WORKAROUND - class AppsSupplier implements Supplier<Collection<Application>> { - @Override - public Collection<Application> get() { - return mgmt.getApplications(); - } - } - return new AppsSupplier(); - } - - public static <T> Function<Entity, T> attribute(AttributeSensor<T> attribute) { - return new GetEntityAttributeFunction<T>(checkNotNull(attribute, "attribute")); - } - - protected static class GetEntityAttributeFunction<T> implements Function<Entity, T> { - private final AttributeSensor<T> attribute; - protected GetEntityAttributeFunction(AttributeSensor<T> attribute) { - this.attribute = attribute; - } - @Override public T apply(Entity input) { - return (input == null) ? null : input.getAttribute(attribute); - } - } - - public static <T> Function<Object, T> attribute(Entity entity, AttributeSensor<T> attribute) { - return new GetFixedEntityAttributeFunction<>(entity, attribute); - } - - protected static class GetFixedEntityAttributeFunction<T> implements Function<Object, T> { - private final Entity entity; - private final AttributeSensor<T> attribute; - protected GetFixedEntityAttributeFunction(Entity entity, AttributeSensor<T> attribute) { - this.entity = entity; - this.attribute = attribute; - } - @Override public T apply(Object input) { - return entity.getAttribute(attribute); - } - } - - public static <T> Function<Entity, T> config(ConfigKey<T> key) { - return new GetEntityConfigFunction<T>(checkNotNull(key, "key")); - } - - protected static class GetEntityConfigFunction<T> implements Function<Entity, T> { - private final ConfigKey<T> key; - - protected GetEntityConfigFunction(ConfigKey<T> key) { - this.key = key; - } - - @Override public T apply(Entity input) { - return (input == null) ? null : input.getConfig(key); - } - } - - public static Function<Entity, String> displayName() { - return GetEntityDisplayName.INSTANCE; - } - - protected static class GetEntityDisplayName implements Function<Entity, String> { - public static final GetEntityDisplayName INSTANCE = new GetEntityDisplayName(); - @Override public String apply(Entity input) { - return (input == null) ? null : input.getDisplayName(); - } - } - - public static Function<Identifiable, String> id() { - return GetIdFunction.INSTANCE; - } - - protected static class GetIdFunction implements Function<Identifiable, String> { - public static final GetIdFunction INSTANCE = new GetIdFunction(); - @Override public String apply(Identifiable input) { - return (input == null) ? null : input.getId(); - } - } - - - /** returns a function which sets the given sensors on the entity passed in, - * with {@link Entities#UNCHANGED} and {@link Entities#REMOVE} doing those actions. */ - public static Function<Entity,Void> settingSensorsConstant(final Map<AttributeSensor<?>,Object> values) { - return new SettingSensorsConstantFunction(checkNotNull(values, "values")); - } - - protected static class SettingSensorsConstantFunction implements Function<Entity, Void> { - private final Map<AttributeSensor<?>, Object> values; - - protected SettingSensorsConstantFunction(Map<AttributeSensor<?>, Object> values) { - this.values = values; - } - @SuppressWarnings({ "unchecked", "rawtypes" }) - @Override public Void apply(Entity input) { - for (Map.Entry<AttributeSensor<?>,Object> entry : values.entrySet()) { - AttributeSensor sensor = (AttributeSensor)entry.getKey(); - Object value = entry.getValue(); - if (value==Entities.UNCHANGED) { - // nothing - } else if (value==Entities.REMOVE) { - ((EntityInternal)input).sensors().remove(sensor); - } else { - value = TypeCoercions.coerce(value, sensor.getTypeToken()); - ((EntityInternal)input).sensors().set(sensor, value); - } - } - return null; - } - } - - /** as {@link #settingSensorsConstant(Map)} but as a {@link Runnable} */ - public static Runnable settingSensorsConstant(final Entity entity, final Map<AttributeSensor<?>,Object> values) { - checkNotNull(entity, "entity"); - checkNotNull(values, "values"); - return Functionals.runnable(Suppliers.compose(settingSensorsConstant(values), Suppliers.ofInstance(entity))); - } - - public static <K,V> Function<Entity, Void> updatingSensorMapEntry(final AttributeSensor<Map<K,V>> mapSensor, final K key, final Supplier<? extends V> valueSupplier) { - return new UpdatingSensorMapEntryFunction<K,V>(mapSensor, key, valueSupplier); - } - - protected static class UpdatingSensorMapEntryFunction<K, V> implements Function<Entity, Void> { - private final AttributeSensor<Map<K, V>> mapSensor; - private final K key; - private final Supplier<? extends V> valueSupplier; - - public UpdatingSensorMapEntryFunction(AttributeSensor<Map<K, V>> mapSensor, K key, Supplier<? extends V> valueSupplier) { - this.mapSensor = mapSensor; - this.key = key; - this.valueSupplier = valueSupplier; - } - @Override public Void apply(Entity input) { - ServiceStateLogic.updateMapSensorEntry((EntityLocal)input, mapSensor, key, valueSupplier.get()); - return null; - } - } - - public static <K,V> Runnable updatingSensorMapEntry(final Entity entity, final AttributeSensor<Map<K,V>> mapSensor, final K key, final Supplier<? extends V> valueSupplier) { - return Functionals.runnable(Suppliers.compose(updatingSensorMapEntry(mapSensor, key, valueSupplier), Suppliers.ofInstance(entity))); - } - - public static Supplier<Collection<Application>> applications(ManagementContext mgmt) { - return new AppsSupplier(checkNotNull(mgmt, "mgmt")); - } - - protected static class AppsSupplier implements Supplier<Collection<Application>> { - private final ManagementContext mgmt; - - public AppsSupplier(ManagementContext mgmt) { - this.mgmt = mgmt; - } - @Override - public Collection<Application> get() { - return mgmt.getApplications(); - } - } - - public static Function<Entity, Location> locationMatching(Predicate<? super Location> filter) { - return new LocationMatching(filter); - } - - private static class LocationMatching implements Function<Entity, Location> { - private Predicate<? super Location> filter; - - private LocationMatching() { /* for xstream */ - } - public LocationMatching(Predicate<? super Location> filter) { - this.filter = filter; - } - @Override public Location apply(Entity input) { - return Iterables.find(input.getLocations(), filter); - } - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java deleted file mode 100644 index a258007..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInitializers.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import java.util.List; - -import org.apache.brooklyn.api.entity.EntityInitializer; -import org.apache.brooklyn.api.entity.EntityLocal; - -import com.google.common.collect.ImmutableList; - -public class EntityInitializers { - - public static class AddTags implements EntityInitializer { - public final List<Object> tags; - - public AddTags(Object... tags) { - this.tags = ImmutableList.copyOf(tags); - } - - @Override - public void apply(EntityLocal entity) { - for (Object tag: tags) - entity.tags().addTag(tag); - } - } - - - public static EntityInitializer addingTags(Object... tags) { - return new AddTags(tags); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java deleted file mode 100644 index 3602bee..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityInternal.java +++ /dev/null @@ -1,274 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import java.util.Collection; -import java.util.Map; - -import javax.annotation.Nullable; - -import org.apache.brooklyn.api.effector.Effector; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.EntityLocal; -import org.apache.brooklyn.api.entity.Group; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.api.mgmt.ExecutionContext; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.api.mgmt.rebind.RebindSupport; -import org.apache.brooklyn.api.mgmt.rebind.Rebindable; -import org.apache.brooklyn.api.mgmt.rebind.mementos.EntityMemento; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.api.sensor.Feed; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.core.entity.internal.EntityConfigMap; -import org.apache.brooklyn.core.mgmt.internal.EntityManagementSupport; -import org.apache.brooklyn.core.objs.BrooklynObjectInternal; -import org.apache.brooklyn.util.core.config.ConfigBag; - -import com.google.common.annotations.Beta; - -/** - * Extended Entity interface with additional functionality that is purely-internal (i.e. intended - * for the brooklyn framework only). - */ -@Beta -public interface EntityInternal extends BrooklynObjectInternal, EntityLocal, Rebindable { - - void addLocations(@Nullable Collection<? extends Location> locations); - - void removeLocations(Collection<? extends Location> locations); - - void clearLocations(); - - /** - * @deprecated since 0.8.0; use {@link SensorSupportInternal#setWithoutPublishing(AttributeSensor, Object)} via code like {@code sensors().setWithoutPublishing(attribute, val)}. - */ - <T> T setAttributeWithoutPublishing(AttributeSensor<T> sensor, T val); - - /** - * @deprecated since 0.7.0; instead just use methods on {@link ConfigurationSupportInternal} returned by {@link #config()} - */ - @Deprecated - EntityConfigMap getConfigMap(); - - /** - * @return a read-only copy of all the config key/value pairs on this entity. - * - * @deprecated since 0.7.0; instead just use methods on {@link ConfigurationSupportInternal} returned by {@link #config()}, - * e.g. getBag().getAllConfigAsConfigKeyMap(). - */ - @Deprecated - @Beta - Map<ConfigKey<?>,Object> getAllConfig(); - - /** - * Returns a read-only view of all the config key/value pairs on this entity, backed by a string-based map, - * including config names that did not match anything on this entity. - * - * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().getBag()} - */ - @Deprecated - ConfigBag getAllConfigBag(); - - /** - * Returns a read-only view of the local (i.e. not inherited) config key/value pairs on this entity, - * backed by a string-based map, including config names that did not match anything on this entity. - * - * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().getLocalBag()} - */ - @Deprecated - ConfigBag getLocalConfigBag(); - - /** - * @deprecated since 0.8.0; use {@link SensorSupportInternal#getAll()} via code like {@code sensors().getAll()}. - */ - @Beta - Map<AttributeSensor, Object> getAllAttributes(); - - /** - * @deprecated since 0.8.0; use {@link SensorSupportInternal#remove(AttributeSensor)} via code like {@code sensors().remove(attribute)}. - */ - @Beta - void removeAttribute(AttributeSensor<?> attribute); - - /** - * - * @deprecated since 0.7.0; use {@link #config()}, such as {@code entity.config().refreshInheritedConfig()} - */ - @Deprecated - void refreshInheritedConfig(); - - /** - * Must be called before the entity is started. - * - * @return this entity (i.e. itself) - */ - @Beta // for internal use only - EntityInternal configure(Map flags); - - /** - * @return Routings for accessing and inspecting the management context of the entity - */ - EntityManagementSupport getManagementSupport(); - - /** - * Should be invoked at end-of-life to clean up the item. - */ - @Beta - void destroy(); - - /** - * Returns the management context for the entity. If the entity is not yet managed, some - * operations on the management context will fail. - * - * Do not cache this object; instead call getManagementContext() each time you need to use it. - */ - ManagementContext getManagementContext(); - - /** - * Returns the task execution context for the entity. If the entity is not yet managed, some - * operations on the management context will fail. - * - * Do not cache this object; instead call getExecutionContext() each time you need to use it. - */ - ExecutionContext getExecutionContext(); - - /** returns the dynamic type corresponding to the type of this entity instance */ - @Beta - EntityDynamicType getMutableEntityType(); - - /** returns the effector registered against a given name */ - @Beta - Effector<?> getEffector(String effectorName); - - FeedSupport feeds(); - - /** - * @since 0.7.0-M2 - * @deprecated since 0.7.0-M2; use {@link #feeds()} - */ - @Deprecated - FeedSupport getFeedSupport(); - - Map<String, String> toMetadataRecord(); - - /** - * Users are strongly discouraged from calling or overriding this method. - * It is for internal calls only, relating to persisting/rebinding entities. - * This method may change (or be removed) in a future release without notice. - */ - @Override - @Beta - RebindSupport<EntityMemento> getRebindSupport(); - - @Override - RelationSupportInternal<Entity> relations(); - - /** - * Can be called to request that the entity be persisted. - * This persistence may happen asynchronously, or may not happen at all if persistence is disabled. - */ - void requestPersist(); - - @Override - SensorSupportInternal sensors(); - - @Override - PolicySupportInternal policies(); - - @Override - EnricherSupportInternal enrichers(); - - @Beta - public interface SensorSupportInternal extends Entity.SensorSupport { - /** - * - * Like {@link EntityLocal#setAttribute(AttributeSensor, Object)}, except does not publish an attribute-change event. - */ - <T> T setWithoutPublishing(AttributeSensor<T> sensor, T val); - - @Beta - Map<AttributeSensor<?>, Object> getAll(); - - @Beta - void remove(AttributeSensor<?> attribute); - } - - public interface FeedSupport { - Collection<Feed> getFeeds(); - - /** - * Adds the given feed to this entity. The feed will automatically be re-added on brooklyn restart. - */ - <T extends Feed> T addFeed(T feed); - - /** - * Removes the given feed from this entity. - * @return True if the feed existed at this entity; false otherwise - */ - boolean removeFeed(Feed feed); - - /** - * Removes all feeds from this entity. - * Use with caution as some entities automatically register feeds; this will remove those feeds as well. - * @return True if any feeds existed at this entity; false otherwise - */ - boolean removeAllFeeds(); - } - - @Beta - public interface PolicySupportInternal extends Entity.PolicySupport { - /** - * Removes all policy from this entity. - * @return True if any policies existed at this entity; false otherwise - */ - boolean removeAllPolicies(); - } - - @Beta - public interface EnricherSupportInternal extends Entity.EnricherSupport { - /** - * Removes all enricher from this entity. - * Use with caution as some entities automatically register enrichers; this will remove those enrichers as well. - * @return True if any enrichers existed at this entity; false otherwise - */ - boolean removeAll(); - } - - @Beta - public interface GroupSupportInternal extends Entity.GroupSupport { - /** - * Add this entity as a member of the given {@link Group}. Called by framework. - * <p> - * Users should call {@link Group#addMember(Entity)} instead; this method will then - * automatically be called. However, the reverse is not true (calling this method will - * not tell the group; this behaviour may change in a future release!) - */ - void add(Group group); - - /** - * Removes this entity as a member of the given {@link Group}. Called by framework. - * <p> - * Users should call {@link Group#removeMember(Entity)} instead; this method will then - * automatically be called. However, the reverse is not true (calling this method will - * not tell the group; this behaviour may change in a future release!) - */ - void remove(Group group); - } -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityPredicates.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityPredicates.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityPredicates.java deleted file mode 100644 index a618784..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityPredicates.java +++ /dev/null @@ -1,451 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import java.util.Collection; -import java.util.regex.Pattern; - -import javax.annotation.Nullable; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.Group; -import org.apache.brooklyn.api.location.Location; -import org.apache.brooklyn.api.sensor.AttributeSensor; -import org.apache.brooklyn.config.ConfigKey; -import org.apache.brooklyn.config.ConfigKey.HasConfigKey; -import org.apache.brooklyn.util.collections.CollectionFunctionals; -import org.apache.brooklyn.util.guava.SerializablePredicate; -import org.apache.brooklyn.util.javalang.Reflections; -import org.apache.brooklyn.util.text.StringPredicates; - -import com.google.common.base.Objects; -import com.google.common.base.Predicate; -import com.google.common.base.Predicates; - -@SuppressWarnings("serial") -public class EntityPredicates { - - public static Predicate<Entity> idEqualTo(final String val) { - return idSatisfies(Predicates.equalTo(val)); - } - - public static Predicate<Entity> idSatisfies(final Predicate<? super String> condition) { - return new IdSatisfies(condition); - } - - protected static class IdSatisfies implements SerializablePredicate<Entity> { - protected final Predicate<? super String> condition; - protected IdSatisfies(Predicate<? super String> condition) { - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getId()); - } - @Override - public String toString() { - return "idSatisfies("+condition+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> idEqualToOld(final T val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getId(), val); - } - }; - } - - // --------------------------- - - public static Predicate<Entity> displayNameEqualTo(final String val) { - return displayNameSatisfies(Predicates.equalTo(val)); - } - - public static Predicate<Entity> displayNameSatisfies(final Predicate<? super String> condition) { - return new DisplayNameSatisfies(condition); - } - - protected static class DisplayNameSatisfies implements SerializablePredicate<Entity> { - protected final Predicate<? super String> condition; - protected DisplayNameSatisfies(Predicate<? super String> condition) { - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getDisplayName()); - } - @Override - public String toString() { - return "displayNameSatisfies("+condition+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> displayNameEqualToOld(final T val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getDisplayName(), val); - } - }; - } - - /** @deprecated since 0.7.0 use {@link #displayNameSatisfies(Predicate)} to clarify this is *regex* matching - * (passing {@link StringPredicates#matchesRegex(String)} as the predicate) */ - public static Predicate<Entity> displayNameMatches(final String regex) { - return displayNameSatisfies(StringPredicates.matchesRegex(regex)); - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static class DisplayNameMatches implements SerializablePredicate<Entity> { - private final String regex; - DisplayNameMatches(String regex) { - this.regex = regex; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null && input.getDisplayName() != null) && input.getDisplayName().matches(regex); - } - @Override - public String toString() { - return "DisplayNameMatches("+regex+")"; - } - }; - - // --------------------------- - - public static Predicate<Entity> applicationIdEqualTo(final String val) { - return applicationIdSatisfies(Predicates.equalTo(val)); - } - - public static Predicate<Entity> applicationIdSatisfies(final Predicate<? super String> condition) { - return new ApplicationIdSatisfies(condition); - } - - protected static class ApplicationIdSatisfies implements SerializablePredicate<Entity> { - protected final Predicate<? super String> condition; - protected ApplicationIdSatisfies(Predicate<? super String> condition) { - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getApplicationId()); - } - @Override - public String toString() { - return "applicationIdSatisfies("+condition+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static Predicate<Entity> applicationIdEqualToOld(final String val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && val.equals(input.getApplicationId()); - } - }; - } - - // --------------------------- - - public static <T> Predicate<Entity> attributeEqualTo(final AttributeSensor<T> attribute, final T val) { - return attributeSatisfies(attribute, Predicates.equalTo(val)); - } - - public static <T> Predicate<Entity> attributeSatisfies(final AttributeSensor<T> attribute, final Predicate<T> condition) { - return new AttributeSatisfies<T>(attribute, condition); - } - - protected static class AttributeSatisfies<T> implements SerializablePredicate<Entity> { - protected final AttributeSensor<T> attribute; - protected final Predicate<T> condition; - private AttributeSatisfies(AttributeSensor<T> attribute, Predicate<T> condition) { - this.attribute = attribute; - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getAttribute(attribute)); - } - @Override - public String toString() { - return "attributeSatisfies("+attribute.getName()+","+condition+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> attributeEqualToOld(final AttributeSensor<T> attribute, final T val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getAttribute(attribute), val); - } - }; - } - - public static <T> Predicate<Entity> attributeNotEqualTo(final AttributeSensor<T> attribute, final T val) { - return attributeSatisfies(attribute, Predicates.not(Predicates.equalTo(val))); - } - - // --------------------------- - - public static <T> Predicate<Entity> configEqualTo(final ConfigKey<T> configKey, final T val) { - return configSatisfies(configKey, Predicates.equalTo(val)); - } - - public static <T> Predicate<Entity> configSatisfies(final ConfigKey<T> configKey, final Predicate<T> condition) { - return new ConfigKeySatisfies<T>(configKey, condition); - } - - public static <T> Predicate<Entity> configEqualTo(final HasConfigKey<T> configKey, final T val) { - return configEqualTo(configKey.getConfigKey(), val); - } - - public static <T> Predicate<Entity> configSatisfies(final HasConfigKey<T> configKey, final Predicate<T> condition) { - return new ConfigKeySatisfies<T>(configKey.getConfigKey(), condition); - } - - protected static class ConfigKeySatisfies<T> implements SerializablePredicate<Entity> { - protected final ConfigKey<T> configKey; - protected final Predicate<T> condition; - private ConfigKeySatisfies(ConfigKey<T> configKey, Predicate<T> condition) { - this.configKey = configKey; - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getConfig(configKey)); - } - @Override - public String toString() { - return "configKeySatisfies("+configKey.getName()+","+condition+")"; - } - } - - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> configEqualToOld(final ConfigKey<T> configKey, final T val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getConfig(configKey), val); - } - }; - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> configEqualToOld(final HasConfigKey<T> configKey, final T val) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getConfig(configKey), val); - } - }; - } - - // --------------------------- - - /** - * @param typeRegex a regular expression - * @return true if any of the interfaces implemented by the entity (including those derived) match typeRegex. - */ - public static Predicate<Entity> hasInterfaceMatching(String typeRegex) { - return new ImplementsInterface(typeRegex); - } - - protected static class ImplementsInterface implements SerializablePredicate<Entity> { - protected final Pattern pattern; - - public ImplementsInterface(String typeRegex) { - this.pattern = Pattern.compile(typeRegex); - } - - @Override - public boolean apply(@Nullable Entity input) { - if (input == null) return false; - for (Class<?> cls : Reflections.getAllInterfaces(input.getClass())) { - if (pattern.matcher(cls.getName()).matches()) { - return true; - } - } - return false; - } - } - - // --------------------------- - - /** - * Returns a predicate that determines if a given entity is a direct child of this {@code parent}. - */ - public static Predicate<Entity> isChildOf(final Entity parent) { - return new IsChildOf(parent); - } - - // if needed, could add parentSatisfies(...) - - protected static class IsChildOf implements SerializablePredicate<Entity> { - protected final Entity parent; - protected IsChildOf(Entity parent) { - this.parent = parent; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getParent(), parent); - } - @Override - public String toString() { - return "isChildOf("+parent+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> isChildOfOld(final Entity parent) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Objects.equal(input.getParent(), parent); - } - }; - } - - // --------------------------- - - public static Predicate<Entity> isMemberOf(final Group group) { - return new IsMemberOf(group); - } - - protected static class IsMemberOf implements SerializablePredicate<Entity> { - protected final Group group; - protected IsMemberOf(Group group) { - this.group = group; - } - @Override - public boolean apply(@Nullable Entity input) { - return (group != null) && (input != null) && group.hasMember(input); - } - @Override - public String toString() { - return "isMemberOf("+group+")"; - } - } - - /** @deprecated since 0.7.0 kept only to allow conversion of anonymous inner classes */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> isMemberOfOld(final Group group) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && group.hasMember(input); - } - }; - } - - // --------------------------- - - /** - * Create a predicate that matches any entity who has an exact match for the given location - * (i.e. {@code entity.getLocations().contains(location)}). - */ - public static <T> Predicate<Entity> locationsIncludes(Location location) { - return locationsSatisfy(CollectionFunctionals.contains(location)); - - } - - public static <T> Predicate<Entity> locationsSatisfy(final Predicate<Collection<Location>> condition) { - return new LocationsSatisfy(condition); - } - - protected static class LocationsSatisfy implements SerializablePredicate<Entity> { - protected final Predicate<Collection<Location>> condition; - protected LocationsSatisfy(Predicate<Collection<Location>> condition) { - this.condition = condition; - } - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && condition.apply(input.getLocations()); - } - @Override - public String toString() { - return "locationsSatisfy("+condition+")"; - } - } - - /** @deprecated since 0.7.0 use {@link #locationsIncludes(Location)} */ - @Deprecated - public static <T> Predicate<Entity> withLocation(final Location location) { - return locationsIncludes(location); - } - - /** @deprecated since 0.7.0 use {@link #locationsIncludes(Location)}, introduced to allow deserialization of anonymous inner class */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> withLocationOld(final Location location) { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && input.getLocations().contains(location); - } - }; - } - - // --------------------------- - - public static <T> Predicate<Entity> isManaged() { - return new IsManaged(); - } - - protected static class IsManaged implements SerializablePredicate<Entity> { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Entities.isManaged(input); - } - @Override - public String toString() { - return "isManaged()"; - } - } - - /** @deprecated since 0.7.0 use {@link #isManaged()} */ @Deprecated - public static <T> Predicate<Entity> managed() { - return isManaged(); - } - - /** @deprecated since 0.7.0 use {@link #isManaged()}, introduced to allow deserialization of anonymous inner class */ - @SuppressWarnings("unused") @Deprecated - private static <T> Predicate<Entity> managedOld() { - return new SerializablePredicate<Entity>() { - @Override - public boolean apply(@Nullable Entity input) { - return (input != null) && Entities.isManaged(input); - } - }; - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityRelations.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityRelations.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityRelations.java deleted file mode 100644 index d29b80c..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntityRelations.java +++ /dev/null @@ -1,179 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import java.util.Map; -import java.util.Set; - -import org.apache.brooklyn.api.entity.Application; -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.api.entity.Group; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.api.objs.BrooklynObject; -import org.apache.brooklyn.api.objs.BrooklynObject.RelationSupport; -import org.apache.brooklyn.api.policy.Policy; -import org.apache.brooklyn.api.relations.RelationshipType; -import org.apache.brooklyn.core.objs.BrooklynObjectInternal; -import org.apache.brooklyn.core.relations.AbstractBasicRelationSupport; -import org.apache.brooklyn.core.relations.RelationshipTypes; -import org.apache.brooklyn.util.collections.MutableMap; - -import com.google.common.annotations.Beta; - -/** TODO these relations are not used yet; see issue where this is introduced and email thread */ -@Beta -public class EntityRelations<T extends BrooklynObject> { - - /** {@link #MANAGER_OF} indicates that one entity is the manager of another entity, - * in the internal Brooklyn management hierarchy model. - * Apart from root {@link Application} entities, every deployed entity must have exactly one manager. - * The inverse relationship is {@link #MANAGED_BY}. */ - public static final RelationshipType<Entity,Entity> MANAGER_OF = RelationshipTypes.newRelationshipPair( - "manager", "managers", Entity.class, "manager_of", - "managed child", "managed children", Entity.class, "managed_by"); - /** Inverse of {@link #MANAGER_OF}. */ - public static final RelationshipType<Entity,Entity> MANAGED_BY = MANAGER_OF.getInverseRelationshipType(); - - /** {@link #GROUP_CONTAINS} indicates that one entity, typically a {@link Group}, - * has zero or more entities which are labelled as "members" of that group entity. - * What membership means will depend on the group entity. - * An entity may be a member of any number of other entities. - * The inverse relationship is {@link #IN_GROUP}. */ - public static final RelationshipType<Entity,Entity> GROUP_CONTAINS = RelationshipTypes.newRelationshipPair( - "group", "groups", Entity.class, "group_contains", - "member", "members", Entity.class, "in_group"); - /** Inverse of {@link #GROUP_CONTAINS}. */ - public static final RelationshipType<Entity,Entity> IN_GROUP = GROUP_CONTAINS.getInverseRelationshipType(); - - /** {@link #HAS_TARGET} indicates that one entity directs to one or more other entities. - * What this targeting relationship means depends on the targetter. - * The inverse relationship is {@link #TARGETTED_BY}. */ - public static final RelationshipType<Entity,Entity> HAS_TARGET = RelationshipTypes.newRelationshipPair( - "targetter", "targetters", Entity.class, "has_target", - "target", "targets", Entity.class, "targetted_by"); - /** Inverse of {@link #HAS_TARGET}. */ - public static final RelationshipType<Entity,Entity> TARGETTED_BY = HAS_TARGET.getInverseRelationshipType(); - - /** {@link #ACTIVE_PARENT_OF} indicates that one entity is should be considered as the logical parent of another, - * e.g. for presentation purposes to the end user. - * Frequently this relationship coincides with a {@link #MANAGED_BY} relationship, - * but sometimes some managed children are there for purposes the designers consider less important, - * and they can choose to suppress the {@link #ACTIVE_PARENT_OF} relationship - * so that the active children is a subset of the managed children. - * <p> - * One recommended consideration is whether the child should be shown in a default tree view. - * Whilst a user can always fina a way to see all managed children, - * it may be the case that only some of those are of primary interest, - * and it is to identify those that this relationship exists. - * <p> - * It is permitted that an entity be an {@link #ACTIVE_PARENT_OF} an entity for which it is not a manager, - * but in most cases a different relationship type is more appropriate where there is not also a management relationship. - * <p> - * The inverse relationship is {@link #ACTIVE_CHILD_OF}, - * and an entity should normally be an {@link #ACTIVE_CHILD_OF} zero or one entities. */ - public static final RelationshipType<Entity,Entity> ACTIVE_PARENT_OF = RelationshipTypes.newRelationshipPair( - "parent", "parents", Entity.class, "parent_of_active", - "active child", "active children", Entity.class, "active_child_of"); - /** Inverse of {@link #ACTIVE_PARENT_OF}. */ - public static final RelationshipType<Entity,Entity> ACTIVE_CHILD_OF = ACTIVE_PARENT_OF.getInverseRelationshipType(); - - /** {@link #HAS_POLICY} indicates that an entity has a policy associated to it. - * The inverse relationship is {@link #POLICY_FOR}. */ - public static final RelationshipType<Entity,Policy> HAS_POLICY = RelationshipTypes.newRelationshipPair( - "entity", "entities", Entity.class, "has_policy", - "policy", "policies", Policy.class, "policy_for"); - /** Inverse of {@link #HAS_POLICY}. */ - public static final RelationshipType<Policy,Entity> POLICY_FOR = HAS_POLICY.getInverseRelationshipType(); - - // ---- - - // TODO replace by relations stored in catalog when catalog supports arbitrary types - private static Map<String,RelationshipType<? extends BrooklynObject, ? extends BrooklynObject>> KNOWN_RELATIONSHIPS = MutableMap.of(); - private static void addRelationship(RelationshipType<? extends BrooklynObject, ? extends BrooklynObject> r) { - KNOWN_RELATIONSHIPS.put(r.getRelationshipTypeName(), r); - if (r.getInverseRelationshipType()!=null) { - KNOWN_RELATIONSHIPS.put(r.getInverseRelationshipType().getRelationshipTypeName(), r.getInverseRelationshipType()); - } - } - static { - addRelationship(MANAGER_OF); - addRelationship(GROUP_CONTAINS); - addRelationship(HAS_TARGET); - addRelationship(HAS_POLICY); - } - - /** Find the typed Relationship instance for the given relationship name, if known; - * behaviour is not guaranteed by the API if not known (hence the Beta marker), - * it may fail fast or return null or create a poor-man's relationship instance. - */ - @Beta - public static RelationshipType<? extends BrooklynObject, ? extends BrooklynObject> lookup(ManagementContext mgmt, String relationshipTypeName) { - if (relationshipTypeName==null) return null; - RelationshipType<? extends BrooklynObject, ? extends BrooklynObject> result = KNOWN_RELATIONSHIPS.get(relationshipTypeName); - if (result!=null) return result; - - /* TODO ultimately we'd like to support arbitrary relationships via persistence and lookup against the catalog; - * however for now, so that we can persist nicely (without catalog items for relationships) - * we are smart about the relationships defined here, and we return a poor-man's version for items elsewhere. - * - * for now, a poor-man's relationship; if not in catalog ultimately we should fail. */ - return RelationshipTypes.newRelationshipOneway("source", "sources", BrooklynObject.class, relationshipTypeName, "target", "targets", BrooklynObject.class); - } - - /** - * As {@link RelationSupport#getRelationshipTypes()} for the given object. Callers can use either method. - * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static <T extends BrooklynObject> Set<RelationshipType<? super T,? extends BrooklynObject>> getRelationshipTypes(T source) { - return (Set) ((BrooklynObjectInternal)source).relations().getLocalBackingStore().getRelationshipTypes(); - } - - /** - * As {@link RelationSupport#getRelations(RelationshipType)} for the given object. Callers can use either method. - * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static <T extends BrooklynObject,U extends BrooklynObject> Set<U> getRelations(RelationshipType<? super T,U> relationship, T source) { - return (Set) ((BrooklynObjectInternal)source).relations().getLocalBackingStore().getRelations((RelationshipType)relationship); - } - - /** - * As {@link RelationSupport#add(RelationshipType, BrooklynObject)} for the given object. Callers can use either method. - * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static <T extends BrooklynObject,U extends BrooklynObject> void add(T source, RelationshipType<? super T,? super U> relationship, U target) { - ((BrooklynObjectInternal)source).relations().getLocalBackingStore().add((RelationshipType)relationship, target); - if (relationship.getInverseRelationshipType()!=null) - ((BrooklynObjectInternal)target).relations().getLocalBackingStore().add((RelationshipType)relationship.getInverseRelationshipType(), source); - } - - /** - * As {@link RelationSupport#remove(RelationshipType, BrooklynObject)} for the given object. Callers can use either method. - * See {@link AbstractBasicRelationSupport} for a discussion of why double dispatch is used and both methods are present. - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - public static <T extends BrooklynObject,U extends BrooklynObject> void remove(T source, RelationshipType<? super T,? super U> relationship, U target) { - ((BrooklynObjectInternal)source).relations().getLocalBackingStore().remove((RelationshipType)relationship, target); - if (relationship.getInverseRelationshipType()!=null) - ((BrooklynObjectInternal)target).relations().getLocalBackingStore().remove((RelationshipType)relationship.getInverseRelationshipType(), source); - } - -} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/d03f254b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java ---------------------------------------------------------------------- diff --git a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java b/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java deleted file mode 100644 index 406485a..0000000 --- a/brooklyn-server/core/src/main/java/org/apache/brooklyn/core/entity/EntitySuppliers.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.core.entity; - -import org.apache.brooklyn.api.entity.Entity; -import org.apache.brooklyn.core.location.Machines; -import org.apache.brooklyn.location.ssh.SshMachineLocation; - -import com.google.common.base.Supplier; - -public class EntitySuppliers { - - public static Supplier<SshMachineLocation> uniqueSshMachineLocation(Entity entity) { - return new UniqueSshMchineLocation(entity); - } - - private static class UniqueSshMchineLocation implements Supplier<SshMachineLocation> { - private Entity entity; - - private UniqueSshMchineLocation() { /* for xstream */ - } - - private UniqueSshMchineLocation(Entity entity) { - this.entity = entity; - } - - @Override public SshMachineLocation get() { - return Machines.findUniqueMachineLocation(entity.getLocations(), SshMachineLocation.class).get(); - } - } -}
