[New] Added ExecutionPolicy.runAsAgentWithResult()

This commit is contained in:
Robert von Burg 2018-08-23 10:51:07 +02:00
parent 15db21b45e
commit 844bb670b4
1 changed files with 52 additions and 35 deletions

View File

@ -15,6 +15,7 @@ import li.strolch.privilege.model.Certificate;
import li.strolch.privilege.model.PrivilegeContext; import li.strolch.privilege.model.PrivilegeContext;
import li.strolch.runtime.StrolchConstants; import li.strolch.runtime.StrolchConstants;
import li.strolch.runtime.privilege.PrivilegedRunnable; import li.strolch.runtime.privilege.PrivilegedRunnable;
import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
/** /**
* <p> * <p>
@ -66,10 +67,10 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
* </p> * </p>
* *
* @param action * @param action
* the {@link Action} to check if it can be executed * the {@link Action} to check if it can be executed
* *
* @return true if the action can be executed, false if not, i.e. the current state disallows the action to be * @return true if the action can be executed, false if not, i.e. the current state disallows the action to be
* executed * executed
*/ */
public boolean isExecutable(Action action) { public boolean isExecutable(Action action) {
return true; return true;
@ -79,7 +80,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
* Starts the execution of the given {@link Action}, i.e. sets the state to {@link State#EXECUTION} * Starts the execution of the given {@link Action}, i.e. sets the state to {@link State#EXECUTION}
* *
* @param action * @param action
* the action to start execution for * the action to start execution for
*/ */
public abstract void toExecution(Action action); public abstract void toExecution(Action action);
@ -87,34 +88,34 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
* Completes execution of the given {@link Action}, i.e. sets the state to {@link State#EXECUTED} * Completes execution of the given {@link Action}, i.e. sets the state to {@link State#EXECUTED}
* *
* @param action * @param action
* the action to set to executed * the action to set to executed
*/ */
public abstract void toExecuted(Action action); public abstract void toExecuted(Action action);
/** /**
* Stops the execution of this {@link Action} without completing its execution, i.e. sets the state to * Stops the execution of this {@link Action} without completing its execution, i.e. sets the state to {@link
* {@link State#STOPPED} * State#STOPPED}
* *
* @param action * @param action
* the action to stop execution for * the action to stop execution for
*/ */
public abstract void toStopped(Action action); public abstract void toStopped(Action action);
/** /**
* Sets this {@link Action} which should be in execution to an error state, i.e. sets the state to * Sets this {@link Action} which should be in execution to an error state, i.e. sets the state to {@link
* {@link State#ERROR} * State#ERROR}
* *
* @param action * @param action
* the action to set to error state * the action to set to error state
*/ */
public abstract void toError(Action action); public abstract void toError(Action action);
/** /**
* Sets this {@link Action} which should be in execution to a warning state, i.e. sets the state to * Sets this {@link Action} which should be in execution to a warning state, i.e. sets the state to {@link
* {@link State#WARNING} * State#WARNING}
* *
* @param action * @param action
* the action to set to warning state * the action to set to warning state
*/ */
public abstract void toWarning(Action action); public abstract void toWarning(Action action);
@ -135,7 +136,7 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
/** /**
* @return the {@link DelayedExecutionTimer} to simplify the delayed execution of an {@link Action}, e.g. for * @return the {@link DelayedExecutionTimer} to simplify the delayed execution of an {@link Action}, e.g. for
* simulated execution or simple wait tasks * simulated execution or simple wait tasks
*/ */
protected DelayedExecutionTimer getDelayedExecutionTimer() { protected DelayedExecutionTimer getDelayedExecutionTimer() {
return getComponent(ExecutionHandler.class).getDelayedExecutionTimer(); return getComponent(ExecutionHandler.class).getDelayedExecutionTimer();
@ -151,14 +152,13 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
} }
/** /**
* Opens a {@link StrolchTransaction} where the realm retrieved using * Opens a {@link StrolchTransaction} where the realm retrieved using {@link ComponentContainer#getRealm(Certificate)}.
* {@link ComponentContainer#getRealm(Certificate)}. This transaction should be used in a try-with-resource clause * This transaction should be used in a try-with-resource clause so it is properly closed.
* so it is properly closed.
* *
* @return the open {@link StrolchTransaction} * @return the open {@link StrolchTransaction}
* *
* @throws StrolchException * @throws StrolchException
* if the {@link StrolchRealm} does not exist with the given name * if the {@link StrolchRealm} does not exist with the given name
*/ */
protected StrolchTransaction openTx(PrivilegeContext ctx) throws StrolchException { protected StrolchTransaction openTx(PrivilegeContext ctx) throws StrolchException {
if (this.tx.isOpen()) if (this.tx.isOpen())
@ -172,11 +172,28 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
* Performs the given {@link PrivilegedRunnable} as the system user {@link StrolchConstants#SYSTEM_USER_AGENT} * Performs the given {@link PrivilegedRunnable} as the system user {@link StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param runnable * @param runnable
* the runnable to perform * the runnable to perform
* *
* @throws PrivilegeException if the agent is missing the privilege * @throws PrivilegeException
* if the agent is missing the privilege
*/ */
protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException { protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException {
getContainer().getPrivilegeHandler().runAs(StrolchConstants.SYSTEM_USER_AGENT, runnable); getContainer().getPrivilegeHandler().runAs(StrolchConstants.SYSTEM_USER_AGENT, runnable);
} }
/**
* Performs the given {@link PrivilegedRunnableWithResult} as the system user {@link
* StrolchConstants#SYSTEM_USER_AGENT}
*
* @param runnable
* the runnable to perform
*
* @return the result of the operation
*
* @throws PrivilegeException
* if the agent is missing the privilege
*/
protected <T> T runAsAgentWithResult(PrivilegedRunnableWithResult<T> runnable) throws PrivilegeException {
return getContainer().getPrivilegeHandler().runWithResult(StrolchConstants.SYSTEM_USER_AGENT, runnable);
}
} }