From a684877410cf96fee8e65e91681dc44976d350ef Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Thu, 15 Jul 2021 10:12:35 +0200 Subject: [PATCH] [Minor] cleanup formatting --- content/documentation/observers.md | 36 ++++++++------ content/documentation/policies.md | 80 ++++++++++++++++-------------- 2 files changed, 63 insertions(+), 53 deletions(-) diff --git a/content/documentation/observers.md b/content/documentation/observers.md index 2a9f682..73d95e9 100644 --- a/content/documentation/observers.md +++ b/content/documentation/observers.md @@ -40,23 +40,29 @@ Registering for updates is done by registering an Observer on the ObserverHandler of the realm itself: ```java -ObserverHandler observerHandler = container.getRealm(StrolchConstants.DEFAULT_REALM).getObserverHandler(); -observerHandler.registerObserver(Tags.RESOURCE, new Observer() { +public class Example { + public static void main(String[] args) { - @Override - public void update(String key, List elements) { - logger.info(elements.size() + " resources were updated!"); - } + ObserverHandler observerHandler = container + .getRealm(StrolchConstants.DEFAULT_REALM).getObserverHandler(); + observerHandler.registerObserver(Tags.RESOURCE, new Observer() { - @Override - public void remove(String key, List elements) { - logger.info(elements.size() + " resources were removed!"); - } + @Override + public void update(String key, List elements) { + logger.info(elements.size() + " resources were updated!"); + } - @Override - public void add(String key, List elements) { - logger.info(elements.size() + " resources were added!"); - } -}); + @Override + public void remove(String key, List elements) { + logger.info(elements.size() + " resources were removed!"); + } + + @Override + public void add(String key, List elements) { + logger.info(elements.size() + " resources were added!"); + } + }); + } +} ``` diff --git a/content/documentation/policies.md b/content/documentation/policies.md index 1c007ab..cb01a29 100644 --- a/content/documentation/policies.md +++ b/content/documentation/policies.md @@ -33,7 +33,7 @@ following shows defining two policies on a Resource, a PlanningPolicy, an ExecutionPolicy in XML: ```xml - + ... @@ -42,62 +42,66 @@ ExecutionPolicy in XML: ``` -{{% notice tip %}} Note how the PlanningPolicy has a value of key:SimplePlanning -and the ExecutionPolicy defines a reference to an actual class. {{% /notice %}} +{{% notice tip %}} Note how the `PlanningPolicy` has a value of `key:SimplePlanning` +and the `ExecutionPolicy` defines a reference to an actual class. {{% /notice %}} -To use the PolicyHandler, it must be configured in the StrolchConfiguration.xml +To use the `PolicyHandler`, it must be configured in the `StrolchConfiguration.xml` as follows: ```xml - - - PolicyHandler - li.strolch.policy.PolicyHandler - li.strolch.policy.DefaultPolicyHandler - - true - StrolchPolicies.xml - - + + + + PolicyHandler + li.strolch.policy.PolicyHandler + li.strolch.policy.DefaultPolicyHandler + + true + StrolchPolicies.xml + + + + ``` And this policy handler implementation requires a file where the lookups for the policies is defined, e.g.: ```xml - - - - + + - - - + + + + + - ``` Now at runtime we can access the policies: ```java -PolicyHandler policyHandler=getComponent(PolicyHandler.class); +public class MyService extends AbstractService { + @Override + protected ServiceResult internalDoService(ServiceArgument arg) throws Exception { + try (StrolchTransaction tx = openArgOrUserTx(arg)) { + Resource res = tx.getResourceBy("MyType", "myTestResource"); -try(StrolchTransaction tx=openTx()){ - Resource res=tx.getResourceBy("TestType","MyTestResource"); - PolicyDefs policyDefs=res.getPolicyDefs(); - - PolicyDef planningPolicyDef=policyDefs.getPolicyDef("PlanningPolicy"); - PlanningPolicy planningPolicy=policyHandler.getPolicy(planningPolicyDef,tx); - planningPolicy.plan(...); - - PolicyDef executionPolicyDef=res.getPolicyDefs().getPolicyDef("ExecutionPolicy"); - ExecutionPolicy executionPolicy=policyHandler.getPolicy(executionPolicyDef,tx); - executionPolicy.execute(...); + PolicyDef planningPolicyDef = res.getPolicyDef(PlanningPolicy.class); + PlanningPolicy planningPolicy = tx.getPolicy(planningPolicyDef); + planningPolicy.plan(...); + + PolicyDef executionPolicyDef = res.getPolicyDef(ExecutionPolicy.class); + ExecutionPolicy executionPolicy = tx.getPolicy(executionPolicyDef); + executionPolicy.toExecution(...); + + tx.commitOnClose(); + } + + return ServiceResult.success(); + } } ```