[New] Added new methods using default realm in ExecutionHandler

This commit is contained in:
Robert von Burg 2023-02-16 09:57:35 +01:00
parent f81af240d1
commit 7085c18893
Signed by: eitch
GPG Key ID: 75DB9C85C74331F7
2 changed files with 347 additions and 8 deletions

View File

@ -44,6 +44,30 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
this.controllers = synchronizedMapOfMaps(new MapOfMaps<>(true));
}
@Override
public boolean isControlling(Activity activity) {
return this.controllers.containsElement(getDefaultRealm(), activity.getLocator());
}
@Override
public boolean isControlling(String realm, Activity activity) {
return this.controllers.containsElement(realm, activity.getLocator());
}
@Override
public boolean isControlling(Locator locator) {
return this.controllers.containsElement(getDefaultRealm(), locator);
}
public boolean isControlling(String realm, Locator locator) {
return this.controllers.containsElement(realm, locator);
}
@Override
public List<Controller> getControllers() {
return getControllers(getDefaultRealm());
}
@Override
public List<Controller> getControllers(String realm) {
Map<Locator, Controller> controllersByRealm = this.controllers.getMap(realm);
@ -52,16 +76,31 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
return new ArrayList<>(controllersByRealm.values());
}
@Override
public Controller getController(Activity activity) {
return getController(getDefaultRealm(), activity);
}
@Override
public Controller getController(String realm, Activity activity) {
return this.controllers.getElement(realm, activity.getLocator());
}
@Override
public Controller getController(Locator locator) {
return getController(getDefaultRealm(), locator);
}
@Override
public Controller getController(String realm, Locator locator) {
return this.controllers.getElement(realm, locator.trim(3));
}
@Override
public Set<Locator> getActiveActivitiesLocator() {
return getActiveActivitiesLocator(getDefaultRealm());
}
@Override
public Set<Locator> getActiveActivitiesLocator(String realm) {
if (this.controllers == null)
@ -126,6 +165,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
}
}
@Override
public void addForExecution(Activity activity) {
addForExecution(getDefaultRealm(), activity);
}
@Override
public void addForExecution(String realm, Activity activity) {
ExecutionHandlerState state = this.statesByRealm.getOrDefault(realm, ExecutionHandlerState.Running);
@ -143,6 +187,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
triggerExecution(realm);
}
@Override
public void toExecution(Activity activity) {
toExecution(getDefaultRealm(), activity);
}
@Override
public void toExecution(String realm, Activity activity) {
ExecutionHandlerState state = this.statesByRealm.getOrDefault(realm, ExecutionHandlerState.Running);
@ -160,6 +209,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
toExecution(controller);
}
@Override
public void toExecution(Locator activityLoc) {
toExecution(getDefaultRealm(), activityLoc);
}
@Override
public void toExecution(String realm, Locator activityLoc) {
ExecutionHandlerState state = this.statesByRealm.getOrDefault(realm, ExecutionHandlerState.Running);
@ -185,6 +239,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
}
}
@Override
public void removeFromExecution(Locator activityLoc) {
removeFromExecution(getDefaultRealm(), activityLoc);
}
@Override
public void removeFromExecution(String realm, Locator activityLoc) {
Locator rootElemLoc = activityLoc.trim(3);
@ -193,6 +252,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
getExecutor().submit(() -> notifyObserverRemove(controller));
}
@Override
public void clearAllCurrentExecutions() {
clearAllCurrentExecutions(getDefaultRealm());
}
@Override
public void clearAllCurrentExecutions(String realm) {
Map<Locator, Controller> removed = this.controllers.removeMap(realm);
@ -203,6 +267,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
getContainer().getRealmNames().forEach(realmName -> reloadActivitiesInExecution(ctx, realmName));
}
@Override
public void reloadActivitiesInExecution(PrivilegeContext ctx) {
reloadActivitiesInExecution(ctx, getDefaultRealm());
}
@Override
public void reloadActivitiesInExecution(PrivilegeContext ctx, String realmName) {
try (StrolchTransaction tx = openTx(realmName, ctx.getCertificate(), false)) {
@ -250,6 +319,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
return this.controllers.getMapOrDefault(realm, emptyMap()).values().stream();
}
@Override
public void triggerExecution() {
triggerExecution(getDefaultRealm());
}
@Override
public void triggerExecution(String realm) {
ExecutionHandlerState state = this.statesByRealm.getOrDefault(realm, ExecutionHandlerState.Running);
@ -297,6 +371,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
});
}
@Override
public void toExecuted(Locator actionLoc) {
toExecuted(getDefaultRealm(), actionLoc);
}
@Override
public void toExecuted(String realm, Locator locator) {
getExecutor().execute(() -> {
@ -321,6 +400,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
});
}
@Override
public void toStopped(Locator actionLoc) {
toStopped(getDefaultRealm(), actionLoc);
}
@Override
public void toStopped(String realm, Locator locator) {
getExecutor().execute(() -> {
@ -343,6 +427,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
});
}
@Override
public void toError(Locator actionLoc) {
toExecuted(getDefaultRealm(), actionLoc);
}
@Override
public void toError(String realm, Locator locator) {
getExecutor().execute(() -> {
@ -365,6 +454,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
});
}
@Override
public void toWarning(Locator actionLoc) {
toWarning(getDefaultRealm(), actionLoc);
}
@Override
public void toWarning(String realm, Locator locator) {
getExecutor().execute(() -> {
@ -387,6 +481,11 @@ public class EventBasedExecutionHandler extends ExecutionHandler {
});
}
@Override
public void archiveActivity(Locator activityLoc) {
archiveActivity(getDefaultRealm(), activityLoc);
}
@Override
public void archiveActivity(String realm, Locator activityLoc) {
getExecutor().execute(() -> {

View File

@ -1,6 +1,5 @@
package li.strolch.execution;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutorService;
@ -17,8 +16,10 @@ import li.strolch.model.activity.TimeOrdering;
import li.strolch.persistence.api.StrolchTransaction;
import li.strolch.privilege.model.Certificate;
import li.strolch.privilege.model.PrivilegeContext;
import li.strolch.runtime.configuration.ComponentConfiguration;
import li.strolch.runtime.privilege.PrivilegedRunnable;
import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
import li.strolch.utils.helper.StringHelper;
/**
* <p>
@ -26,8 +27,8 @@ import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
* </p>
*
* <p>
* To start the execution of an {@link Activity} add it to the {@link ExecutionHandler} by calling {@link
* #toExecution(String, Activity)} or {@link #toExecution(String, Activity)}. Actual execution is asynchronously
* To start the execution of an {@link Activity} add it to the {@link ExecutionHandler} by calling
* {@link #toExecution(String, Activity)} or {@link #toExecution(String, Activity)}. Actual execution is asynchronously
* performed and the {@link ExecutionPolicy} of the resources of the {@link Action Actions} will perform the actual
* execution.
* </p>
@ -41,15 +42,31 @@ import li.strolch.runtime.privilege.PrivilegedRunnableWithResult;
*/
public abstract class ExecutionHandler extends StrolchComponent {
public ExecutionHandler(ComponentContainer container, String componentName) {
super(container, componentName);
}
public static final String PROP_RESTART_EXECUTION = "restartExecution";
public static final String PROP_LOCK_RETRIES = "lockRetries";
public static final String PARAM_STATE = "state";
private String defaultRealm;
public ExecutionHandler(ComponentContainer container, String componentName) {
super(container, componentName);
}
@Override
public void initialize(ComponentConfiguration configuration) throws Exception {
Set<String> realmNames = getAgent().getContainer().getRealmNames();
if (realmNames.size() == 1)
this.defaultRealm = realmNames.iterator().next();
super.initialize(configuration);
}
protected String getDefaultRealm() {
if (StringHelper.isEmpty(this.defaultRealm))
throw new IllegalStateException("No default realm defined!");
return this.defaultRealm;
}
@Override
public StrolchTransaction openTx(String realm, Certificate cert, Class<?> action, boolean readOnly) {
return super.openTx(realm, cert, action.getName(), readOnly);
@ -69,6 +86,69 @@ public abstract class ExecutionHandler extends StrolchComponent {
return getExecutorService("ExecutionHandler");
}
/**
* Returns true if the given {@link Activity} is currently being controlled on the default realm
*
* @param activity
* the activity to check if it is being controlled
*
* @return true or false
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract boolean isControlling(Activity activity);
/**
* Returns true if the given {@link Activity} is currently being controlled on the given realm
*
* @param realm
* the realm
* @param activity
* the activity to check if it is being controlled
*
* @return true or false
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract boolean isControlling(String realm, Activity activity);
/**
* Returns true if the given {@link Activity} is currently being controlled on the default realm
*
* @param locator
* the locator of the activity to check if it is being controlled
*
* @return true or false
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract boolean isControlling(Locator locator);
/**
* Returns true if the given {@link Activity} is currently being controlled on the given realm
*
* @param realm
* the realm
* @param locator
* the locator of the activity to check if it is being controlled
*
* @return true or false
*/
public abstract boolean isControlling(String realm, Locator locator);
/**
* Returns the controllers for the default realm, if set
*
* @return the controllers
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract List<Controller> getControllers();
/**
* Returns the controllers for the given realm
*
@ -79,6 +159,19 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract List<Controller> getControllers(String realm);
/**
* Returns the controller for the default realm and activity, null if it does not exist
*
* @param activity
* the activity for which to get the controller
*
* @return the controller, or null if it does not exist
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract Controller getController(Activity activity);
/**
* Returns the controller for the given realm and activity, null if it does not exist
*
@ -91,6 +184,19 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract Controller getController(String realm, Activity activity);
/**
* Returns the controller for the default realm and activity, null if it does not exist
*
* @param locator
* the locator of the activity for which to get the controller
*
* @return the controller, or null if it does not exist
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract Controller getController(Locator locator);
/**
* Returns the controller for the given realm and activity, null if it does not exist
*
@ -104,7 +210,20 @@ public abstract class ExecutionHandler extends StrolchComponent {
public abstract Controller getController(String realm, Locator locator);
/**
* Registers the given {@link Activity} for execution. Execution is started when the concrete implementation deems necessary
* Registers the given {@link Activity} for execution on the default realm. Execution is started when the concrete
* implementation deems necessary
*
* @param activity
* the {@link Activity}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void addForExecution(Activity activity);
/**
* Registers the given {@link Activity} for execution. Execution is started when the concrete implementation deems
* necessary
*
* @param realm
* the realm where the {@link Activity} resides
@ -113,6 +232,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void addForExecution(String realm, Activity activity);
/**
* Registers the given {@link Activity} for execution on the default realm, and submits it for execution immediately
* in an asynchronous manner
*
* @param activity
* the {@link Activity}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toExecution(Activity activity);
/**
* Registers the given {@link Activity} for execution, and submits it for execution immediately in an asynchronous
* manner
@ -124,6 +255,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void toExecution(String realm, Activity activity);
/**
* Triggers the execution of the given {@link Activity}'s {@link Locator} on the default realm. If the
* {@link Controller} is available, then it is triggered
*
* @param activityLoc
* the {@link Locator} for the activity to execute
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toExecution(Locator activityLoc);
/**
* Triggers the execution of the given {@link Activity}'s {@link Locator}. If the {@link Controller} is available,
* then it is triggered
@ -143,6 +286,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void removeFromExecution(Controller controller);
/**
* Removes the given {@link Locator} for an {@link Activity} from execution from the default realm, so it is not
* executed further
*
* @param activityLoc
* the {@link Locator} of the {@link Activity}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void removeFromExecution(Locator activityLoc);
/**
* Removes the given {@link Locator} for an {@link Activity} from execution, so it is not executed further
*
@ -153,6 +308,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void removeFromExecution(String realm, Locator activityLoc);
/**
* Restarts all existing Activities on the given realm, which are not yet executed and already in state of
* execution
*
* @param ctx
* the privilege context
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void reloadActivitiesInExecution(PrivilegeContext ctx);
/**
* Restarts all existing Activities which are not yet executed and already in state of execution
*
@ -163,6 +330,14 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void reloadActivitiesInExecution(PrivilegeContext ctx, String realm);
/**
* Removes all currently registered {@link Activity Activities} from execution from the default realm
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void clearAllCurrentExecutions();
/**
* Removes all currently registered {@link Activity Activities} from execution
*
@ -171,6 +346,14 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void clearAllCurrentExecutions(String realm);
/**
* Triggers execution for all registered activities in the default realm
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void triggerExecution();
/**
* Triggers execution for all registered activities in the given realm
*
@ -211,6 +394,17 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void archiveActivity(String realm, Locator activityLoc);
/**
* Returns the {@link Set} of {@link Locator Locators} of {@link Activity Activities} which are registered for
* execution for the default realm
*
* @return a set of locators
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract Set<Locator> getActiveActivitiesLocator();
/**
* Returns the {@link Set} of {@link Locator Locators} of {@link Activity Activities} which are registered for
* execution for the given realm
@ -236,6 +430,17 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract DelayedExecutionTimer getDelayedExecutionTimer();
/**
* Completes the execution of the given {@link Action} with the given {@link Locator} on the default realm
*
* @param actionLoc
* the {@link Locator} of the {@link Action}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toExecuted(Locator actionLoc);
/**
* Completes the execution of the given {@link Action} with the given {@link Locator}
*
@ -246,6 +451,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void toExecuted(String realm, Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#STOPPED} on the default
* realm
*
* @param actionLoc
* the {@link Locator} of the {@link Action}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toStopped(Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#STOPPED}
*
@ -256,6 +473,18 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void toStopped(String realm, Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#WARNING} on the default
* realm
*
* @param actionLoc
* the {@link Locator} of the {@link Action}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toWarning(Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#WARNING}
*
@ -266,6 +495,17 @@ public abstract class ExecutionHandler extends StrolchComponent {
*/
public abstract void toWarning(String realm, Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#ERROR} on the default realm
*
* @param actionLoc
* the {@link Locator} of the {@link Action}
*
* @throws IllegalStateException
* if the default realm is not set!
*/
public abstract void toError(Locator actionLoc);
/**
* Sets the state of the {@link Action} with the given {@link Locator} to {@link State#ERROR}
*