strolch/li.strolch.website/www.strolch.li/documentation-policies.html

179 lines
7.3 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="google-site-verification" content="CPhbjooaiTdROm7Vs4E7kuHZvBfkeLUtonGgcVUbTL8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="author" content="">
<link rel="shortcut icon" href="ico/favicon.ico">
<title>Strolch: Policies</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="css/custom.css" rel="stylesheet">
<!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries --><!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script><![endif]-->
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="index.html">Strolch</a>
</div>
<div class="collapse navbar-collapse">
<ul class="nav navbar-nav">
<li><a href="index.html">Overview</a></li>
<li><a href="api.html">API</a></li>
<li class="active"><a href="documentation.html">Documentation</a></li>
<li><a href="tutorial.html">Tutorial</a></li>
<li><a href="downloads.html">Downloads</a></li>
<li><a href="development.html">Development</a></li>
<li><a href="blog.html">Blog</a></li>
</ul>
</div>
<!--/.nav-collapse -->
</div>
</div>
<div class="container">
<div class="page-header">
<h1 class="page-title">Documentation: Policies</h1>
<p class="lead page-description">This page discusses the Strolch policies</p>
</div>
<div class="content">
<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 <code>StrolchPolicy</code>. 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>
<pre>
&lt;Resource ...&gt;
...
&lt;Policies&gt;
&lt;Policy Type="PlanningPolicy" Value="key:SimplePlanning" /&gt;
&lt;Policy Type="ExecutionPolicy" Value="java:li.strolch.policytest.TestSimulatedExecutionPolicy" /&gt;
&lt;/Policies&gt;
&lt;/Resource&gt;</pre>
<p><b>Note</b> how the PlanningPolicy has a value of <code>key:SimplePlanning</code> and the ExecutionPolicy
defines a reference to an actual class.</p>
<br />
<p>To use the PolicyHandler, it must be configured in the StrolchConfiguration.xml as follows:</p>
<pre>
&lt;Component>
&lt;name>PolicyHandler&lt;/name&gt;
&lt;api&gt;li.strolch.policy.PolicyHandler&lt;/api&gt;
&lt;impl&gt;li.strolch.policy.DefaultPolicyHandler&lt;/impl&gt;
&lt;Properties&gt;
&lt;readPolicyFile&gt;true&lt;/readPolicyFile&gt;
&lt;policyConfigFile&gt;StrolchPolicies.xml&lt;/policyConfigFile&gt;
&lt;/Properties&gt;
&lt;/Component&gt;</pre>
<br />
<p>And this policy handler implementation requires a file where the lookups for the policies is defined,
e.g.:</p>
<pre>
&lt;StrolchPolicies&gt;
&lt;PolicyType Type="PlanningPolicy" Api="li.strolch.policytest.TestPlanningPolicy"&gt;
&lt;Policy Key="SimplePlanning" Class="li.strolch.policytest.TestSimplePlanningPolicy" /&gt;
&lt;/PolicyType&gt;
&lt;PolicyType Type="ConfirmationPolicy" Api="li.strolch.policytest.TestConfirmationPolicy"&gt;
&lt;Policy Key="NoConfirmation" Class="li.strolch.policytest.TestNoConfirmationPolicy" /&gt;
&lt;/PolicyType&gt;
&lt;/StrolchPolicies&gt;</pre>
<br />
<p>Now at runtime we can access the policies:</p>
<pre>
PolicyHandler policyHandler = getComponent(PolicyHandler.class);
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(...);
}</pre>
<!-- content here -->
</div>
<!-- /.content -->
<div id="footer">
<div class="container">
<p class="text-muted">&copy; Strolch / <a href="mailto:eitch@eitchnet.ch">Robert von Burg</a> / Hosting by
<a href="http://www.eitchnet.ch">eitchnet.ch</a></p>
</div>
</div>
</div>
<!-- /.container -->
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual xsd as needed -->
<script src="js/bootstrap.min.js"></script>
<!-- Piwik -->
<script type="text/javascript">
var _paq = _paq || [];
_paq.push(['trackPageView']);
_paq.push(['enableLinkTracking']);
(function () {
var u = (("https:" == document.location.protocol) ? "https" : "http") + "://piwik.eitchnet.ch/";
_paq.push(['setTrackerUrl', u + 'piwik.php']);
_paq.push(['setSiteId', 2]);
var d = document, g = d.createElement('script'), s = d.getElementsByTagName('script')[0];
g.type = 'text/javascript';
g.defer = true;
g.async = true;
g.src = u + 'piwik.js';
s.parentNode.insertBefore(g, s);
})();
</script>
<noscript><p><img src="http://piwik.eitchnet.ch/piwik.php?idsite=2" style="border:0;" alt="" /></p></noscript>
<!-- End Piwik Code -->
</body>
</html>