[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>
@ -92,8 +93,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
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
@ -101,8 +102,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
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
@ -110,8 +111,8 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
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
@ -151,9 +152,8 @@ 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}
* *
@ -174,9 +174,26 @@ public abstract class ExecutionPolicy extends StrolchPolicy {
* @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);
}
} }