brooklyn.entity.database.DatastoreMixins Maven / Gradle / Ivy
package brooklyn.entity.database;
import java.io.InputStream;
import javax.annotation.Nullable;
import brooklyn.config.ConfigKey;
import brooklyn.entity.Effector;
import brooklyn.entity.Entity;
import brooklyn.entity.basic.ConfigKeys;
import brooklyn.entity.effector.Effectors;
import brooklyn.event.AttributeSensor;
import brooklyn.event.basic.Sensors;
import brooklyn.util.ResourceUtils;
import brooklyn.util.flags.SetFromFlag;
import brooklyn.util.stream.KnownSizeInputStream;
import brooklyn.util.text.Strings;
public class DatastoreMixins {
private DatastoreMixins() {}
public static final AttributeSensor DATASTORE_URL = HasDatastoreUrl.DATASTORE_URL;
public static interface HasDatastoreUrl {
public static final AttributeSensor DATASTORE_URL = Sensors.newStringSensor("datastore.url",
"Primary contact URL for a datastore (e.g. mysql://localhost:3306/)");
}
public static final Effector EXECUTE_SCRIPT = CanExecuteScript.EXECUTE_SCRIPT;
public static interface CanExecuteScript {
public static final Effector EXECUTE_SCRIPT = Effectors.effector(String.class, "executeScript")
.description("executes the given script contents")
.parameter(String.class, "commands")
.buildAbstract();
}
public static final ConfigKey CREATION_SCRIPT_CONTENTS = CanGiveCreationScript.CREATION_SCRIPT_CONTENTS;
public static final ConfigKey CREATION_SCRIPT_URL = CanGiveCreationScript.CREATION_SCRIPT_URL;
public static interface CanGiveCreationScript {
@SetFromFlag("creationScriptContents")
public static final ConfigKey CREATION_SCRIPT_CONTENTS = ConfigKeys.newStringConfigKey("datastore.creation.script.contents", "Contensts of creation script to initialize the datastore", "");
@SetFromFlag("creationScriptUrl")
public static final ConfigKey CREATION_SCRIPT_URL = ConfigKeys.newStringConfigKey("datastore.creation.script.url", "URL of creation script to use to initialize the datastore (ignored if contents are specified)", "");
}
/** returns the creation script contents, if it exists, or null if none is defined (error if it cannot be loaded) */
@Nullable public static InputStream getDatabaseCreationScript(Entity entity) {
String url = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_URL);
if (!Strings.isBlank(url))
return new ResourceUtils(entity).getResourceFromUrl(url);
String contents = entity.getConfig(DatastoreMixins.CREATION_SCRIPT_CONTENTS);
if (!Strings.isBlank(contents))
return KnownSizeInputStream.of(contents);
return null;
}
/** An entity with the most common datastore config, sensors, and effectors */
public interface DatastoreCommon extends Entity, DatastoreMixins.HasDatastoreUrl, DatastoreMixins.CanExecuteScript, DatastoreMixins.CanGiveCreationScript {
}
}