[Minor] Added simpler constructor to Command class

This commit is contained in:
Robert von Burg 2018-11-17 16:49:47 +01:00
parent fefa23b62c
commit 09bcdfd772
1 changed files with 77 additions and 69 deletions

View File

@ -1,12 +1,12 @@
/* /*
* Copyright 2013 Robert von Burg <eitch@eitchnet.ch> * Copyright 2013 Robert von Burg <eitch@eitchnet.ch>
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,9 +15,6 @@
*/ */
package li.strolch.service.api; package li.strolch.service.api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import li.strolch.agent.api.ComponentContainer; import li.strolch.agent.api.ComponentContainer;
import li.strolch.agent.api.StrolchComponent; import li.strolch.agent.api.StrolchComponent;
import li.strolch.model.PolicyContainer; import li.strolch.model.PolicyContainer;
@ -33,18 +30,20 @@ import li.strolch.privilege.model.Restrictable;
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; import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* <p> * <p>
* Implementation of the Command Pattern to create re-usable components which are performed during * Implementation of the Command Pattern to create re-usable components which are performed during {@link
* {@link StrolchTransaction StrolchTransactions} as part of the execution of {@link Service Services} * StrolchTransaction StrolchTransactions} as part of the execution of {@link Service Services}
* </p> * </p>
* *
* <p> * <p>
* <b>Note:</b> Do not call {@link #doCommand()} from {@link Service Services} or other places. Add {@link Command} * <b>Note:</b> Do not call {@link #doCommand()} from {@link Service Services} or other places. Add {@link Command}
* instances to a {@link StrolchTransaction} by calling {@link StrolchTransaction#addCommand(Command)} * instances to a {@link StrolchTransaction} by calling {@link StrolchTransaction#addCommand(Command)}
* </p> * </p>
* *
* @author Robert von Burg <eitch@eitchnet.ch> * @author Robert von Burg <eitch@eitchnet.ch>
*/ */
public abstract class Command implements Restrictable { public abstract class Command implements Restrictable {
@ -54,11 +53,23 @@ public abstract class Command implements Restrictable {
private final StrolchTransaction tx; private final StrolchTransaction tx;
/** /**
* Instantiate a new {@link Command} * Instantiate a new command
* *
* @param container
* the {@link ComponentContainer} to access components at runtime
* @param tx * @param tx
* the transaction
*/
public Command(StrolchTransaction tx) {
this.container = tx.getContainer();
this.tx = tx;
}
/**
* Instantiate a new command
*
* @param container
* the {@link ComponentContainer} to access components at runtime
* @param tx
* the transaction
*/ */
public Command(ComponentContainer container, StrolchTransaction tx) { public Command(ComponentContainer container, StrolchTransaction tx) {
this.container = container; this.container = container;
@ -66,16 +77,15 @@ public abstract class Command implements Restrictable {
} }
/** /**
* Allows the concrete {@link Command} implementation access to {@link StrolchComponent StrolchComponents} at * Allows the concrete command implementation access to {@link StrolchComponent StrolchComponents} at runtime
* runtime *
*
* @param clazz * @param clazz
* the type of component to be returned * the type of component to be returned
* *
* @return the component with the given {@link Class} which is registered on the {@link ComponentContainer} * @return the component with the given {@link Class} which is registered on the {@link ComponentContainer}
* *
* @throws IllegalArgumentException * @throws IllegalArgumentException
* if the component with the given class does not exist * if the component with the given class does not exist
*/ */
protected <V> V getComponent(Class<V> clazz) throws IllegalArgumentException { protected <V> V getComponent(Class<V> clazz) throws IllegalArgumentException {
return this.container.getComponent(clazz); return this.container.getComponent(clazz);
@ -90,13 +100,11 @@ public abstract class Command implements Restrictable {
/** /**
* Returns a {@link StrolchPolicy} instance from the given parameters * Returns a {@link StrolchPolicy} instance from the given parameters
* *
* @param policyClass * @param policyClass
* the policy type to return. The simple name of the class determines the type of Policy to return. * the policy type to return. The simple name of the class determines the type of Policy to return.
* @param policyDefs * @param policyContainer the container
* 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 return
*
* @return the policy * @return the policy
*/ */
protected <T extends StrolchPolicy> T getPolicy(Class<T> policyClass, PolicyContainer policyContainer) { protected <T extends StrolchPolicy> T getPolicy(Class<T> policyClass, PolicyContainer policyContainer) {
@ -110,12 +118,12 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link SystemAction} as a system user with the given username * Performs the given {@link SystemAction} as a system user with the given username
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param action * @param action
* the action to perform * the action to perform
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected void runAs(String username, SystemAction action) throws PrivilegeException { protected void runAs(String username, SystemAction action) throws PrivilegeException {
@ -124,14 +132,14 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link SystemAction} as a system user with the given username * Performs the given {@link SystemAction} as a system user with the given username
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param action * @param action
* the action to perform * the action to perform
* *
* @return the result * @return the result
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected <T> T runWithResult(String username, SystemActionWithResult<T> action) throws PrivilegeException { protected <T> T runWithResult(String username, SystemActionWithResult<T> action) throws PrivilegeException {
@ -140,12 +148,12 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link PrivilegedRunnable} as a system user with the given username * Performs the given {@link PrivilegedRunnable} as a system user with the given username
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param runnable * @param runnable
* the runnable to perform * the runnable to perform
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected void runAs(String username, PrivilegedRunnable runnable) throws PrivilegeException { protected void runAs(String username, PrivilegedRunnable runnable) throws PrivilegeException {
@ -154,14 +162,14 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link PrivilegedRunnableWithResult} as a system user with the given username * Performs the given {@link PrivilegedRunnableWithResult} as a system user with the given username
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param runnable * @param runnable
* the runnable to perform * the runnable to perform
* *
* @return the result * @return the result
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected <V> V runWithResult(String username, PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException { protected <V> V runWithResult(String username, PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException {
@ -170,12 +178,12 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link SystemAction} as the privileged system user {@link StrolchConstants#SYSTEM_USER_AGENT} * Performs the given {@link SystemAction} as the privileged system user {@link StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param action * @param action
* the action to perform * the action to perform
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected void runAsAgent(SystemAction action) throws PrivilegeException { protected void runAsAgent(SystemAction action) throws PrivilegeException {
@ -184,14 +192,14 @@ public abstract class Command implements Restrictable {
/** /**
* Performs the given {@link SystemAction} as the privileged system user {@link StrolchConstants#SYSTEM_USER_AGENT} * Performs the given {@link SystemAction} as the privileged system user {@link StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param username * @param username
* the name of the system user to perform the action as * the name of the system user to perform the action as
* @param action * @param action
* the action to perform * the action to perform
* *
* @return the result * @return the result
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected <V> V runAsAgentWithResult(SystemActionWithResult<V> action) throws PrivilegeException { protected <V> V runAsAgentWithResult(SystemActionWithResult<V> action) throws PrivilegeException {
@ -199,12 +207,12 @@ public abstract class Command implements Restrictable {
} }
/** /**
* Performs the given {@link PrivilegedRunnable} as the privileged system user * Performs the given {@link PrivilegedRunnable} as the privileged system user {@link
* {@link StrolchConstants#SYSTEM_USER_AGENT} * StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param action * @param action
* the action to perform * the action to perform
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException { protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException {
@ -212,14 +220,14 @@ public abstract class Command implements Restrictable {
} }
/** /**
* Performs the given {@link PrivilegedRunnableWithResult} as the privileged system user * Performs the given {@link PrivilegedRunnableWithResult} as the privileged system user {@link
* {@link StrolchConstants#SYSTEM_USER_AGENT} * StrolchConstants#SYSTEM_USER_AGENT}
* *
* @param action * @param action
* the action to perform * the action to perform
* *
* @return the result * @return the result
* *
* @throws PrivilegeException * @throws PrivilegeException
*/ */
protected <V> V runAsAgentWithResult(PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException { protected <V> V runAsAgentWithResult(PrivilegedRunnableWithResult<V> runnable) throws PrivilegeException {
@ -228,7 +236,7 @@ public abstract class Command implements Restrictable {
/** /**
* Returns the {@link StrolchTransaction} bound to this {@link Command}'s runtime * Returns the {@link StrolchTransaction} bound to this {@link Command}'s runtime
* *
* @return the {@link StrolchTransaction} bound to this {@link Command}'s runtime * @return the {@link StrolchTransaction} bound to this {@link Command}'s runtime
*/ */
protected StrolchTransaction tx() { protected StrolchTransaction tx() {
@ -261,11 +269,11 @@ public abstract class Command implements Restrictable {
* <p> * <p>
* Clients implement this method to perform the work which is to be done in this {@link Command}. * Clients implement this method to perform the work which is to be done in this {@link Command}.
* </p> * </p>
* *
* <p> * <p>
* <b>Note:</b> Do not call this method directly, this method is called by the {@link StrolchTransaction} when the * <b>Note:</b> Do not call this method directly, this method is called by the {@link StrolchTransaction} when the
* transaction is committed. Add this {@link Command} to the transaction by calling * transaction is committed. Add this {@link Command} to the transaction by calling {@link
* {@link StrolchTransaction#addCommand(Command)} * StrolchTransaction#addCommand(Command)}
* </p> * </p>
*/ */
public abstract void doCommand(); public abstract void doCommand();