Skip to content

Commit

Permalink
Merge pull request brooklyncentral#1296 from Nakomis/fix/yaml-additio…
Browse files Browse the repository at this point in the history
…nal-interfaces

Add additional interfaces when instantiating concrete implementation def...
  • Loading branch information
aledsage committed Apr 1, 2014
2 parents 45ea4ea + 0528db9 commit d629ee9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.slf4j.LoggerFactory;

import brooklyn.config.ConfigKey;
import brooklyn.entity.Application;
import brooklyn.entity.Entity;
import brooklyn.entity.basic.AbstractApplication;
import brooklyn.entity.basic.AbstractEntity;
Expand All @@ -41,6 +42,7 @@
import brooklyn.util.flags.FlagUtils;
import brooklyn.util.flags.FlagUtils.FlagConfigKeyAndValueRecord;
import brooklyn.util.guava.Maybe;
import brooklyn.util.javalang.Reflections;
import brooklyn.util.text.Strings;

import com.google.common.base.Function;
Expand Down Expand Up @@ -166,10 +168,20 @@ public <T extends Entity> EntitySpec<T> resolveSpec() {
public <T extends Entity> EntitySpec<T> resolveSpec(Class<T> type, Class<? extends T> optionalImpl) {
if (alreadyBuilt.getAndSet(true))
throw new IllegalStateException("Spec can only be used once: "+this);

EntitySpec<T> spec = EntitySpec.create(type);
if (optionalImpl != null) spec.impl(optionalImpl);


EntitySpec<T> spec;
if (optionalImpl != null) {
spec = EntitySpec.create(type).impl(optionalImpl);
} else if (type.isInterface()) {
spec = EntitySpec.create(type);
} else {
// If this is a concrete class, particularly for an Application class, we want the proxy
// to expose all interfaces it implements.
Class interfaceclazz = (Application.class.isAssignableFrom(type)) ? Application.class : Entity.class;
List<Class<?>> additionalInterfaceClazzes = Reflections.getAllInterfaces(type);
spec = EntitySpec.create(interfaceclazz).impl(type).additionalInterfaces(additionalInterfaceClazzes);
}

String name, templateId=null, planId=null;
if (template.isPresent()) {
name = template.get().getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,17 @@ public void testAppWithSameServerEntityStarts() throws Exception {
assertTrue(child instanceof BasicEntity, "child="+child);
}
}

@Test
public void testEntityImplExposesAllInterfacesIncludingStartable() throws Exception {
String yaml =
"services:\n"+
"- serviceType: brooklyn.test.entity.TestEntityImpl\n";

Application app = (Application) createStartWaitAndLogApplication(new StringReader(yaml));
TestEntity entity = (TestEntity) Iterables.getOnlyElement(app.getChildren());
assertTrue(entity.getCallHistory().contains("start"), "history="+entity.getCallHistory());
}

@Override
protected Logger getLogger() {
Expand Down

0 comments on commit d629ee9

Please sign in to comment.