2023-03-16 15:31:32 +01:00
<!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 = "Robert von Burg" > < link rel = icon href = /favicon.ico type = image/ico > < title > Policies - Strolch< / title > < link href = /css/nucleus.css?1678977092 rel = stylesheet > < link href = /css/fontawesome-all.min.css?1678977092 rel = stylesheet > < link href = /css/hybrid.css?1678977092 rel = stylesheet > < link href = /css/featherlight.min.css?1678977092 rel = stylesheet > < link href = /css/perfect-scrollbar.min.css?1678977092 rel = stylesheet > < link href = /css/auto-complete.css?1678977092 rel = stylesheet > < link href = /css/atom-one-dark-reasonable.css?1678977092 rel = stylesheet > < link href = /css/theme.css?1678977092 rel = stylesheet > < link href = /css/hugo-theme.css?1678977092 rel = stylesheet > < link href = /css/theme-green.css?1678977092 rel = stylesheet > < script src = /js/jquery-3.3.1.min.js?1678977092 > < / 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?1678977092 > < / script > < script type = text/javascript src = /js/auto-complete.js?1678977092 > < / script > < script type = text/javascript > var baseurl = "https:\/\/strolch.li\/" ; < / script > < script type = text/javascript src = /js/search.js?1678977092 > < / 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 = /documentation/ title = Documentation class = "dd-item
2022-11-11 08:35:56 +01:00
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 > < ul > < li data-nav-id = /development/prerequisites/ title = Prerequisites class = dd-item > < a href = /development/prerequisites/ > Prerequisites< / a > < / li > < li data-nav-id = /development/building/ title = "Building Strolch" class = dd-item > < a href = /development/building/ > Building Strolch< / a > < / li > < li data-nav-id = /development/maven-archetypes/ title = "Maven Archetypes" class = dd-item > < a href = /development/maven-archetypes/ > Maven Archetypes< / a > < / li > < li data-nav-id = /development/web-app/ title = "Web App" class = dd-item > < a href = /development/web-app/ > Web App< / a > < / li > < li data-nav-id = /development/main-class-app/ title = "Main Class App" class = dd-item > < a href = /development/main-class-app/ > Main Class App< / a > < / li > < li data-nav-id = /development/converting-existing/ title = "Converting Existing App" class = dd-item > < a href = /development/converting-existing/ > Converting Existing App< / a > < / li > < / ul > < / li > < li data-nav-id = /blog/ title = Blog class = dd-item > < a href = /blog/ > Blog< / a > < ul > < li data-nav-id = /blog/post-00018/ title = "Strolch PLC 1.2.3 released" class = dd-item > < a href = /blog/post-00018/ > Strolch PLC 1.2.3 released< / a > < / li > < li data-nav-id = /blog/post-0001
2021-07-11 20:37:40 +02:00
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
2021-07-15 10:13:07 +02:00
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 > < Resource< / span > < span style = color:#a6e22e > Id=< / span > < span style = color:#e6db74 > " myResource" < / span > < span style = color:#a6e22e > Name=< / span > < span style = color:#e6db74 > " My Resource" < / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " MyType" < / span > < span style = color:#f92672 > > < / span >
2021-07-11 20:37:40 +02:00
...
< span style = color:#f92672 > < Policies> < / span >
< span style = color:#f92672 > < Policy< / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " PlanningPolicy" < / span > < span style = color:#a6e22e > Value=< / span > < span style = color:#e6db74 > " key:SimplePlanning" < / span > < span style = color:#f92672 > /> < / span >
< span style = color:#f92672 > < Policy< / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " ExecutionPolicy" < / span > < span style = color:#a6e22e > Value=< / span > < span style = color:#e6db74 > " java:li.strolch.policytest.TestSimulatedExecutionPolicy" < / span > < span style = color:#f92672 > /> < / span >
< span style = color:#f92672 > < /Policies> < / span >
< span style = color:#f92672 > < /Resource> < / span >
2021-07-15 10:13:07 +02:00
< / code > < / pre > < / div > < div class = "notices tip" > < p > Note how the < code > PlanningPolicy< / code > has a value of < code > key:SimplePlanning< / code >
and the < code > ExecutionPolicy< / code > defines a reference to an actual class.< / p > < / div > < p > To use the < code > PolicyHandler< / code > , it must be configured in the < code > StrolchConfiguration.xml< / code >
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 > < StrolchConfiguration> < / span >
< span style = color:#f92672 > < env< / span > < span style = color:#a6e22e > id=< / span > < span style = color:#e6db74 > " dev" < / span > < span style = color:#f92672 > > < / span >
< span style = color:#f92672 > < Component> < / span >
< span style = color:#f92672 > < name> < / span > PolicyHandler< span style = color:#f92672 > < /name> < / span >
< span style = color:#f92672 > < api> < / span > li.strolch.policy.PolicyHandler< span style = color:#f92672 > < /api> < / span >
< span style = color:#f92672 > < impl> < / span > li.strolch.policy.DefaultPolicyHandler< span style = color:#f92672 > < /impl> < / span >
< span style = color:#f92672 > < Properties> < / span >
< span style = color:#f92672 > < readPolicyFile> < / span > true< span style = color:#f92672 > < /readPolicyFile> < / span >
< span style = color:#f92672 > < policyConfigFile> < / span > StrolchPolicies.xml< span style = color:#f92672 > < /policyConfigFile> < / span >
< span style = color:#f92672 > < /Properties> < / span >
< span style = color:#f92672 > < /Component> < / span >
< span style = color:#f92672 > < /env> < / span >
< span style = color:#f92672 > < /StrolchConfiguration> < / span >
2021-07-11 20:37:40 +02:00
< / code > < / pre > < / div > < p > And this policy handler implementation requires a file where the lookups for the
2021-07-15 10:13:07 +02:00
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 > < StrolchPolicies> < / span >
< span style = color:#f92672 > < PolicyType< / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " PlanningPolicy" < / span > < span style = color:#a6e22e > Api=< / span > < span style = color:#e6db74 > " li.strolch.policytest.TestPlanningPolicy" < / span > < span style = color:#f92672 > > < / span >
< span style = color:#f92672 > < Policy< / span > < span style = color:#a6e22e > Key=< / span > < span style = color:#e6db74 > " SimplePlanning" < / span > < span style = color:#a6e22e > Class=< / span > < span style = color:#e6db74 > " li.strolch.policytest.TestSimplePlanningPolicy" < / span > < span style = color:#f92672 > /> < / span >
2021-07-11 20:37:40 +02:00
< span style = color:#f92672 > < /PolicyType> < / span >
2021-07-15 10:13:07 +02:00
< span style = color:#f92672 > < PolicyType< / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " ExecutionPolicy" < / span > < span style = color:#a6e22e > Api=< / span > < span style = color:#e6db74 > " li.strolch.execution.policy.ExecutionPolicy" < / span > < span style = color:#f92672 > > < / span >
< span style = color:#f92672 > < Policy< / span > < span style = color:#a6e22e > Key=< / span > < span style = color:#e6db74 > " SimulatedExecution" < / span > < span style = color:#a6e22e > Class=< / span > < span style = color:#e6db74 > " li.strolch.execution.policy.RandomDurationExecution" < / span > < span style = color:#f92672 > /> < / span >
< span style = color:#f92672 > < /PolicyType> < / span >
< span style = color:#f92672 > < PolicyType< / span > < span style = color:#a6e22e > Type=< / span > < span style = color:#e6db74 > " ConfirmationPolicy" < / span > < span style = color:#a6e22e > Api=< / span > < span style = color:#e6db74 > " li.strolch.policytest.TestConfirmationPolicy" < / span > < span style = color:#f92672 > > < / span >
< span style = color:#f92672 > < Policy< / span > < span style = color:#a6e22e > Key=< / span > < span style = color:#e6db74 > " NoConfirmation" < / span > < span style = color:#a6e22e > Class=< / span > < span style = color:#e6db74 > " li.strolch.policytest.TestNoConfirmationPolicy" < / span > < span style = color:#f92672 > /> < / span >
2021-07-11 20:37:40 +02:00
< span style = color:#f92672 > < /PolicyType> < / span >
< span style = color:#f92672 > < /StrolchPolicies> < / span >
2021-07-15 10:13:07 +02:00
< / 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 > < span style = color:#66d9ef > public< / span > < span style = color:#66d9ef > class< / span > < span style = color:#a6e22e > MyService< / span > < span style = color:#66d9ef > extends< / span > AbstractService< span style = color:#f92672 > < < / span > ServiceArgument< span style = color:#f92672 > ,< / span > ServiceResult< span style = color:#f92672 > > < / span > < span style = color:#f92672 > {< / span >
< span style = color:#a6e22e > @Override< / span >
< span style = color:#66d9ef > protected< / span > ServiceResult < span style = color:#a6e22e > internalDoService< / span > < span style = color:#f92672 > (< / span > ServiceArgument arg< span style = color:#f92672 > )< / span > < span style = color:#66d9ef > throws< / span > Exception < span style = color:#f92672 > {< / span >
< span style = color:#66d9ef > try< / span > < span style = color:#f92672 > (< / span > StrolchTransaction tx < span style = color:#f92672 > =< / span > openArgOrUserTx< span style = color:#f92672 > (< / span > arg< span style = color:#f92672 > ))< / span > < 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 > " MyType" < / span > < span style = color:#f92672 > ,< / span > < span style = color:#e6db74 > " myTestResource" < / span > < span style = color:#f92672 > );< / span >
2021-07-19 12:14:10 +02:00
PlanningPolicy planningPolicy < span style = color:#f92672 > =< / span > tx< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > getPolicy< / span > < span style = color:#f92672 > (< / span > res< span style = color:#f92672 > ,< / span > PlanningPolicy< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > class< / span > < span style = color:#f92672 > );< / span >
2021-07-15 10:13:07 +02:00
planningPolicy< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > plan< / span > < span style = color:#f92672 > (...);< / span >
2021-07-19 12:14:10 +02:00
ExecutionPolicy executionPolicy < span style = color:#f92672 > =< / span > tx< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > getPolicy< / span > < span style = color:#f92672 > (< / span > res< span style = color:#f92672 > ,< / span > ExecutionPolicy< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > class< / span > < span style = color:#f92672 > );< / span >
2021-07-15 10:13:07 +02:00
executionPolicy< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > toExecution< / span > < span style = color:#f92672 > (...);< / span >
tx< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > commitOnClose< / span > < span style = color:#f92672 > ();< / span >
< span style = color:#f92672 > }< / span >
2021-07-11 20:37:40 +02:00
2021-07-15 10:13:07 +02:00
< span style = color:#66d9ef > return< / span > ServiceResult< span style = color:#f92672 > .< / span > < span style = color:#a6e22e > success< / span > < span style = color:#f92672 > ();< / span >
< span style = color:#f92672 > }< / span >
2021-07-11 20:37:40 +02:00
< span style = color:#f92672 > }< / span >
2023-03-16 15:31:32 +01:00
< / 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?1678977092 > < / script > < script src = /js/perfect-scrollbar.min.js?1678977092 > < / script > < script src = /js/perfect-scrollbar.jquery.min.js?1678977092 > < / script > < script src = /js/jquery.sticky.js?1678977092 > < / script > < script src = /js/featherlight.min.js?1678977092 > < / script > < script src = /js/highlight.pack.js?1678977092 > < / script > < script > hljs . initHighlightingOnLoad ( ) ; < / script > < script src = /js/modernizr.custom-3.6.0.js?1678977092 > < / script > < script src = /js/learn.js?1678977092 > < / script > < script src = /js/hugo-learn.js?1678977092 > < / script > < script src = /mermaid/mermaid.js?1678977092 > < / script > < script > mermaid . initialize ( { startOnLoad : true } ) ; < / script > < script type = text/javascript > var _paq = window . _paq = window . _paq || [ ] ; _paq . push ( [ "setDocumentTitle" , document . domain + "/" + document . title ] ) ; _paq . push ( [ "setCookieDomain" , "*.strolch.li" ] ) ; _paq . push ( [ 'trackPageView' ] ) ; _paq . push ( [ 'enableLinkTracking' ] ) ; ( function ( ) { var u = "https://piwik.eitchnet.ch/" ; _paq . push ( [ 'setTrackerUrl' , u + 'matomo.php' ] ) ; _paq . push ( [ 'setSiteId' , '2' ] ) ; var d = document , g = d . createElement ( 'script' ) , s = d . getElementsByTagName ( 'script' ) [ 0 ] ; g . type = 'text/javascript' ; g . async = true ; g . src = u + 'matomo.js' ; s . parentNode . insertBefore ( g , s ) ; } ) ( ) ; < / script > < / body > < / html >