Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Brooklyn entity creation API
/*
* Below is an example of 0.5.0-M2 code using ApplicationBuilder:
*/
public class MyExample extends ApplicationBuilder {
protected void doBuild() {
MySqlNode mysql = createChild(BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, myUrl));
}
}
/*
* 0.5.0-M2 code within an entity, to create other entities:
*/
@Override
public void postConstruct() {
super.postConstruct();
// ugly cast caused by groovy use...
MySqlNode mysql = (MySqlNode) addChild(
getEntityManager().createEntity(BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, scriptUrl)) );
// Or convenience (for groovy primarily):
MySqlNode mysql2 = (MySqlNode) addChild(
getEntityManager().createEntity(MutableMap.of(MySqlNode.CREATION_SCRIPT_URL, scriptUrl), MySqlNode.class));
}
/*
* For reference, pre 0.5.0-M2 code would call the constructor directly - now deprecated as horrible for distributed brooklyn.
*/
// Java
MySqlNode mysql = new MySqlNodeImpl(MutableMap.of(MySqlNode.CREATION_SCRIPT_URL, scriptUrl), this);
// Groovy
MySqlNode mysql = new MySqlNodeImpl(this, creationScriptUrl: scriptUrl)
/*
* Below are some examples of what the code could look like:
*/
// OPTION 1: buildEntity()
// buildEntity returns something extending EntitySpec that has an additional build() method;
// builder returned by buildEntity() so builder can have ref to ManagementContext
MySqlNode mysql1 = addChild(
getEntityManager().buildEntity(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, scriptUrl)
.build());
// OPTION 2: getEntityContext()
// The entityContext knows which entity is doing the calling, so knows who to add the child to etc
// entityContext methods would be very similar to ApplicationBuilder's methods
MySqlNode mysql2 = getEntityContext().createChild(BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, scriptUrl));
// OPTION 3: getEntityContext().buildEntity()
MySqlNode mysql3 = getEntityContext().buildChild(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, scriptUrl)
.build();
// OPTION 4: top-level methods, identical to those on ApplicationBuilder
// But I like grouping methods, rather than sticking more and more on AbstractEntity.
MySqlNode mysql4 = createChild(BasicEntitySpec.newInstance(MySqlNode.class)
.configure(MySqlNode.CREATION_SCRIPT_URL, scriptUrl));
// OPTION 5: we write custom EntitySpec classes for commonly used entities
MySqlNode mysql5 = createChild(MySqlNode.Spec.newInstance()
.creationScriptUrl(scriptUrl));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment