Package io.toro.martini
Class GloopMethods
- java.lang.Object
-
- io.toro.martini.GloopMethods
-
public final class GloopMethods extends Object
Contains utility methods for use in Gloop.
-
-
Constructor Summary
Constructors Constructor Description GloopMethods()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static String
contextToJsonString(GloopExecutionContext context)
Return a JSON string representation of the current Gloop variable context.static void
cursorAppend(GloopCursor cursor, Object value)
Add new value(s) to the cursor.static void
cursorClose(GloopCursor cursor)
Close a cursor.static Object
cursorGetValue(GloopCursor cursor)
Get the value of the current record from the cursor.static boolean
cursorHasNext(GloopCursor cursor)
Check whether or not the given Gloop cursor has another record.static int
cursorLast(GloopCursor cursor)
Iterate through the cursor until the last record is reached.static void
cursorNextRecord(GloopCursor cursor)
Move the cursor to the next record.static long
cursorSize(GloopCursor cursor)
Get the size of a cursor.static int
cursorSkip(GloopCursor cursor, int skipCount)
Try to skip the next n records in a cursor.static GloopModel
cursorToArray(GloopCursor cursor)
Iterate through a cursor, and create an in-memory Gloop model array.static GloopModel
dynamicModelToMapModelArray(GloopModel anonymousModel)
Create an array of models describing the property names and values of a provided model.static GloopObject<?>
fromGloopDoc(String string)
Creates a gloop object based from the providedstring
static String
getSystemProperty(String name, String defaultVal)
Get a Java system property.static long
getSystemTime()
Get the current system time in milliseconds.static void
loadContextFromAvro(GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static void
loadContextFromAvro(List<String> names, byte[] data, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static void
loadContextFromAvro(List<String> names, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static void
loadContextFromAvro(List<String> names, String filename, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static GloopModel
mapModelArrayToDynamicModel(GloopModel mapModelArray)
Create a new model from an array of Gloop models, which describe each of the new model's property names and values.static Object
runScriptlet(String scriptlet, GloopExecutionContext context)
Run a piece of Groovy code within the current context.static Object
runScriptlet(String language, String scriptlet, GloopExecutionContext context)
Run a piece of code within the current context.static void
saveContextAsAvro(GloopExecutionContext context)
Saves the properties with the given names from the context to a file in the martini home, in the Apache Avro data file format.static void
saveContextAsAvro(List<String> names, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static void
saveContextAsAvro(List<String> names, OutputStream outputStream, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static void
saveContextAsAvro(List<String> names, String filename, GloopExecutionContext context)
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.static String
setSystemProperty(String name, String value)
Set a Java system property.static void
throwApiException(GloopModel exception)
Throws the provided exception.static void
throwException(String message)
Throws an exception with the given message.static void
throwException(String message, Throwable cause)
Throws an exception with the given message and cause.(package private) static void
throwException(Throwable cause)
-
-
-
Method Detail
-
getSystemTime
@GloopParameter(name="systemTime") public static long getSystemTime()
Get the current system time in milliseconds.- Returns:
- the time in milliseconds
- See Also:
System.currentTimeMillis()
-
getSystemProperty
@GloopParameter(name="systemProperty") public static String getSystemProperty(String name, String defaultVal)
Get a Java system property.- Parameters:
name
- the name of the property to getdefaultVal
- the default value to return if the property isn't set- Returns:
- the system property value;
null
if the property isn't set - See Also:
System.getProperty(String, String)
-
setSystemProperty
public static String setSystemProperty(String name, String value)
Set a Java system property.- Parameters:
name
- the name of the propertyvalue
- the value to assign to the system property- Returns:
- the previous value of the system property, or
null
if it did not have one. - See Also:
System.setProperty(String, String)
-
runScriptlet
public static Object runScriptlet(String scriptlet, GloopExecutionContext context) throws Throwable
Run a piece of Groovy code within the current context.- Parameters:
scriptlet
- the snippet to execute against the current contextcontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- the result of the scriptlet
- Throws:
Throwable
-
runScriptlet
public static Object runScriptlet(String language, String scriptlet, GloopExecutionContext context) throws Throwable
Run a piece of code within the current context.- Parameters:
language
- the language the snippet is written inscriptlet
- the snippet to execute against the current contextcontext
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- the result of the scriptlet
- Throws:
Throwable
-
cursorHasNext
public static boolean cursorHasNext(GloopCursor cursor)
Check whether or not the given Gloop cursor has another record.- Returns:
- true if the cursor has more records; false otherwise
-
cursorNextRecord
public static void cursorNextRecord(GloopCursor cursor)
Move the cursor to the next record.
-
cursorGetValue
public static Object cursorGetValue(GloopCursor cursor)
Get the value of the current record from the cursor.- Returns:
- the current value of the record the cursor is pointing to
-
cursorAppend
public static void cursorAppend(@GloopParameter(allowNull=false) GloopCursor cursor, @GloopParameter(allowNull=false) Object value)
Add new value(s) to the cursor. Ifvalue
is a collections, array, or an array ofGloopObject
s, it will be iterated, and elements will be added individually.- Parameters:
value
- the value(s) to add to the cursor
-
cursorSize
public static long cursorSize(GloopCursor cursor)
Get the size of a cursor.- Returns:
- the size of the cursor
-
cursorSkip
public static int cursorSkip(GloopCursor cursor, int skipCount)
Try to skip the next n records in a cursor.- Parameters:
skipCount
- how many records to skip over- Returns:
- how many records were actually skipped
-
cursorLast
public static int cursorLast(GloopCursor cursor)
Iterate through the cursor until the last record is reached.- Returns:
- the number of records that were skipped over
-
cursorClose
public static void cursorClose(GloopCursor cursor)
Close a cursor.
-
cursorToArray
@GloopObjectParameter("output{\ncursorValues[]*{\n}\n}") public static GloopModel cursorToArray(GloopCursor cursor)
Iterate through a cursor, and create an in-memory Gloop model array.- Returns:
- the model array containing all cursor data
-
contextToJsonString
public static String contextToJsonString(GloopExecutionContext context)
Return a JSON string representation of the current Gloop variable context.- Parameters:
context
- the Gloop execution context (automatically mapped by Gloop)- Returns:
- a JSON string representing the Gloop context
-
throwException
public static void throwException(@GloopParameter(allowNull=false) String message) throws Exception
Throws an exception with the given message.- Parameters:
message
- the exception message- Throws:
Exception
- the to-be-thrown exception
-
throwException
public static void throwException(@GloopParameter("Exception Message") String message, @GloopParameter("Cause") Throwable cause) throws Exception
Throws an exception with the given message and cause.- Throws:
Exception
- the to-be-thrown exception
-
throwException
static void throwException(@GloopParameter("Cause") Throwable cause) throws Exception
- Throws:
Exception
-
throwApiException
public static void throwApiException(@GloopObjectParameter("apiException#io.toro.martini.api.APIException{\n}") @GloopParameter(allowNull=false) GloopModel exception) throws APIException
Throws the provided exception.- Parameters:
exception
- to throw- Throws:
APIException
- Since:
- 1.0.2
-
dynamicModelToMapModelArray
@GloopObjectParameter("output{\n output[]{\n name\n value:object\n }\n}") public static GloopModel dynamicModelToMapModelArray(@GloopParameter GloopModel anonymousModel)
Create an array of models describing the property names and values of a provided model. Each model in the resulting array will have two properties namedname
andvalue
. This one-liner will create a model for every property in the provided model, and map the property's name toname
, and value tovalue
. Consider the following model:{ "TORO": "Cloud", "Martini": "Ninja" }
By providing this model to this one-liner, the resulting array of models will be:[ { "name": "TORO", "value": "Cloud" }, { "name": "Martini", "value": "Ninja" } ]
- Parameters:
anonymousModel
- the model whose structure will be recreated as an array of name-value pairs- Returns:
- an array of models describing the property names and values of the model
-
mapModelArrayToDynamicModel
@GloopParameter public static GloopModel mapModelArrayToDynamicModel(@GloopObjectParameter("model[]{\n name\n value:object\n}\n") GloopModel mapModelArray)
Create a new model from an array of Gloop models, which describe each of the new model's property names and values. For example, consider an array of two models, whosename
s are TORO and Martini, and whosevalue
s are Cloud and Ninja respectively.[ { "name": "TORO", "value": "Cloud" }, { "name": "Martini", "value": "Ninja" } ]
Using this one-liner, the resulting model would be represented in JSON as follows:{ "TORO": "Cloud", "Martini": "Ninja" }
- Parameters:
mapModelArray
- an array of models describing the property names and values of the new model- Returns:
- new model whose structure is derived from the array of models
-
fromGloopDoc
public static GloopObject<?> fromGloopDoc(@GloopParameter(allowNull=false) String string)
Creates a gloop object based from the providedstring
- Parameters:
string
- string containing information about the gloop object- Returns:
- gloop object created from the string
-
saveContextAsAvro
public static void saveContextAsAvro(GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to a file in the martini home, in the Apache Avro data file format. The file will be stored at <Martini Home>/data/contexts/<Gloop-Service-Name>.avro- Parameters:
context
- Gloop Execution Context- Throws:
IOException
- See Also:
loadContextFromAvro(List, byte[], GloopExecutionContext)
-
saveContextAsAvro
public static void saveContextAsAvro(List<String> names, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format. The file will be stored at <Martini Home>/data/contexts/<Gloop-Service-Name>.avro- Parameters:
names
- root-level properties to save. If null or empty, all properties will be savedcontext
- Gloop Execution Context- Throws:
IOException
- See Also:
loadContextFromAvro(List, byte[], GloopExecutionContext)
-
saveContextAsAvro
public static void saveContextAsAvro(List<String> names, @GloopParameter(allowNull=false) String filename, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
names
- root-level properties to save. If null or empty, all properties will be savedfilename
- the file to write the Avro data to. It will be stored in %lt;Martini Home>>/data/contextscontext
- Gloop Execution Context- Throws:
IOException
- See Also:
The file will be stored at Martini Home/data/contexts/
-
saveContextAsAvro
public static void saveContextAsAvro(List<String> names, @GloopParameter(allowNull=false) OutputStream outputStream, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
names
- root-level properties to save. If null or empty, all properties will be savedoutputStream
- where to write the Avro data tocontext
- Gloop Execution Context- Throws:
IOException
- See Also:
loadContextFromAvro(List, byte[], GloopExecutionContext)
-
loadContextFromAvro
public static void loadContextFromAvro(GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
context
- Gloop Execution Context- Throws:
IOException
- See Also:
The data will be loaded from Martini Home/data/contexts/Gloop-Service-Name.avro
-
loadContextFromAvro
public static void loadContextFromAvro(List<String> names, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
names
- root-level properties to save, if null or empty, all properties will be savedcontext
- Gloop Execution Context- Throws:
IOException
- See Also:
The data will be loaded from Martini Home/data/contexts/Gloop-Service-Name.avro
-
loadContextFromAvro
public static void loadContextFromAvro(List<String> names, String filename, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
names
- root-level properties to save, if null or empty, all properties will be savedfilename
- where to read the avro data from in the <Martini Home>/data/contexts/dircontext
- Gloop Execution Context- Throws:
IOException
- See Also:
The data will be loaded from Martini Home/data/contexts/filename
-
loadContextFromAvro
public static void loadContextFromAvro(List<String> names, @GloopParameter(allowNull=false) byte[] data, GloopExecutionContext context) throws IOException
Saves the properties with the given names from the context to an output stream, in the Apache Avro data file format.- Parameters:
names
- root-level properties to save, if null or empty, all properties will be saveddata
- where to read the avro data fromcontext
- Gloop Execution Context- Throws:
IOException
- See Also:
saveContextAsAvro(List, OutputStream, GloopExecutionContext)
-
-