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

209 lines
7.9 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: Components</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: Components</h1>
<p class="lead page-description">This page discusses Strolch Components</p>
</div>
<div class="content">
<p>A Strolch agent can be easily extended with arbitrary components. An agent is basically a container for
classes extending <code>StrolchComponent</code>. Theses classes mostly implement an interface which describes
the operations that are supported by the component.</p>
<p>The following represents a list of the most used components:</p>
<ul>
<li><code>RealmHandler</code>: li.strolch.agent.impl.DefaultRealmHandler</li>
<li><code>PrivilegeHandler</code>: li.strolch.runtime.privilege.DefaultStrolchPrivilegeHandler</li>
<li><code>EnumHandler</code>: li.strolch.runtime.query.enums.DefaultEnumHandler</li>
<li><code>PolicyHandler</code>: li.strolch.policy.DefaultPolicyHandler</li>
<li><code>ServiceHandler</code>: li.strolch.service.api.DefaultServiceHandler</li>
<li><code>StrolchSessionHandler</code>: li.strolch.rest.DefaultStrolchSessionHandler</li>
<li><code>PersistenceHandler</code>: multiple implementations</li>
<li><code>PostInitializer</code>: project specific implementation</li>
<li><code>MailHandler</code>: li.strolch.handler.mail.SmtpMailHandler</li>
</ul>
<p>A component has a life-cycle, which is governed by the Agent's own life-cycle. The life-cycle is as
follows:</p>
<pre>
setup -> initialize -> start <-> stop -> destroy</pre>
<p>The setup step is used to instantiate the component, the initialize step is used to validate configuration
parameters, and the run step is used to start the component, i.e. start threads, etc. The stop step stops
these threads and also allows the component to be started again. The destroy step destroys the instance and
makes it unusable anymore, i.e. shutdown of the agent.</p>
<p>Each component has its own configuration parameters. A component is registered in the <code>StrolchConfiguration.xml</code>
file with a</p>
<ul>
<li>name</li>
<li>api class name</li>
<li>implementation class name</li>
<li>configuration parameters</li>
<li>any required dependencies</li>
</ul>
<p>The dependencies is an important feature as the dependencies of a component are always started before the
actual component.</p>
<p>By example of the <code>MailHandler</code> we shall show how a strolch component would be implemented.</p>
<p>First define an interface:</p>
<pre>
public interface MailHandler {
public void sendMail(String subject, String text, String recipient);
}
</pre>
<p>Then implement a concrete <code>MailHandler</code>:</p>
<pre>
public class SmtpMailHandler extends StrolchComponent implements MailHandler {
// instance fields with configuration properties to send the mail
public SmtpMailHandler(ComponentContainer container, String componentName) {
super(container, componentName);
}
@Override
public void initialize(ComponentConfiguration configuration) throws Exception {
// store any properties needed from the configuration
super.initialize(configuration);
}
@Override
public void sendMail(String subject, String text, String recipient) {
// send the e-mail using SMTP, or store in stack to send by thread
}
}
</pre>
<p>Now that the component is written, it must be registered on the component, so that it is loaded when the
agent is started. For this the <code>StrolchConfiguration.xml</code> file must be modified to include a
component element:</p>
<pre>
&lt;StrolchConfiguration&gt;
&lt;env id="dev"&gt;
...
&lt;Component&gt;
&lt;name&gt;MailHandler&lt;/name&gt;
&lt;api&gt;li.strolch.handler.mail.MailHandler&lt;/api&gt;
&lt;impl&gt;li.strolch.handler.mail.SmtpMailHandler&lt;/impl&gt;
&lt;Properties&gt;
&lt;username&gt;test&lt;/username&gt;
&lt;password&gt;test&lt;/password&gt;
&lt;hostName&gt;localhost&lt;/hostName&gt;
...
&lt;/Properties&gt;
&lt;/Component&gt;
...
&lt;/env&gt;
&lt;/StrolchConfiguration&gt;
</pre>
<p>Now when the agent is started, the component can be retrieved and used. E.g from inside a
<code>Service</code>:</p>
<pre>
MailHandler mailHandler = getComponent(MailHandler.class);
mailHandler.sendMail("My Subject", "Hello World", "test@test.ch");
</pre>
</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>