Skip to content

Commit d6a09d1

Browse files
committed
New effector to remove entities
1 parent efeebaa commit d6a09d1

File tree

1 file changed

+105
-0
lines changed

1 file changed

+105
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
Predicate<Entity> predicate = EntityInitializers.resolve(params, ENTITY_PREDICATE);
87+
if (predicate == null) {
88+
String entityId = EntityInitializers.resolve(params, ENTITY_ID);
89+
predicate = EntityPredicates.idEqualTo(entityId);
90+
}
91+
Optional<Entity> child = Iterables.tryFind(entity().getChildren(), predicate);
92+
if (child.isPresent()) {
93+
boolean success = entity().removeChild(child.get());
94+
if (success) {
95+
LOG.debug("{}: Removed child {} from {}", new Object[] { this, child.get(), entity() });
96+
return true;
97+
}
98+
}
99+
LOG.warn("{}: Could not find child of {} using {}", new Object[] { this, entity(), predicate });
100+
return false;
101+
}
102+
}
103+
}
104+
105+
}

0 commit comments

Comments
 (0)