From d0aa763f6a70a972fb55f62ea2824cd3185a689d Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Fri, 15 Sep 2017 10:35:37 +0200 Subject: [PATCH] [New] Added StrolchJob for recurring jobs --- .../main/java/li/strolch/job/StrolchJob.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java diff --git a/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java new file mode 100644 index 000000000..e8da50923 --- /dev/null +++ b/li.strolch.agent/src/main/java/li/strolch/job/StrolchJob.java @@ -0,0 +1,71 @@ +package li.strolch.job; + +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledExecutorService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import li.strolch.agent.api.ComponentContainer; +import li.strolch.agent.api.StrolchAgent; +import li.strolch.persistence.api.StrolchTransaction; +import li.strolch.privilege.base.PrivilegeException; +import li.strolch.privilege.model.Certificate; +import li.strolch.runtime.StrolchConstants; +import li.strolch.runtime.privilege.PrivilegedRunnable; + +public abstract class StrolchJob implements Runnable { + + protected static final Logger logger = LoggerFactory.getLogger(StrolchJob.class); + + private StrolchAgent agent; + + public StrolchJob(StrolchAgent agent) { + this.agent = agent; + } + + /** + * @return the agent + */ + protected StrolchAgent getAgent() { + return this.agent; + } + + /** + * @return the {@link ComponentContainer} + */ + protected ComponentContainer getContainer() { + return getAgent().getContainer(); + } + + protected ScheduledExecutorService getScheduledExecutor() { + return getAgent().getScheduledExecutor(); + } + + /** + * Performs the given {@link PrivilegedRunnable} as the privileged system user + * {@link StrolchConstants#SYSTEM_USER_AGENT} + * + * @param runnable + * the runnable to perform + * + * @throws PrivilegeException + */ + protected void runAsAgent(PrivilegedRunnable runnable) throws PrivilegeException { + getContainer().getPrivilegeHandler().runAsAgent(runnable); + } + + /** + * Opens a {@link StrolchTransaction} for the default realm and certificate + * + * @param cert + * the certificate authorizing the transaction + * + * @return the newly created transaction + */ + protected StrolchTransaction openTx(Certificate cert) { + return getContainer().getRealm(cert).openTx(cert, this.getClass()); + } + + public abstract Future schedule(); +}