public abstract class Command extends Object implements Restrictable
Implementation of the Command Pattern to create re-usable components which are performed during
StrolchTransactions as part of the execution of Services
Note: Do not call doCommand() from Services or other places. Add Command
instances to a StrolchTransaction by calling StrolchTransaction.addCommand(Command)
| Modifier and Type | Field and Description |
|---|---|
protected static org.slf4j.Logger |
logger |
| Constructor and Description |
|---|
Command(ComponentContainer container,
StrolchTransaction tx)
Instantiate a new
Command |
| Modifier and Type | Method and Description |
|---|---|
abstract void |
doCommand()
Clients implement this method to perform the work which is to be done in this
Command. |
protected <V> V |
getComponent(Class<V> clazz)
Allows the concrete
Command implementation access to StrolchComponents at
runtime |
protected ComponentContainer |
getContainer() |
protected <T extends StrolchPolicy> |
getPolicy(Class<T> policyClass,
PolicyContainer policyContainer)
Returns a
StrolchPolicy instance from the given parameters |
String |
getPrivilegeName() |
Object |
getPrivilegeValue() |
protected void |
runAs(String username,
PrivilegedRunnable runnable)
Performs the given
PrivilegedRunnable as a system user with the given username |
protected void |
runAs(String username,
SystemAction action)
Performs the given
SystemAction as a system user with the given username |
protected void |
runAsAgent(PrivilegedRunnable runnable)
Performs the given
PrivilegedRunnable as the privileged system user
StrolchConstants.SYSTEM_USER_AGENT |
protected void |
runAsAgent(SystemAction action)
Performs the given
SystemAction as the privileged system user StrolchConstants.SYSTEM_USER_AGENT |
protected <V> V |
runAsAgentWithResult(PrivilegedRunnableWithResult<V> runnable)
Performs the given
PrivilegedRunnableWithResult as the privileged system user
StrolchConstants.SYSTEM_USER_AGENT |
protected <V> V |
runAsAgentWithResult(SystemActionWithResult<V> action)
Performs the given
SystemAction as the privileged system user StrolchConstants.SYSTEM_USER_AGENT |
protected <V> V |
runWithResult(String username,
PrivilegedRunnableWithResult<V> runnable)
Performs the given
PrivilegedRunnableWithResult as a system user with the given username |
protected <T> T |
runWithResult(String username,
SystemActionWithResult<T> action)
Performs the given
SystemAction as a system user with the given username |
protected StrolchTransaction |
tx()
Returns the
StrolchTransaction bound to this Command's runtime |
abstract void |
undo()
Should the transaction fail, either due to a
Command throwing an exception when validate() is
called, or while committing the transaction, then this method should properly undo any changes it has done. |
abstract void |
validate()
To ensure that as few possibilities for exceptions as possible occur when
doCommand() is called, the
Command should verify any input data so that a doCommand() can be performed without exceptions |
public Command(ComponentContainer container, StrolchTransaction tx)
Commandcontainer - the ComponentContainer to access components at runtimetx - protected <V> V getComponent(Class<V> clazz) throws IllegalArgumentException
Command implementation access to StrolchComponents at
runtimeclazz - the type of component to be returnedClass which is registered on the ComponentContainerIllegalArgumentException - if the component with the given class does not existprotected ComponentContainer getContainer()
protected <T extends StrolchPolicy> T getPolicy(Class<T> policyClass, PolicyContainer policyContainer)
StrolchPolicy instance from the given parameterspolicyClass - the policy type to return. The simple name of the class determines the type of Policy to return.policyDefs - the policy defs from which to get the policy by using the simple name of the policy class to determine
the type of policy to returnprotected void runAs(String username, SystemAction action) throws PrivilegeException
SystemAction as a system user with the given usernameusername - the name of the system user to perform the action asaction - the action to performPrivilegeExceptionprotected <T> T runWithResult(String username, SystemActionWithResult<T> action) throws PrivilegeException
SystemAction as a system user with the given usernameusername - the name of the system user to perform the action asaction - the action to performPrivilegeExceptionprotected void runAs(String username, PrivilegedRunnable runnable) throws PrivilegeException
PrivilegedRunnable as a system user with the given usernameusername - the name of the system user to perform the action asrunnable - the runnable to performPrivilegeExceptionprotected <V> V runWithResult(String username, PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException
PrivilegedRunnableWithResult as a system user with the given usernameusername - the name of the system user to perform the action asrunnable - the runnable to performPrivilegeExceptionprotected void runAsAgent(SystemAction action) throws PrivilegeException
SystemAction as the privileged system user StrolchConstants.SYSTEM_USER_AGENTusername - the name of the system user to perform the action asaction - the action to performPrivilegeExceptionprotected <V> V runAsAgentWithResult(SystemActionWithResult<V> action) throws PrivilegeException
SystemAction as the privileged system user StrolchConstants.SYSTEM_USER_AGENTusername - the name of the system user to perform the action asaction - the action to performPrivilegeExceptionprotected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException
PrivilegedRunnable as the privileged system user
StrolchConstants.SYSTEM_USER_AGENTaction - the action to performPrivilegeExceptionprotected <V> V runAsAgentWithResult(PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException
PrivilegedRunnableWithResult as the privileged system user
StrolchConstants.SYSTEM_USER_AGENTaction - the action to performPrivilegeExceptionprotected StrolchTransaction tx()
StrolchTransaction bound to this Command's runtimeStrolchTransaction bound to this Command's runtimepublic String getPrivilegeName()
getPrivilegeName in interface RestrictableRestrictable.getPrivilegeName()public Object getPrivilegeValue()
getPrivilegeValue in interface RestrictableRestrictable.getPrivilegeValue()public abstract void validate()
doCommand() is called, the
Command should verify any input data so that a doCommand() can be performed without exceptionspublic abstract void doCommand()
Clients implement this method to perform the work which is to be done in this Command.
Note: Do not call this method directly, this method is called by the StrolchTransaction when the
transaction is committed. Add this Command to the transaction by calling
StrolchTransaction.addCommand(Command)
public abstract void undo()
Should the transaction fail, either due to a Command throwing an exception when validate() is
called, or while committing the transaction, then this method should properly undo any changes it has done. It is
imperative that this method does not throw further exceptions and that the state to be rolled back is remembered
in the Command during committing
Copyright © 2011–2016 Strolch. All rights reserved.