|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.jbbres.lib.actions.tools.elements.AbstractElementService com.jbbres.lib.actions.tools.elements.AbstractActionService<I,O>
I
- - represents the type of object that the action accepts as an
input.O
- - represents the type of object that the action generates as a
result.public abstract class AbstractActionService<I,O>
This abstract class provides default implementations for most of the methods
in the ActionService
interface.
AbstractActionService
is a parameterized abstract class with two
type variables, which means that all implementation of this class requires
two type arguments:
I
) represents the type of object that
the action accepts as an input.File[]
) and return a text (String
), the content of
your service class file will look like the following example:
package packageName; import java.io.File; import com.jbbres.lib.actions.elements.*; import com.jbbres.lib.actions.tools.elements.*; public class ActionNameService extends AbstractActionService<File[], String> { public ActionNameService(AbstractAction parent) { super(parent); } public String executeAction(File[] input, Parameters parameters) throws ActionExecutionException { // The logic for the action goes here return "Result of the action"; } }If an action does not have to deal with the input data handed it – for example, its role is to select some items in the file system – the first parameterized argument should be
java.lang.Void
. On the
opposite, if the action is able to handle any type of input data, the first
parameterized argument will be java.lang.Object
.java.lang.Object
informs that the
result of the action can be any type of object.
A particular case in action is a service not receiving input nor generating
result. Following the convention, such a service should extend
AbstractActionService<java.lang.Void, java.lang.Void>
.
Action(s) will recognise this specific case and will route the flow of
data around the action.
v1.1.0:
outputClass()
and outputClass(Class)
methods
Constructor Summary | |
---|---|
AbstractActionService(AbstractAction parent)
Instantiates a new action service. |
Method Summary | |
---|---|
void |
addExecutionListener(ActionExecutionListener l)
Adds a listener to the execution. |
java.lang.Object |
execute(java.lang.Object input,
Parameters parameters)
Executes the action. |
abstract O |
executeAction(I input,
Parameters parameters)
Executes the action. |
void |
fireActionExecutionEnd(int status,
java.lang.Object input,
Parameters parameters,
java.lang.Object result)
Calls the actionExecutionEnded(ActionExecutionEvent) method of all the listeners. |
void |
fireActionExecutionEnd(int status,
java.lang.Object input,
Parameters parameters,
java.lang.Object result,
java.lang.Throwable exception)
Calls the actionExecutionEnded(ActionExecutionEvent) method of all the listeners. |
void |
fireActionExecutionStart(java.lang.Object input,
Parameters parameters)
Calls the actionExecutionStarted(ActionExecutionEvent) method of all the
listeners. |
ActionExecutionListener[] |
getActionExecutionListeners()
Returns the listeners. |
java.lang.Class<? extends java.lang.Object> |
inputClass()
Returns the class of accepted input objects. |
boolean |
isValidInputClass(java.lang.Class<? extends java.lang.Object> clazz)
Returns true if an instance of the given class is an valid
input. |
java.lang.Class<? extends java.lang.Object> |
outputClass()
Returns the class of generated output objects. |
java.lang.Class<? extends java.lang.Object> |
outputClass(java.lang.Class<? extends java.lang.Object> inputClazz)
Returns the class of the object returned by the execute
method, depending on the class of the object received as an input. |
void |
removeExecutionListener(ActionExecutionListener l)
Removes a listener to the execution. |
Methods inherited from class com.jbbres.lib.actions.tools.elements.AbstractElementService |
---|
getDescription, getParent, getWorkflow |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public AbstractActionService(AbstractAction parent)
parent
- - the parent actionMethod Detail |
---|
public abstract O executeAction(I input, Parameters parameters) throws ActionExecutionException
The executeAction method has two parameters, input
, and
parameters
.
input
parameter contains (in most instances) the
output of the previous action in the workflow; it is almost always in the
form of a list. The type of input
is the first parameterized
argument of the service class. Remember that if the previous action
result is not compatible, or if this action is the first one in the
workflow, the input
parameter will be null
.
Make sure that you handle this case properly to avoid a
NullPointerException
.parameters
parameter contains the settings made in
the action’s user interface.null
or the
input
object.
Most action services operate on the input data given them from the
previous action. Action(s) includes an internal technology called
Smart-Casting that converts, when possible, the output of an
action into an acceptable input type for the next action. For example, if
an action returning an Image
is followed by an action that
requires a File
, Action(s) will create a temporary
file containing the image result of the first action and pass it as the
input of the second action. Smart-Casting can also convert a single
object into an array to ensure action’s compatibility. This means that,
as a developer, you do not have to worry too much about the type of
object you receive as an input or generate as an output.
The input object and the output object for an action are almost always
array objects. This is why many action services implement
executeAction
using the following general approach:
public String[] executeAction(final File[] input, final Parameters parameters) throws ActionExecutionException { if (input == null) return null; final String[] result = new String[input.length]; for (int i = 0; i < input.length; i++) { try { final FileInputStream fis = new FileInputStream(f); final FileChannel fc = fis.getChannel(); final long sz = fc.size(); final MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz); final Charset charset = Charset.forName("ISO-8859-1"); final CharsetDecoder decoder = charset.newDecoder(); final CharBuffer cb = decoder.decode(bb); result[i] = cb.toString(); fc.close(); fis.close(); } catch (final IOException e) { throw new ActionExecutionException(e); } } return result; }If your action service encounters an error that prevents it from proceeding, it should give information describing the error to Action(s), which then stops executing the workflow and displays an error message.
ActionExecutionException
exception. In the example above, you can see
how the code stops and informs Action(s) if an I/O exception
occurs when trying to read the file.
An action service has only two restrictions related to its implementation
of executeAction
:
executeAction
method until the picture is taken. So the
action has to implement whatever blocking algorithm, timeout logic, or
threading strategy is necessary until the picture is taken.executeAction
is run on a secondary
thread, if they want to display a dialog window it must be done on the
main thread. Especially, javax.swing.JDialog
and
javax.swing.JOptionPane
objects require a dialog owner to
display correctly. The
getWorkflow().dialogOwner()
method is adequate for this purpose; for
example:
input
- - in most instances, the output of the previous action in the
workflow; it is almost always in the form of a list. In some
cases, if the previous action result is not compatible or if
this action is the first one in the workflow, the
input
parameter will be null
. Make
sure that you handle this case properly to avoid a
NullPointerException
.parameters
- - the settings made in the action’s user interface.
ActionExecutionException
- if the execution failedpublic final java.lang.Object execute(java.lang.Object input, Parameters parameters) throws ActionExecutionException
ActionService
The method receives as an input the result of the previous action in the
workflow, casted in the input type of the action. As it might appends
that the action is the first one of the workflow, or that the previous
one did not generate an output compatible with the expected input of this
one, the value received as a parameter might be null
. The
method should handle this case in order to avoid workflow execution
errors.
This method returns the result of the execution, which will be used as
input of the next action. Action(s) considers that the execution
of the action is successful if no exception is raised during its
execution. If the result of this function is null
, it will
be considered as successful and the workflow will continue its execution.
An ActionExecutionException
means that the action failed during
its execution. Such an exception (and any other exception) will result in
ending the workflow execution.
If the action is not supposed to return any output (result of the
ActionService.outputClass(Class)
method is Void
), it returns a
null
object.
execute
in interface ActionService
input
- - the result of the previous actionparameters
- - the settings related to this action, to apply for this
execution.
ActionExecutionException
- - if the execution failedpublic java.lang.Class<? extends java.lang.Object> inputClass()
public java.lang.Class<? extends java.lang.Object> outputClass()
public java.lang.Class<? extends java.lang.Object> outputClass(java.lang.Class<? extends java.lang.Object> inputClazz)
execute
method, depending on the class of the object received as an input.
outputClass
in interface ActionService
inputClazz
- - the input class
execute
methodActionService.isValidInputClass(Class)
public boolean isValidInputClass(java.lang.Class<? extends java.lang.Object> clazz)
true
if an instance of the given class is an valid
input.
isValidInputClass
in interface ActionService
clazz
- - the class to test
true
if an instance of the given class is a valid
input. false
otherwise.ActionService.outputClass(Class)
public void fireActionExecutionStart(java.lang.Object input, Parameters parameters) throws ActionExecutionException
actionExecutionStarted(ActionExecutionEvent)
method of all the
listeners.
input
- - the inputparameters
- - the parameters
ActionExecutionException
- if an exception occurspublic void fireActionExecutionEnd(int status, java.lang.Object input, Parameters parameters, java.lang.Object result) throws ActionExecutionException
actionExecutionEnded(ActionExecutionEvent)
method of all the listeners.
status
- - the status of the action (SUCCEED or FAILED)input
- - the input objectparameters
- - the parametersresult
- - the result object
ActionExecutionException
- the action exceptionpublic void fireActionExecutionEnd(int status, java.lang.Object input, Parameters parameters, java.lang.Object result, java.lang.Throwable exception) throws ActionExecutionException
actionExecutionEnded(ActionExecutionEvent)
method of all the listeners.
status
- - the status of the action (SUCCEED or FAILED)input
- - the input objectparameters
- - the parametersresult
- - the result objectexception
- - the exception that occurs during the execution
ActionExecutionException
- the action exceptionpublic void addExecutionListener(ActionExecutionListener l)
ActionService
addExecutionListener
in interface ActionService
l
- - new listener.ActionService.removeExecutionListener(ActionExecutionListener)
public void removeExecutionListener(ActionExecutionListener l)
ActionService
removeExecutionListener
in interface ActionService
l
- - listener to removeActionService.addExecutionListener(ActionExecutionListener)
public ActionExecutionListener[] getActionExecutionListeners()
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |