[Minor] cleanup formatting

This commit is contained in:
Robert von Burg 2021-07-15 10:12:35 +02:00
parent 1d151822e0
commit a684877410
2 changed files with 63 additions and 53 deletions

View File

@ -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<StrolchRootElement> 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<StrolchRootElement> elements) {
logger.info(elements.size() + " resources were removed!");
}
@Override
public void update(String key, List<StrolchRootElement> elements) {
logger.info(elements.size() + " resources were updated!");
}
@Override
public void add(String key, List<StrolchRootElement> elements) {
logger.info(elements.size() + " resources were added!");
}
});
@Override
public void remove(String key, List<StrolchRootElement> elements) {
logger.info(elements.size() + " resources were removed!");
}
@Override
public void add(String key, List<StrolchRootElement> elements) {
logger.info(elements.size() + " resources were added!");
}
});
}
}
```

View File

@ -33,7 +33,7 @@ following shows defining two policies on a Resource, a PlanningPolicy, an
ExecutionPolicy in XML:
```xml
<Resource ...>
<Resource Id="myResource" Name="My Resource" Type="MyType">
...
<Policies>
<Policy Type="PlanningPolicy" Value="key:SimplePlanning" />
@ -42,62 +42,66 @@ ExecutionPolicy in XML:
</Resource>
```
{{% 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
<Component>
<name>PolicyHandler</name>
<api>li.strolch.policy.PolicyHandler</api>
<impl>li.strolch.policy.DefaultPolicyHandler</impl>
<Properties>
<readPolicyFile>true</readPolicyFile>
<policyConfigFile>StrolchPolicies.xml</policyConfigFile>
</Properties>
</Component>
<StrolchConfiguration>
<env id="dev">
<Component>
<name>PolicyHandler</name>
<api>li.strolch.policy.PolicyHandler</api>
<impl>li.strolch.policy.DefaultPolicyHandler</impl>
<Properties>
<readPolicyFile>true</readPolicyFile>
<policyConfigFile>StrolchPolicies.xml</policyConfigFile>
</Properties>
</Component>
</env>
</StrolchConfiguration>
```
And this policy handler implementation requires a file where the lookups for the
policies is defined, e.g.:
```xml
<StrolchPolicies>
<PolicyType Type="PlanningPolicy"
Api="li.strolch.policytest.TestPlanningPolicy">
<Policy Key="SimplePlanning"
Class="li.strolch.policytest.TestSimplePlanningPolicy"/>
<PolicyType Type="PlanningPolicy" Api="li.strolch.policytest.TestPlanningPolicy">
<Policy Key="SimplePlanning" Class="li.strolch.policytest.TestSimplePlanningPolicy"/>
</PolicyType>
<PolicyType Type="ConfirmationPolicy"
Api="li.strolch.policytest.TestConfirmationPolicy">
<Policy Key="NoConfirmation"
Class="li.strolch.policytest.TestNoConfirmationPolicy"/>
<PolicyType Type="ExecutionPolicy" Api="li.strolch.execution.policy.ExecutionPolicy">
<Policy Key="SimulatedExecution" Class="li.strolch.execution.policy.RandomDurationExecution"/>
</PolicyType>
<PolicyType Type="ConfirmationPolicy" Api="li.strolch.policytest.TestConfirmationPolicy">
<Policy Key="NoConfirmation" Class="li.strolch.policytest.TestNoConfirmationPolicy"/>
</PolicyType>
</StrolchPolicies>
```
Now at runtime we can access the policies:
```java
PolicyHandler policyHandler=getComponent(PolicyHandler.class);
public class MyService extends AbstractService<ServiceArgument, ServiceResult> {
@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();
}
}
```