Entity-author example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Everyone uses this interface when dealing with MySqlNode instances. | |
*/ | |
@ImplementedBy(MySqlNodeImpl.class) | |
public interface MySqlNode extends Entity, Startable { | |
// Note needs this generic to make sub-classing simple | |
public static class Recipe<T extends MySqlNode> extends SoftwareProcessEntity.Recipe<T> { | |
private PortRange port = PortRanges.fromString("3306, 13306+"); | |
private String creationScriptContents; | |
public static Recipe<MySqlNode> newInstance() { | |
return new Recipe<MySqlNode>(MySqlNode.class); | |
} | |
public Recipe(Class<T> type) { | |
super(type); | |
version("5.5.25a"); // TODO Or some better way to pass in defaults? | |
mirrorUrl(URI.create("http://www.mirrorservice.org/sites/ftp.mysql.com/")); | |
} | |
public Recipe port(PortRange val) { | |
this.port = val; | |
return this; | |
} | |
public Recipe creationScriptContents(String val) { | |
this.creationScriptContents = val; | |
return this; | |
} | |
// TODO There could be a reflection-based method for use by groovy that takes a map | |
// and then looks for the similarly named methods to call. This would allow also allow | |
// us to easily fail early if given invalid configuration options etc. | |
} | |
public static final AttributeSensor<String> MYSQL_URL = new BasicAttributeSensor<String>(String.class, "mysql.url"); | |
public static final AttributeSensor<Integer> PORT = new BasicAttributeSensor<Integer>(Integer.class, "mysql.port"); | |
// TODO How to relate the port attribute to the Recipe.port() config? | |
// In the current Brooklyn, we use PortAttributeSensorAndConfigKey which is concise+nifty. | |
// But could we somehow make it more obvious to the entity-author that a port is being | |
// chosen and being set? | |
} | |
/** | |
* Instances of the entity are only constructed by the framework... | |
*/ | |
public class MySqlNodeImpl extends SoftwareProcessEntityImpl implements MySqlNode { | |
final MySqlNode.Recipe recipe; | |
public MySqlNodeImpl(MySqlNode.Recipe recipe) { | |
super(recipe); | |
this.recipe = recipe; | |
} | |
@Override | |
protected void connectSensors() { | |
// blah, blah; code is very similar to what an entity-author would currently write | |
} | |
} |
can we merge this new Recipe
and the existing Factory
?
not sure i like either name though. :)
EntitySpec
? (and subclass MySqlNode.Spec
)
and could resuscitate @ImplementedBy
when used with default EntitySpec, i.e. EntitySpec
(aka Recipe
) has a method
getImplementationClass() { return implementationClassInConstructor ?: getClass().getAnnotion(ImplementedBy.class)... }
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
rather than @ImplementedBy could we have Recipe have a function which returns the type the framework should implement? then say e.g.