|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.brooklyn.core.effector; |
| 20 | + |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.brooklyn.api.effector.Effector; |
| 24 | +import org.apache.brooklyn.api.entity.Entity; |
| 25 | +import org.apache.brooklyn.config.ConfigKey; |
| 26 | +import org.apache.brooklyn.core.config.ConfigKeys; |
| 27 | +import org.apache.brooklyn.core.effector.Effectors.EffectorBuilder; |
| 28 | +import org.apache.brooklyn.core.entity.EntityInitializers; |
| 29 | +import org.apache.brooklyn.core.entity.EntityPredicates; |
| 30 | +import org.apache.brooklyn.util.core.config.ConfigBag; |
| 31 | +import org.slf4j.Logger; |
| 32 | +import org.slf4j.LoggerFactory; |
| 33 | + |
| 34 | +import com.google.common.annotations.Beta; |
| 35 | +import com.google.common.base.Optional; |
| 36 | +import com.google.common.base.Predicate; |
| 37 | +import com.google.common.collect.Iterables; |
| 38 | +import com.google.common.reflect.TypeToken; |
| 39 | + |
| 40 | +/** |
| 41 | + * @since 0.11.0 |
| 42 | + */ |
| 43 | +@Beta |
| 44 | +public class RemoveEntityEffector extends AddEffector { |
| 45 | + |
| 46 | + private static final Logger LOG = LoggerFactory.getLogger(RemoveEntityEffector.class); |
| 47 | + |
| 48 | + public static final ConfigKey<String> ENTITY_ID = ConfigKeys.builder(String.class) |
| 49 | + .name("entityId") |
| 50 | + .description("The id of the entity to be removed") |
| 51 | + .build(); |
| 52 | + |
| 53 | + public static final ConfigKey<Predicate<Entity>> ENTITY_PREDICATE = ConfigKeys.builder(new TypeToken<Predicate<Entity>>() { }) |
| 54 | + .name("predicate") |
| 55 | + .description("A predicate that will match the entity to be removed") |
| 56 | + .build(); |
| 57 | + |
| 58 | + public RemoveEntityEffector(ConfigBag params) { |
| 59 | + super(newEffectorBuilder(params).build()); |
| 60 | + } |
| 61 | + |
| 62 | + public RemoveEntityEffector(Map<String,String> params) { |
| 63 | + this(ConfigBag.newInstance(params)); |
| 64 | + } |
| 65 | + |
| 66 | + public static EffectorBuilder<Boolean> newEffectorBuilder(ConfigBag params) { |
| 67 | + EffectorBuilder<Boolean> eff = (EffectorBuilder) AddEffector.newEffectorBuilder(Boolean.class, params); |
| 68 | + eff.impl(new Body(eff.buildAbstract(), params)); |
| 69 | + return eff; |
| 70 | + } |
| 71 | + |
| 72 | + protected static class Body extends EffectorBody<Boolean> { |
| 73 | + protected final Effector<?> effector; |
| 74 | + protected final ConfigBag config; |
| 75 | + |
| 76 | + protected Object mutex = new Object[0]; |
| 77 | + |
| 78 | + public Body(Effector<?> eff, ConfigBag config) { |
| 79 | + this.effector = eff; |
| 80 | + this.config = config; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public Boolean call(final ConfigBag params) { |
| 85 | + synchronized (mutex) { |
| 86 | + ConfigBag all = ConfigBag.newInstanceCopying(config).putAll(params); |
| 87 | + Predicate<Entity> predicate = EntityInitializers.resolve(all, ENTITY_PREDICATE); |
| 88 | + if (predicate == null) { |
| 89 | + String entityId = EntityInitializers.resolve(all, ENTITY_ID); |
| 90 | + predicate = EntityPredicates.idEqualTo(entityId); |
| 91 | + } |
| 92 | + Optional<Entity> child = Iterables.tryFind(entity().getChildren(), predicate); |
| 93 | + if (child.isPresent()) { |
| 94 | + boolean success = entity().removeChild(child.get()); |
| 95 | + if (success) { |
| 96 | + LOG.debug("{}: Removed child {} from {}", new Object[] { this, child.get(), entity() }); |
| 97 | + return true; |
| 98 | + } |
| 99 | + } |
| 100 | + LOG.warn("{}: Could not find child of {} using {}", new Object[] { this, entity(), predicate }); |
| 101 | + return false; |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments