strolch-website/docs/documentation/policies/index.html

68 lines
16 KiB
HTML

<!doctype html><html lang=en class="js csstransforms3d"><head><meta charset=utf-8><meta name=viewport content="width=device-width,initial-scale=1"><meta name=generator content="Hugo 0.80.0"><meta name=description content="Strolch is a parameterized framework for use on servers and IoT"><meta name=author content="Strolch"><link rel=icon href=/favicon.ico type=image/ico><title>Policies - Strolch</title><link href=/css/nucleus.css?1626091328 rel=stylesheet><link href=/css/fontawesome-all.min.css?1626091328 rel=stylesheet><link href=/css/hybrid.css?1626091328 rel=stylesheet><link href=/css/featherlight.min.css?1626091328 rel=stylesheet><link href=/css/perfect-scrollbar.min.css?1626091328 rel=stylesheet><link href=/css/auto-complete.css?1626091328 rel=stylesheet><link href=/css/atom-one-dark-reasonable.css?1626091328 rel=stylesheet><link href=/css/theme.css?1626091328 rel=stylesheet><link href=/css/hugo-theme.css?1626091328 rel=stylesheet><link href=/css/theme-green.css?1626091328 rel=stylesheet><script src=/js/jquery-3.3.1.min.js?1626091328></script><style>:root #header+#content>#left>#rlblock_left{display:none!important}</style></head><body data-url=/documentation/policies/><nav id=sidebar><div id=header-wrapper><div id=header><a id=logo href=/><img src=/logo.png></a></div><div class=searchbox><label for=search-by><i class="fas fa-search"></i></label><input data-search-input id=search-by type=search placeholder=Search...>
<span data-search-clear><i class="fas fa-times"></i></span></div><script type=text/javascript src=/js/lunr.min.js?1626091328></script><script type=text/javascript src=/js/auto-complete.js?1626091328></script><script type=text/javascript>var baseurl="https:\/\/strolch.li\/";</script><script type=text/javascript src=/js/search.js?1626091328></script></div><section id=homelinks><ul><li><a class=padding href=/><i class="fas fa-home"></i>Home</a></li></ul></section><div class=highlightable><ul class=topics><li data-nav-id=/api/ title=API class=dd-item><a href=/api/>API</a></li><li data-nav-id=/history/ title=History class=dd-item><a href=/history/>History</a></li><li data-nav-id=/documentation/ title=Documentation class="dd-item
parent"><a href=/documentation/>Documentation</a><ul><li data-nav-id=/documentation/architecture/ title=Architecture class=dd-item><a href=/documentation/architecture/>Architecture</a></li><li data-nav-id=/documentation/model/ title=Model class=dd-item><a href=/documentation/model/>Model</a></li><li data-nav-id=/documentation/do-and-donts/ title="Do and Don't" class=dd-item><a href=/documentation/do-and-donts/>Do and Don't</a></li><li data-nav-id=/documentation/runtime-configuration/ title="Runtime Configuration" class=dd-item><a href=/documentation/runtime-configuration/>Runtime Configuration</a></li><li data-nav-id=/documentation/realms/ title=Realms class=dd-item><a href=/documentation/realms/>Realms</a></li><li data-nav-id=/documentation/components/ title=Components class=dd-item><a href=/documentation/components/>Components</a></li><li data-nav-id=/documentation/services-and-commands/ title="Services and Commands" class=dd-item><a href=/documentation/services-and-commands/>Services and Commands</a></li><li data-nav-id=/documentation/searches/ title=Searches class=dd-item><a href=/documentation/searches/>Searches</a></li><li data-nav-id=/documentation/queries/ title=Queries class=dd-item><a href=/documentation/queries/>Queries</a></li><li data-nav-id=/documentation/transactions/ title=Transactions class=dd-item><a href=/documentation/transactions/>Transactions</a></li><li data-nav-id=/documentation/policies/ title=Policies class="dd-item active"><a href=/documentation/policies/>Policies</a></li><li data-nav-id=/documentation/observers/ title=Observers class=dd-item><a href=/documentation/observers/>Observers</a></li><li data-nav-id=/documentation/versioning/ title=Versioning class=dd-item><a href=/documentation/versioning/>Versioning</a></li><li data-nav-id=/documentation/reports/ title=Reports class=dd-item><a href=/documentation/reports/>Reports</a></li><li data-nav-id=/documentation/priviles/ title=Privileges class=dd-item><a href=/documentation/priviles/>Privileges</a></li></ul></li><li data-nav-id=/plc/ title=PLC class=dd-item><a href=/plc/>PLC</a><ul><li data-nav-id=/plc/architecture/ title=Architecture class=dd-item><a href=/plc/architecture/>Architecture</a></li><li data-nav-id=/plc/example-set-up/ title="Example Set-Up" class=dd-item><a href=/plc/example-set-up/>Example Set-Up</a></li></ul></li><li data-nav-id=/tutorial/ title=Tutorial class=dd-item><a href=/tutorial/>Tutorial</a><ul><li data-nav-id=/tutorial/configuration/ title=Configuration class=dd-item><a href=/tutorial/configuration/>Configuration</a></li><li data-nav-id=/tutorial/model/ title=Model class=dd-item><a href=/tutorial/model/>Model</a></li><li data-nav-id=/tutorial/crud-book/ title="CRUD Book" class=dd-item><a href=/tutorial/crud-book/>CRUD Book</a></li></ul></li><li data-nav-id=/download/ title=Download class=dd-item><a href=/download/>Download</a></li><li data-nav-id=/development/ title=Development class=dd-item><a href=/development/>Development</a></li></ul><section id=shortcuts><h3>More</h3><ul><li><a class=padding href=https://strolch.li/tags><i class="fas fa-tags"></i>Tags</a></li><li><a class=padding href=https://github.com/strolch-li><i class="fab fa-github"></i>GitHub project</a></li></ul></section><section id=footer><p>Built with <a href=https://github.com/matcornic/hugo-theme-learn><i class="fas fa-heart"></i></a>from <a href=https://getgrav.org>Grav</a> and <a href=https://gohugo.io/>Hugo</a></p></section></div></nav><section id=body><div id=overlay></div><div class="padding highlightable"><div><div id=top-bar><div id=top-github-link><a class=github-link title="Edit this page" href=https://github.com/Pi4J/pi4j.github.io/tree/main/contentdocumentation/policies.md target=blank><i class="fas fa-code-branch"></i><span id=top-github-link-text>Edit this page</span></a></div><div id=breadcrumbs itemscope itemtype=http://data-vocabulary.org/Breadcrumb><span id=sidebar-toggle-span><a href=# id=sidebar-toggle data-sidebar-toggle><i class="fas fa-bars"></i></a></span><span id=toc-menu><i class="fas fa-list-alt"></i></span><span class=links><a href=/>Strolch Overview</a> > <a href=/documentation/>Documentation</a> > Policies</span></div><div class=progress><div class=wrapper><nav id=TableOfContents><ul><li><a href=#policies>Policies</a></li></ul></nav></div></div></div></div><div id=head-tags></div><div id=body-inner><h1>Policies</h1><h2 id=policies>Policies</h2><p>Policies are an integral part when writing business logic in Strolch. In many
cases it would suffice to write all such logic in <code>Services</code> and <code>Commands</code>, but
as soon as behaviour can change, depending on the element being accessed, then
this would quickly lead to many if/else blocks.</p><p>Since writing large if/else blocks is not maintanable in the long run, Strolch
offers a different approach. All Strolch elements can store Policy definitions.
This is a simple key/value store where the key defines the type of policy, and
the value references the policy to use.</p><p>Currently there are two ways to reference a policy in Strolch, either via a key
which defines a further lookup in the <code>PolicyHandler</code>, or directly as the name
of the class to instantiate.</p><p>Using policies in Strolch gives the additional possibility of easily changing
the behaviour at runtime, as a Service and/or Command would delegate the
behaviour to the currently configured policy on the releveant element.</p><p>Policies are implemented by defining an abstract class and extends
StrolchPolicy. This abstract class then defines the API of the actual policy. A
concrete class then extends this abstract class and implements the concrete
methods.</p><p>Policies are registered on Resources, Orders, Activities and Actions. The
following shows defining two policies on a Resource, a PlanningPolicy, an
ExecutionPolicy in XML:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-xml data-lang=xml><span style=color:#f92672>&lt;Resource</span> <span style=color:#960050;background-color:#1e0010>...</span><span style=color:#f92672>&gt;</span>
...
<span style=color:#f92672>&lt;Policies&gt;</span>
<span style=color:#f92672>&lt;Policy</span> <span style=color:#a6e22e>Type=</span><span style=color:#e6db74>&#34;PlanningPolicy&#34;</span> <span style=color:#a6e22e>Value=</span><span style=color:#e6db74>&#34;key:SimplePlanning&#34;</span> <span style=color:#f92672>/&gt;</span>
<span style=color:#f92672>&lt;Policy</span> <span style=color:#a6e22e>Type=</span><span style=color:#e6db74>&#34;ExecutionPolicy&#34;</span> <span style=color:#a6e22e>Value=</span><span style=color:#e6db74>&#34;java:li.strolch.policytest.TestSimulatedExecutionPolicy&#34;</span> <span style=color:#f92672>/&gt;</span>
<span style=color:#f92672>&lt;/Policies&gt;</span>
<span style=color:#f92672>&lt;/Resource&gt;</span>
</code></pre></div><div class="notices tip"><p>Note how the PlanningPolicy has a value of key:SimplePlanning
and the ExecutionPolicy defines a reference to an actual class.</p></div><p>To use the PolicyHandler, it must be configured in the StrolchConfiguration.xml
as follows:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-xml data-lang=xml>
<span style=color:#f92672>&lt;Component&gt;</span>
<span style=color:#f92672>&lt;name&gt;</span>PolicyHandler<span style=color:#f92672>&lt;/name&gt;</span>
<span style=color:#f92672>&lt;api&gt;</span>li.strolch.policy.PolicyHandler<span style=color:#f92672>&lt;/api&gt;</span>
<span style=color:#f92672>&lt;impl&gt;</span>li.strolch.policy.DefaultPolicyHandler<span style=color:#f92672>&lt;/impl&gt;</span>
<span style=color:#f92672>&lt;Properties&gt;</span>
<span style=color:#f92672>&lt;readPolicyFile&gt;</span>true<span style=color:#f92672>&lt;/readPolicyFile&gt;</span>
<span style=color:#f92672>&lt;policyConfigFile&gt;</span>StrolchPolicies.xml<span style=color:#f92672>&lt;/policyConfigFile&gt;</span>
<span style=color:#f92672>&lt;/Properties&gt;</span>
<span style=color:#f92672>&lt;/Component&gt;</span>
</code></pre></div><p>And this policy handler implementation requires a file where the lookups for the
policies is defined, e.g.:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-xml data-lang=xml>
<span style=color:#f92672>&lt;StrolchPolicies&gt;</span>
<span style=color:#f92672>&lt;PolicyType</span> <span style=color:#a6e22e>Type=</span><span style=color:#e6db74>&#34;PlanningPolicy&#34;</span>
<span style=color:#a6e22e>Api=</span><span style=color:#e6db74>&#34;li.strolch.policytest.TestPlanningPolicy&#34;</span><span style=color:#f92672>&gt;</span>
<span style=color:#f92672>&lt;Policy</span> <span style=color:#a6e22e>Key=</span><span style=color:#e6db74>&#34;SimplePlanning&#34;</span>
<span style=color:#a6e22e>Class=</span><span style=color:#e6db74>&#34;li.strolch.policytest.TestSimplePlanningPolicy&#34;</span><span style=color:#f92672>/&gt;</span>
<span style=color:#f92672>&lt;/PolicyType&gt;</span>
<span style=color:#f92672>&lt;PolicyType</span> <span style=color:#a6e22e>Type=</span><span style=color:#e6db74>&#34;ConfirmationPolicy&#34;</span>
<span style=color:#a6e22e>Api=</span><span style=color:#e6db74>&#34;li.strolch.policytest.TestConfirmationPolicy&#34;</span><span style=color:#f92672>&gt;</span>
<span style=color:#f92672>&lt;Policy</span> <span style=color:#a6e22e>Key=</span><span style=color:#e6db74>&#34;NoConfirmation&#34;</span>
<span style=color:#a6e22e>Class=</span><span style=color:#e6db74>&#34;li.strolch.policytest.TestNoConfirmationPolicy&#34;</span><span style=color:#f92672>/&gt;</span>
<span style=color:#f92672>&lt;/PolicyType&gt;</span>
<span style=color:#f92672>&lt;/StrolchPolicies&gt;</span>
</code></pre></div><p>Now at runtime we can access the policies:</p><div class=highlight><pre style=color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4><code class=language-java data-lang=java>PolicyHandler policyHandler<span style=color:#f92672>=</span>getComponent<span style=color:#f92672>(</span>PolicyHandler<span style=color:#f92672>.</span><span style=color:#a6e22e>class</span><span style=color:#f92672>);</span>
<span style=color:#66d9ef>try</span><span style=color:#f92672>(</span>StrolchTransaction tx<span style=color:#f92672>=</span>openTx<span style=color:#f92672>()){</span>
Resource res<span style=color:#f92672>=</span>tx<span style=color:#f92672>.</span><span style=color:#a6e22e>getResourceBy</span><span style=color:#f92672>(</span><span style=color:#e6db74>&#34;TestType&#34;</span><span style=color:#f92672>,</span><span style=color:#e6db74>&#34;MyTestResource&#34;</span><span style=color:#f92672>);</span>
PolicyDefs policyDefs<span style=color:#f92672>=</span>res<span style=color:#f92672>.</span><span style=color:#a6e22e>getPolicyDefs</span><span style=color:#f92672>();</span>
PolicyDef planningPolicyDef<span style=color:#f92672>=</span>policyDefs<span style=color:#f92672>.</span><span style=color:#a6e22e>getPolicyDef</span><span style=color:#f92672>(</span><span style=color:#e6db74>&#34;PlanningPolicy&#34;</span><span style=color:#f92672>);</span>
PlanningPolicy planningPolicy<span style=color:#f92672>=</span>policyHandler<span style=color:#f92672>.</span><span style=color:#a6e22e>getPolicy</span><span style=color:#f92672>(</span>planningPolicyDef<span style=color:#f92672>,</span>tx<span style=color:#f92672>);</span>
planningPolicy<span style=color:#f92672>.</span><span style=color:#a6e22e>plan</span><span style=color:#f92672>(...);</span>
PolicyDef executionPolicyDef<span style=color:#f92672>=</span>res<span style=color:#f92672>.</span><span style=color:#a6e22e>getPolicyDefs</span><span style=color:#f92672>().</span><span style=color:#a6e22e>getPolicyDef</span><span style=color:#f92672>(</span><span style=color:#e6db74>&#34;ExecutionPolicy&#34;</span><span style=color:#f92672>);</span>
ExecutionPolicy executionPolicy<span style=color:#f92672>=</span>policyHandler<span style=color:#f92672>.</span><span style=color:#a6e22e>getPolicy</span><span style=color:#f92672>(</span>executionPolicyDef<span style=color:#f92672>,</span>tx<span style=color:#f92672>);</span>
executionPolicy<span style=color:#f92672>.</span><span style=color:#a6e22e>execute</span><span style=color:#f92672>(...);</span>
<span style=color:#f92672>}</span>
</code></pre></div><footer class=footline></footer></div></div><div id=navigation><a class="nav nav-prev" href=/documentation/transactions/ title=Transactions><i class="fa fa-chevron-left"></i></a><a class="nav nav-next" href=/documentation/observers/ title=Observers style=margin-right:0><i class="fa fa-chevron-right"></i></a></div></section><div style=left:-1000px;overflow:scroll;position:absolute;top:-1000px;border:none;box-sizing:content-box;height:200px;margin:0;padding:0;width:200px><div style=border:none;box-sizing:content-box;height:200px;margin:0;padding:0;width:200px></div></div><script src=/js/clipboard.min.js?1626091328></script><script src=/js/perfect-scrollbar.min.js?1626091328></script><script src=/js/perfect-scrollbar.jquery.min.js?1626091328></script><script src=/js/jquery.sticky.js?1626091328></script><script src=/js/featherlight.min.js?1626091328></script><script src=/js/highlight.pack.js?1626091328></script><script>hljs.initHighlightingOnLoad();</script><script src=/js/modernizr.custom-3.6.0.js?1626091328></script><script src=/js/learn.js?1626091328></script><script src=/js/hugo-learn.js?1626091328></script><script src=/mermaid/mermaid.js?1626091328></script><script>mermaid.initialize({startOnLoad:true});</script></body></html>