You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by bu...@apache.org on 2012/12/12 10:17:17 UTC

svn commit: r841833 [9/28] - in /websites/staging/sling/trunk/content: ./ site/ site/46-line-blog.data/ site/authentication.data/ site/documentation.data/ site/first-steps.data/ site/getting-and-building-sling.data/ site/how-to-manage-events-in-sling.d...

Added: websites/staging/sling/trunk/content/site/embedding-sling.html
==============================================================================
--- websites/staging/sling/trunk/content/site/embedding-sling.html (added)
+++ websites/staging/sling/trunk/content/site/embedding-sling.html Wed Dec 12 09:16:44 2012
@@ -0,0 +1,340 @@
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<HTML>
+  <HEAD>
+    <TITLE>Apache Sling - Embedding Sling</TITLE>
+    <LINK rel="stylesheet" href="http://sling.apache.org/site/media.data/site.css" type="text/css" media="all">
+    <LINK rel="icon" href="http://sling.apache.org/site/media.data/favicon.ico">
+    <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
+  </HEAD>
+  <BODY>
+    <DIV class="title">
+      <DIV class="logo">
+        <A href="http://sling.apache.org/site/index.html">
+          <IMG border="0" alt="Apache Sling" src="http://sling.apache.org/site/media.data/logo.png">
+        </A>
+      </DIV>
+      <DIV class="header">
+        <A href="http://www.apache.org/">
+          <IMG border="0" alt="Apache" src="http://sling.apache.org/site/media.data/apache.png">
+        </A>
+      </DIV>
+    </DIV>
+    <DIV class="menu">
+<P><B>Documentation</B><BR class="atl-forced-newline">
+<A href="getting-started.html" title="Getting Started">Getting Started</A><BR class="atl-forced-newline">
+<A href="the-sling-engine.html" title="The Sling Engine">The Sling Engine</A><BR class="atl-forced-newline">
+<A href="development.html" title="Development">Development</A><BR class="atl-forced-newline">
+<A href="bundles.html" title="Bundles">Bundles</A><BR class="atl-forced-newline">
+<A href="tutorials-how-tos.html" title="Tutorials & How-Tos">Tutorials &amp; How&#45;Tos</A><BR class="atl-forced-newline">
+<A href="configuration.html" title="Configuration">Configuration</A><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/apidocs/sling6/index.html" class="external-link" rel="nofollow">API docs</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.wiki" class="external-link" rel="nofollow">Wiki</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.faq" class="external-link" rel="nofollow">FAQ</A><BR class="atl-forced-newline"></P>
+
+<P><B>Project info</B><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">Downloads</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/licenses/" class="external-link" rel="nofollow">License</A><BR class="atl-forced-newline">
+<A href="contributing.html" title="Contributing">Contributing</A><BR class="atl-forced-newline">
+<A href="news.html" title="News">News</A><BR class="atl-forced-newline">
+<A href="links.html" title="Links">Links</A><BR class="atl-forced-newline">
+<A href="project-information.html" title="Project Information">Project Information</A><BR class="atl-forced-newline">
+<A href="https://issues.apache.org/jira/browse/SLING" class="external-link" rel="nofollow">Issue Tracker</A><BR class="atl-forced-newline">
+<A href="http://svn.apache.org/viewvc/sling/trunk" class="external-link" rel="nofollow">Browse Source Repository</A><BR class="atl-forced-newline">
+<A href="security.html" title="Security">Security</A><BR class="atl-forced-newline"></P>
+
+<P><B>Sponsorship</B><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/sponsorship.html" class="external-link" rel="nofollow">Become a Sponsor</A><BR>
+<A href="http://www.apache.org/foundation/buy_stuff.html" class="external-link" rel="nofollow">Buy Stuff</A></P>
+
+
+  <IFRAME src="http://www.apache.org/ads/button.html" style="border-width:0; float: left" frameborder="0" scrolling="no" width="135" height="135"></IFRAME>
+  <P style="height: 135px"></P>
+    </DIV>
+    <DIV class="main">
+        <DIV class="breadcrump" style="font-size: 80%;">
+<A href="apache-sling.html" title="Apache Sling Website">Apache Sling Website</A>&nbsp;&gt;&nbsp;<A href="apache-sling.html" title="Apache Sling">Apache Sling</A>&nbsp;&gt;&nbsp;<A href="documentation.html" title="Documentation">Documentation</A>&nbsp;&gt;&nbsp;<A href="development.html" title="Development">Development</A>&nbsp;&gt;&nbsp;<A href="" title="Embedding Sling">Embedding Sling</A>
+        </DIV>
+<H1><A name="EmbeddingSling-EmbeddingSling"></A>Embedding Sling</H1>
+
+<P>The Sling Launchpad Launcher can be used to embed the OSGi Framework startup in your own Java application. This functionality is implemented in the <A href="http://svn.apache.org/repos/asf/sling/trunk/launchpad/base" class="external-link" rel="nofollow">Sling Launchpad Base project</A>. This project has the following features:</P>
+
+<UL>
+	<LI>Builds three artifacts:
+	<UL>
+		<LI>A standalone Java Application with the artifact qualifier <EM>app</EM>; e.g. <TT>org.apache.sling.launchpad.base-2.3.1-SNAPSHOT-app.jar</TT></LI>
+		<LI>A Web Application with the artifact qualifier <EM>webapp</EM>; e.g <TT>org.apache.sling.launchpad.base-2.3.1-SNAPSHOT-wepabb.war</TT></LI>
+		<LI>The primary artifact without an artifact qualifier; e.g. <TT>org.apache.sling.launchpad.base-2.3.1-SNAPSHOT.jar</TT></LI>
+	</UL>
+	</LI>
+	<LI>Embeds the OSGi Framework (Apache Felix) in the primary artifact</LI>
+	<LI>Encapsulates the OSGi Framework in its own <TT>URLClassLoader</TT></LI>
+	<LI>Supports Framework restart</LI>
+	<LI>Allows propagation of core setup functionality depending on the environment</LI>
+</UL>
+
+
+<P>This page is about the internal details of the Sling Launchpad Base module. To get an outside overview of the Sling Launchpad you might want to refer to <A href="the-sling-launchpad.html" title="The Sling Launchpad">The Sling Launchpad</A> page.</P>
+
+<H1><A name="EmbeddingSling-Structure"></A>Structure</H1>
+
+<P>The Launcher is based on three parts:</P>
+<OL>
+	<LI>The external part which is for example the standalone Java application's main class or the servlet deployed into the servlet container</LI>
+	<LI>The internal part which is the OSGi framework plus helper classes to control the framework and run initial installations</LI>
+	<LI>The bridging part, which contains API common to the external and internal part.</LI>
+</OL>
+
+
+<P>The external part uses the bridging part to create the class loader into which the internal part is loaded. The bidirectional communication between the external and internal part is implement based on two interfaces:</P>
+
+<UL>
+	<LI>The <TT>Launcher</TT> interface is implemented by a class in the internal part which is loaded through the bridge class loader. This interface allows setting, starting and stopping of the framework.</LI>
+	<LI>The <TT>Notifiable</TT> interface is implemented by a class in the external part which instance is handed to the <TT>Launcher</TT> instance. This interface allows the internal part to communicate back to the external part, most notably to indicate that the framework has been stopped from within or that the framework has been updated and must be restarted.</LI>
+</UL>
+
+
+
+<H1><A name="EmbeddingSling-TheBridgingPart"></A>The Bridging Part</H1>
+
+<P>The bridging part is provided in the <TT>org.apache.sling.launchpad.base.shared</TT> package:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> Class </TD>
+<TD class="confluenceTd"> Description </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> Launcher </TD>
+<TD class="confluenceTd"> The interface implemented by the internal class matching the external class being called to start/stop the framework. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> LauncherClassLoader </TD>
+<TD class="confluenceTd"> <TT>URLClassLoader</TT> implementing the class loader to load the internal part (along with the OSGi framework). This class loader only delegates to the parent class loader any packages not contained in the launchpad library (primary artifact of the Launchpad Base project). </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> Loader </TD>
+<TD class="confluenceTd"> Helper class to find the launchpad library and to create the <TT>LauncherClassLoader</TT> with that library. This class is also used to actually load the <TT>Launcher</TT> implementation to be called from the external launcher class. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> Notifiable </TD>
+<TD class="confluenceTd"> The interface implemented in the external part and handed over to the internal part. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SharedConstants </TD>
+<TD class="confluenceTd"> Constants naming various properties and classes. </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+
+<H1><A name="EmbeddingSling-TheInternalPart"></A>The Internal Part</H1>
+
+<P>The main class from the internal class directly used is <A href="http://svn.apache.org/repos/asf/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/Sling.java" class="external-link" rel="nofollow"><TT>Sling</TT></A> which instantiated to start the OSGi Framework. This class is responsible for setting up the environment to finally start the OSGi Framework:</P>
+
+<UL>
+	<LI>Read the <TT>sling.properties</TT> file</LI>
+	<LI>Ensure the presence of the JMX MBeanServer service</LI>
+	<LI>Execute the bootstrap installations, updates and uninstallations</LI>
+</UL>
+
+
+<P>The <A href="http://svn.apache.org/repos/asf/sling/trunk/launchpad/base/src/main/java/org/apache/sling/launchpad/base/impl/SlingFelix.java" class="external-link" rel="nofollow"><TT>SlingFelix</TT></A> class extends the Apache Felix <TT>Felix</TT> class which is the actual OSGi framework implementation. We extend the class to be able to notify the <TT>Notifiable</TT> implementation and update the OSGi framework from within the OSGi framework by updating the system bundle.</P>
+
+
+<H2><A name="EmbeddingSling-TheExternalPart"></A>The External Part</H2>
+
+<P>The external part is comprised of a main class started from the environment &ndash; main class of the Java applicaction or the servlet deployed in the servlet container &ndash; and a corresponding delegate class located inside of the launchpad base library. This delegate class is instantiated by the <TT>Loader</TT> loading from the <TT>LauncherClassLoader</TT>.</P>
+
+
+<H3><A name="EmbeddingSling-StandaloneJavaApplication"></A>Standalone Java Application</H3>
+
+<P>The standalone Java Application makes use of three classes:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> Class </TD>
+<TD class="confluenceTd"> Description </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> Main </TD>
+<TD class="confluenceTd"> This is the main class whose <TT>main</TT> method is called by the Java VM. This class is itself the <TT>Notifiable</TT> and finds the <TT>sling.home</TT> location from the environment (command line parameter, system property, or environment variable). </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> MainDelegate </TD>
+<TD class="confluenceTd"> This class is loaded by the <TT>Loader</TT> from the <TT>LauncherClassLoader</TT> to actually complete the initial setup before creating the <TT>Sling</TT> class to start the framework. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> ControlListener </TD>
+<TD class="confluenceTd"> This class is used by the <TT>Main</TT> class to open a server socket to be able to start and stop Sling as a server. This class allows for starting (opening the server socket), status check (connecting to the socket asking for status), and shutdown (connecting to the socket asking for shutdown). </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+<P>At the moment these classes are not directly suitable to be embedded in an existing application (or custom application launcher framework) unless that embedding prepares command line arguments in a <TT>String[]</TT> and calls the <TT>Main.main</TT> method. To allow for custom embeddings or extensions, the work distriubtions between the three classes should be refactored.</P>
+
+<H3><A name="EmbeddingSling-EmbeddingtheStandaloneJavaApplication"></A>Embedding the Standalone Java Application</H3>
+
+<DIV class="panelMacro"><TABLE class="infoMacro"><COLGROUP><COL width="24"><COL></COLGROUP><TR><TD valign="top"><IMG src="https://cwiki.apache.org/confluence/images/icons/emoticons/information.gif" width="16" height="16" align="absmiddle" alt="" border="0"></TD><TD>This work is being done as part of <A href="https://issues.apache.org/jira/browse/SLING-2225" class="external-link" rel="nofollow">SLING-2225</A> and will be officially available with the Sling Launchpad Base release 2.4.0. If you want to use the embedding before the release, you have to checkout the source from <A href="http://svn.apache.org/repos/asf/sling/trunk/launchpad/base" class="external-link" rel="nofollow">SVN</A> and build yourself.</TD></TR></TABLE></DIV>
+
+<P>To embedd the Sling Launcher in an application, the <TT>Main</TT> class is extended from. To manage the launcher, the following API is available:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TH class="confluenceTh"> Method </TH>
+<TH class="confluenceTh"> Description </TH>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>Main(Map&lt;String, String&gt; properties)</TT> </TD>
+<TD class="confluenceTd"> Instantiates the Main class with the given configuration properties. These are properties which are used directly as overwrites to the configurations in the <TT>sling.properties</TT> file. There is no more conversion applied. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>doControlCommand()</TT> </TD>
+<TD class="confluenceTd"> Before starting the application for the first time, this method can be called to handle any control command action. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>doStart()</TT> </TD>
+<TD class="confluenceTd"> Starts the Sling Application using the provided configuration properties as overwrites. Also these properties (or the <TT>sling.home</TT> system property or the <TT>SLING_HOME</TT> environment variable are analyzed to get the value for the <TT>sling.home</TT> setting. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>doStop()</TT> </TD>
+<TD class="confluenceTd"> Stops the application started by the <TT>doStart()</TT> method. </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+
+<H4><A name="EmbeddingSling-ExternalControloftheSlingApplication"></A>External Control of the Sling Application</H4>
+
+<P>By using control actions, the Sling Launcher may open or connect to a control port to communicate. The <TT>doControlAction()</TT> method together with the <TT>sling.control.action</TT> and <TT>sling.control.socket</TT> properties is able to setup this communication.</P>
+
+<P>The <TT>sling.control.socket</TT> is either a normal port number, in which case the connection is opened on the <TT>localhost</TT> interface (usually 127.0.0.1). Otheriwse, it may also be a value of the form <EM>host:port</EM> where <EM>host</EM> is the name or IP address of the interface to connect to and port is the port number. For security reasons it is suggested to not use an interface which is available remotely. So the default of <TT>localhost</TT> is usually the best choice.</P>
+
+<P>The <TT>sling.control.action</TT> takes either of three values:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> <TT>start</TT> </TD>
+<TD class="confluenceTd"> Starts a server socket as specified by the <TT>sling.control.socket</TT> property. If the socket cannot be bound to (because the port is in use) an error message is printed. Using the <TT>start</TT> action only makes sense when starting the application. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>stop</TT> </TD>
+<TD class="confluenceTd"> The <TT>stop</TT> action is used to stop a running application. For that a connection is opened to the server running on the socket specified by the <TT>sling.control.socket</TT> property. On this connection the server is instructed to shut down. After executing the <TT>stop</TT> action, the Java application should be terminated. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> <TT>status</TT> </TD>
+<TD class="confluenceTd"> The <TT>status</TT> action is used to check the status of a running application. For that a connection is opened to the server running on the socket specified by the <TT>sling.control.socket</TT> property. On this connection the server is queried on its status. After executing the <TT>stop</TT> action, the Java application should be terminated. </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+
+<H4><A name="EmbeddingSling-ConversionofCommandlineArgumentstoProperties"></A>Conversion of Commandline Arguments to Properties</H4>
+
+<P>When calling the Main class through the JVM startup the <TT>Main.main(String[] args)</TT> methods is called which reads the command line arguments and converts them into a <TT>Map&lt;String, String&gt;</TT> suitable for the constructore as follows:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TH class="confluenceTh"> Command Line Argument </TH>
+<TH class="confluenceTh"> Properties Entry </TH>
+</TR>
+<TR>
+<TD class="confluenceTd"> start </TD>
+<TD class="confluenceTd"> sling.control.action = &quot;start&quot; </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> status </TD>
+<TD class="confluenceTd"> sling.control.action = &quot;status&quot; </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> stop </TD>
+<TD class="confluenceTd"> sling.control.action = &quot;stop&quot; </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -c slinghome </TD>
+<TD class="confluenceTd"> sling.home = slinghome </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -l loglevel </TD>
+<TD class="confluenceTd"> org.apache.sling.commons.log.level = loglevel </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -f logfile </TD>
+<TD class="confluenceTd"> org.apache.sling.commons.log.file = logfile </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -a address </TD>
+<TD class="confluenceTd"> This command line argument is not supported yet and thus ignored </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -p port </TD>
+<TD class="confluenceTd"> org.osgi.service.http.port = port </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -j [ host &quot;:&quot; ] port </TD>
+<TD class="confluenceTd"> sling.control.socket = [ host &quot;:&quot; ] port </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> -h </TD>
+<TD class="confluenceTd"> This command line option is handled directly and not converted into the map </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+
+<H3><A name="EmbeddingSling-WebApplication"></A>Web Application</H3>
+
+<P>The web application makes use of 5 classes:</P>
+
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> Class </TD>
+<TD class="confluenceTd"> Description </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SlingServlet </TD>
+<TD class="confluenceTd"> This is the servlet registered in the <TT>web.xml</TT> descriptor and loaded by the servlet container into which Sling is deplyoed. This class locates the <TT>sling.home</TT> folder and loads the <TT>SlingServletDelagate</TT> to actually launch the framework. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SlingSessionListener </TD>
+<TD class="confluenceTd"> This &ndash; somewhat inappropriately named &ndash; class is registered as a listener by the Sling <TT>web.xml</TT> descriptor. It is called by the servlet container and forwards events to the <TT>SlingHttpSessionListenerDelegate</TT> which in turn forwards the events to the respective Servlet API listener services registered in the OSGi Framework. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SlingBridge </TD>
+<TD class="confluenceTd"> Simple extension of the <TT>Sling</TT> class which registers the system bundle's <TT>BundleContext</TT> as a servlet context attribute of the Sling web application. This allows Servlet Container bridging to properly work. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SlingHttpSessionListenerDelegate </TD>
+<TD class="confluenceTd"> This class is loaded by the <TT>LauncherClassLoader</TT> called from the <TT>SlingSessionListener</TT>. It is called by the <TT>SlingSessionListener</TT> to forward servlet container events to registered Servlet API listener services. </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> SlingServletDelegate </TD>
+<TD class="confluenceTd"> This class is loaded by the <TT>Loader</TT> from the <TT>LauncherClassLoader</TT> to actually complete the initial setup before creating the <TT>SlingBridge</TT> class to start the framework. </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+<P>At the moment these classes, particularly the <TT>SlingServlet</TT> class, are not particularly well suited to be extended by a servlet slightly modifying the launcher.</P>
+        <DIV class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
+Last modified by fmeschbe on 2011-09-24 09:12:01.0
+        </DIV>
+        <DIV class="trademarkFooter">
+Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
+        </DIV>
+    </DIV>
+  </BODY>
+</HTML>
+

Added: websites/staging/sling/trunk/content/site/errorhandling.html
==============================================================================
--- websites/staging/sling/trunk/content/site/errorhandling.html (added)
+++ websites/staging/sling/trunk/content/site/errorhandling.html Wed Dec 12 09:16:44 2012
@@ -0,0 +1,121 @@
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<HTML>
+  <HEAD>
+    <TITLE>Apache Sling - Errorhandling</TITLE>
+    <LINK rel="stylesheet" href="http://sling.apache.org/site/media.data/site.css" type="text/css" media="all">
+    <LINK rel="icon" href="http://sling.apache.org/site/media.data/favicon.ico">
+    <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
+  </HEAD>
+  <BODY>
+    <DIV class="title">
+      <DIV class="logo">
+        <A href="http://sling.apache.org/site/index.html">
+          <IMG border="0" alt="Apache Sling" src="http://sling.apache.org/site/media.data/logo.png">
+        </A>
+      </DIV>
+      <DIV class="header">
+        <A href="http://www.apache.org/">
+          <IMG border="0" alt="Apache" src="http://sling.apache.org/site/media.data/apache.png">
+        </A>
+      </DIV>
+    </DIV>
+    <DIV class="menu">
+<P><B>Documentation</B><BR class="atl-forced-newline">
+<A href="getting-started.html" title="Getting Started">Getting Started</A><BR class="atl-forced-newline">
+<A href="the-sling-engine.html" title="The Sling Engine">The Sling Engine</A><BR class="atl-forced-newline">
+<A href="development.html" title="Development">Development</A><BR class="atl-forced-newline">
+<A href="bundles.html" title="Bundles">Bundles</A><BR class="atl-forced-newline">
+<A href="tutorials-how-tos.html" title="Tutorials & How-Tos">Tutorials &amp; How&#45;Tos</A><BR class="atl-forced-newline">
+<A href="configuration.html" title="Configuration">Configuration</A><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/apidocs/sling6/index.html" class="external-link" rel="nofollow">API docs</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.wiki" class="external-link" rel="nofollow">Wiki</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.faq" class="external-link" rel="nofollow">FAQ</A><BR class="atl-forced-newline"></P>
+
+<P><B>Project info</B><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">Downloads</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/licenses/" class="external-link" rel="nofollow">License</A><BR class="atl-forced-newline">
+<A href="contributing.html" title="Contributing">Contributing</A><BR class="atl-forced-newline">
+<A href="news.html" title="News">News</A><BR class="atl-forced-newline">
+<A href="links.html" title="Links">Links</A><BR class="atl-forced-newline">
+<A href="project-information.html" title="Project Information">Project Information</A><BR class="atl-forced-newline">
+<A href="https://issues.apache.org/jira/browse/SLING" class="external-link" rel="nofollow">Issue Tracker</A><BR class="atl-forced-newline">
+<A href="http://svn.apache.org/viewvc/sling/trunk" class="external-link" rel="nofollow">Browse Source Repository</A><BR class="atl-forced-newline">
+<A href="security.html" title="Security">Security</A><BR class="atl-forced-newline"></P>
+
+<P><B>Sponsorship</B><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/sponsorship.html" class="external-link" rel="nofollow">Become a Sponsor</A><BR>
+<A href="http://www.apache.org/foundation/buy_stuff.html" class="external-link" rel="nofollow">Buy Stuff</A></P>
+
+
+  <IFRAME src="http://www.apache.org/ads/button.html" style="border-width:0; float: left" frameborder="0" scrolling="no" width="135" height="135"></IFRAME>
+  <P style="height: 135px"></P>
+    </DIV>
+    <DIV class="main">
+        <DIV class="breadcrump" style="font-size: 80%;">
+<A href="apache-sling.html" title="Apache Sling Website">Apache Sling Website</A>&nbsp;&gt;&nbsp;<A href="apache-sling.html" title="Apache Sling">Apache Sling</A>&nbsp;&gt;&nbsp;<A href="documentation.html" title="Documentation">Documentation</A>&nbsp;&gt;&nbsp;<A href="the-sling-engine.html" title="The Sling Engine">The Sling Engine</A>&nbsp;&gt;&nbsp;<A href="" title="Errorhandling">Errorhandling</A>
+        </DIV>
+<H1><A name="Errorhandling-HandlingErrors"></A>Handling Errors</H1>
+
+<P>The Sling Engine includes support for handling uncaught <TT>Throwable</TT> as well as rendering custom HTTP status code pages. This is implemented by expecting a (single) <TT>org.apache.sling.engine.servlets.ErrorHandler</TT> service to which handling of uncaught <TT>Throwable</TT> and HTTP status responses are delegated.</P>
+
+<P>The Sling Servlet Resolver bundle implements this interface by providing an elaborate mechanism to find the correct error handling script or servlet using the same algorithms as are used to select the scripts or servlets to handle regular requests.</P>
+
+<P>This page provides more information on how error handler scripts are selected and what is provided out of the box.</P>
+
+
+<H2><A name="Errorhandling-HTTPStatusCodes"></A>HTTP Status Codes</H2>
+
+<P>The Sling engine implements the <TT>HttpServletResponse.sendError</TT> methods by calling the <TT>ErrorHandler.handleError(int status, String message, SlingHttpServletRequest request, SlingHttpServletResponse response)</TT> method.</P>
+
+<P>The Servlet Resolver bundle implementation looks up a script to handle the status code as follows:</P>
+
+<UL>
+	<LI>The status code is converted to a string and used as the request extension. Any request extensions, selectors or suffixes from the actual request are ignored.</LI>
+	<LI>The same resource type hierarchy is followed to find the script as for regular script resolution. The difference is that for error handler scripts <TT>sling/servlet/errorhandler</TT> is used as the implied base resource type (as opposed to <TT>sling/servlet/default</TT> for regular script resolution.</LI>
+</UL>
+
+
+<P><B>Examples:</B></P>
+
+<UL>
+	<LI>An application provider my provide a default handler for the 404/NOT FOUND status. This script might be located in <TT>/libs/sling/servlet/errorhandler/404.jsp</TT>.</LI>
+	<LI>An programmer might provide a handler for the 403/FORBIDDEN status in <TT>/apps/sling/servlet/errorhandler/403.esp</TT>.</LI>
+</UL>
+
+
+
+<H2><A name="Errorhandling-UncaughtThrowables"></A>Uncaught Throwables</H2>
+
+<P>To handle uncaught Throwables the simple name (<TT>Class.getSimpleName()</TT>) of the <TT>Throwable</TT> class is used as request extension. Similarly to the Java try-catch clauses the class hierarchy is supported. That is to handle an uncaught <TT>FileNotFoundException</TT>, the names <TT>FileNotFoundException</TT>, <TT>IOException</TT>, <TT>Exception</TT>, <TT>Throwable</TT> are checked for a Servlet and the first one found is then used. Again, the Serlvet may be a Servlet registered as an OSGi service or may be a plain script stored in the JCR repository or provided through some custom Resource provider.</P>
+
+<P><B>Example:</B><BR>
+To register a catch-all handler for any uncaught Throwables you might create a script <TT>/apps/sling/servlet/errorhandler/Throwable.esp</TT>.</P>
+
+<P><B>Note:</B> If no script or servlet to handle an uncaught <TT>Throwable</TT> is registered, the default handler kicks in, which sends back a 500/INTERNAL SERVER ERROR response containing the <TT>Throwable</TT> and the stack trace. This response is <B>not</B> handled by the HTTP Status Code handling described above because the response status is sent using <TT>HttpServletResponse.setStatus(int, String)</TT>. To prevent this default response you have to implement a catch-all handler for the <TT>Throwable</TT> class as shown in the example.</P>
+
+<H2><A name="Errorhandling-DefaultHandler"></A>Default Handler</H2>
+
+<P>The Sling Servlet Resolver bundle provides a default error handler servlet which is used if the algorithms described above do not resolve to a handler script or servlet. The provided error handler servlet does the following:</P>
+
+<UL>
+	<LI>Print a descriptive message, which is the <TT>javax.servlet.error.message</TT> request attribute by default</LI>
+	<LI>Print a stacktrace if the <TT>javax.servlet.error.exception</TT> is set</LI>
+	<LI>Dump the request progress tracker</LI>
+</UL>
+
+
+<P>Starting with Sling Servlet Resolver version 2.0.10 the default error handler servlet is looked up using the string <TT>default</TT> as the request extension and the provided default servlet is registered as <TT>&lt;prefix&gt;/sling/servlet/errorhandler/default.servlet</TT> where &lt;prefix&gt; is the last entry in the resource resolver search path, <TT>/libs</TT> by default.</P>
+
+<P>Thus to overwrite the default error handler servlet provide a servlet or script for the <TT>default</TT> extension, for example <TT>/apps/sling/servlet/errorhandler/default.groovy</TT>.</P>
+        <DIV class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
+Last modified by fmeschbe on 2010-03-11 08:07:51.0
+        </DIV>
+        <DIV class="trademarkFooter">
+Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
+        </DIV>
+    </DIV>
+  </BODY>
+</HTML>
+

Added: websites/staging/sling/trunk/content/site/eventing-and-jobs.html
==============================================================================
--- websites/staging/sling/trunk/content/site/eventing-and-jobs.html (added)
+++ websites/staging/sling/trunk/content/site/eventing-and-jobs.html Wed Dec 12 09:16:44 2012
@@ -0,0 +1,382 @@
+
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<HTML>
+  <HEAD>
+    <TITLE>Apache Sling - Eventing and Jobs</TITLE>
+    <LINK rel="stylesheet" href="http://sling.apache.org/site/media.data/site.css" type="text/css" media="all">
+    <LINK rel="icon" href="http://sling.apache.org/site/media.data/favicon.ico">
+    <META http-equiv="Content-Type" content="text/html;charset=UTF-8">
+  </HEAD>
+  <BODY>
+    <DIV class="title">
+      <DIV class="logo">
+        <A href="http://sling.apache.org/site/index.html">
+          <IMG border="0" alt="Apache Sling" src="http://sling.apache.org/site/media.data/logo.png">
+        </A>
+      </DIV>
+      <DIV class="header">
+        <A href="http://www.apache.org/">
+          <IMG border="0" alt="Apache" src="http://sling.apache.org/site/media.data/apache.png">
+        </A>
+      </DIV>
+    </DIV>
+    <DIV class="menu">
+<P><B>Documentation</B><BR class="atl-forced-newline">
+<A href="getting-started.html" title="Getting Started">Getting Started</A><BR class="atl-forced-newline">
+<A href="the-sling-engine.html" title="The Sling Engine">The Sling Engine</A><BR class="atl-forced-newline">
+<A href="development.html" title="Development">Development</A><BR class="atl-forced-newline">
+<A href="bundles.html" title="Bundles">Bundles</A><BR class="atl-forced-newline">
+<A href="tutorials-how-tos.html" title="Tutorials & How-Tos">Tutorials &amp; How&#45;Tos</A><BR class="atl-forced-newline">
+<A href="configuration.html" title="Configuration">Configuration</A><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/apidocs/sling6/index.html" class="external-link" rel="nofollow">API docs</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.wiki" class="external-link" rel="nofollow">Wiki</A><BR class="atl-forced-newline">
+<A href="http://s.apache.org/sling.faq" class="external-link" rel="nofollow">FAQ</A><BR class="atl-forced-newline"></P>
+
+<P><B>Project info</B><BR class="atl-forced-newline">
+<A href="http://sling.apache.org/site/downloads.cgi" class="external-link" rel="nofollow">Downloads</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/licenses/" class="external-link" rel="nofollow">License</A><BR class="atl-forced-newline">
+<A href="contributing.html" title="Contributing">Contributing</A><BR class="atl-forced-newline">
+<A href="news.html" title="News">News</A><BR class="atl-forced-newline">
+<A href="links.html" title="Links">Links</A><BR class="atl-forced-newline">
+<A href="project-information.html" title="Project Information">Project Information</A><BR class="atl-forced-newline">
+<A href="https://issues.apache.org/jira/browse/SLING" class="external-link" rel="nofollow">Issue Tracker</A><BR class="atl-forced-newline">
+<A href="http://svn.apache.org/viewvc/sling/trunk" class="external-link" rel="nofollow">Browse Source Repository</A><BR class="atl-forced-newline">
+<A href="security.html" title="Security">Security</A><BR class="atl-forced-newline"></P>
+
+<P><B>Sponsorship</B><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/thanks.html" class="external-link" rel="nofollow">Thanks</A><BR class="atl-forced-newline">
+<A href="http://www.apache.org/foundation/sponsorship.html" class="external-link" rel="nofollow">Become a Sponsor</A><BR>
+<A href="http://www.apache.org/foundation/buy_stuff.html" class="external-link" rel="nofollow">Buy Stuff</A></P>
+
+
+  <IFRAME src="http://www.apache.org/ads/button.html" style="border-width:0; float: left" frameborder="0" scrolling="no" width="135" height="135"></IFRAME>
+  <P style="height: 135px"></P>
+    </DIV>
+    <DIV class="main">
+        <DIV class="breadcrump" style="font-size: 80%;">
+<A href="apache-sling.html" title="Apache Sling Website">Apache Sling Website</A>&nbsp;&gt;&nbsp;<A href="apache-sling.html" title="Apache Sling">Apache Sling</A>&nbsp;&gt;&nbsp;<A href="documentation.html" title="Documentation">Documentation</A>&nbsp;&gt;&nbsp;<A href="the-sling-engine.html" title="The Sling Engine">The Sling Engine</A>&nbsp;&gt;&nbsp;<A href="" title="Eventing and Jobs">Eventing and Jobs</A>
+        </DIV>
+<H2><A name="EventingandJobs-Eventing%2CJobsandScheduling"></A>Eventing, Jobs and Scheduling</H2>
+
+<P>Apache Sling provides some mechanisms and support for eventing, handling jobs and scheduling. </P>
+
+<P>To get some hands on code, you can refer to the following tutorials:</P>
+<UL>
+	<LI><A href="how-to-manage-events-in-sling.html" title="How to Manage Events in Sling">How to Manage Events in Sling</A></LI>
+	<LI><A href="scheduler-service-commons-scheduler.html" title="Scheduler Service (commons scheduler)">Scheduler Service &#40;commons scheduler&#41;</A></LI>
+</UL>
+
+
+<H3><A name="EventingandJobs-PossibleUseCasesforEventing"></A>Possible Use Cases for Eventing</H3>
+<UL>
+	<LI>Workflow</LI>
+	<LI>Post Processing (business processes)</LI>
+	<LI>Caching</LI>
+	<LI>Auditing</LI>
+</UL>
+
+
+<H3><A name="EventingandJobs-SourcesofEvents"></A>Sources of Events</H3>
+
+<P>There is a variety of sources from which events can be send:</P>
+<UL>
+	<LI>JCR observation events</LI>
+	<LI>Application generated events</LI>
+	<LI>Events from messaging systems (~JMS)</LI>
+	<LI>&quot;External events&quot;</LI>
+</UL>
+
+
+<P>The events can eiter be generated inside a current user context, e.g. when the user performs an action through the UI, or they can be out of a user context, e.g. for schedulded events. This leads to different weights of events.</P>
+
+<H3><A name="EventingandJobs-WeightsofEvents"></A>Weights of Events</H3>
+
+<P>We can distinguish two different weights of events, depending how they are distributed in a clustered environment:</P>
+
+<UL>
+	<LI>User generated events - these events are generated directly by some user action and are therefore started on one single node.</LI>
+	<LI>Environment generated events (JCR events, scheduler events etc.) - these events are generated &quot;simultaniously&quot; on all nodes.</LI>
+</UL>
+
+
+<P>External events, like incoming JMS events etc. might fall either into the first or the second category. The receiver of such events must have the knowledge about the weight of the event.</P>
+
+<H3><A name="EventingandJobs-BasicPrinciples"></A>Basic Principles</H3>
+
+<P>The foundation of the event mechanism is to distribute each event to every node in a clustered environment. The event distribution mechanism has no knowledge about the intent of the event and therefore is not able to make delivery decisions by itself. It is up to the sender to decide what should happen, however the sender must explicitly declare an event to be distributed. There are exceptions to &quot;distributing everything to everywhere&quot; as for example framework related events (bundle stopped, installed etc.) should not be distributed.</P>
+
+<P>The event mechanism will provide additional functionality making it easier for event receivers to decide if they should process an event. The event receiver can determine if the event is a local event or comming from a remote application node. Therefore a general rule of thumb is to process events only if they're local and just regard remote events as a FYI.</P>
+
+<P>The event mechanism is an <B>event</B> mechanism which should not be confused with a <B>messaging</B> mechanism. Events are received by the event mechanism and distributed to registered listeners. Concepts like durable listeners, guarantee of processing etc. are not part of the event mechanism itself. However, there will be additional support for such things, like job events.</P>
+
+<P>The application should try to use application events instead of low level JCR events whereever possible. Therefore a bridging between JCR events and the event mechanism is required. However, a general &quot;automatic&quot; mapping will not be provided. It is up to the application to develop such a mapping on a per use case base. There might be some support to make the mapping easier.</P>
+
+<P>The event handling should be made as transparent to the developer as possible. Therefore the additional code for a developer to make the eventing working in a clustered environment etc. should be kept to a minimum (which will hopefully reduce possible user errors).</P>
+
+<H2><A name="EventingandJobs-EventMechanism"></A>Event Mechanism</H2>
+
+<P>The event mechanism is leveraging the OSGi Event Admin Specification (OSGi Compendium 113). The event admin specification provides a sufficient base. It is based on the event publish and subscribe mechanism. Each event is associated with a topic and data. The data consists of custom key-value pairs where the keys are strings and the values can be any object. However, to work in distributed environments it is advisable to use only string and scalar types for data. If complex objects are required they have to be serializable.</P>
+
+<P>Events can either be send synchronously or asynchronously. It is up to the caller (the one sending the event) to decide this by choosing one of the provided methods.</P>
+
+<P>The OSGi API is very simple and leightweight - sending an event is just generating the event object and calling the event admin. Rceiving the event is implementing a single interface and declaring through properties which topics one is interested in. It's possible to add an additional filter (based on property values for example).</P>
+
+<P>%N The event handler should not take too much time to process the event. For example, the Apache Felix implementation of the event admin black lists an event handler if it takes more than 5 seconds to process the event - regardless if the event is sent synchronously or asynchronously. Therefore any heavier processing has to be done in the background. The event is just the trigger to start this. The job mechanism explained in this documentation is a good way of implementing this functionality for an event handler.</P>
+
+<P>The aim is to add all functionality on top of an existing event admin implementation. Therefore everything should be added by additional event handlers.</P>
+
+<H2><A name="EventingandJobs-EventHandler"></A>Event Handler</H2>
+
+<P>An event handler registers itself on a (set of) topic. It can also specify a filter for the events. This event handler is either notified synchronously or asynchronously depending on how the event has been sent.</P>
+
+<H2><A name="EventingandJobs-Events"></A>Events</H2>
+
+<P>The type of the event is specified by the hierarchically organized topic. In order to provide clustering of JCR repositories and clustering of the sling based application instances, each event can contain the following properties - if they are absent, a default value is assumed:</P>
+
+<UL>
+	<LI>=event.distribute= - this flag is set by the sender of an event to give a hint if the event should be distributed across instances. For example JCR observation based events are already distributed on all instances, so there is no further need to distribute them. If the flag is present, the event will be distributed. The value has currently no meaning, however the EventUtil method should be used to add this property. If the flag is absent the event is distributed locally only.</LI>
+	<LI>=event.application= - An identifier for the current application node in the cluster. This information will be used to detect if an event has been created on different nodes. If the event has been created on the same note, the =event.application= is missing, if it is a remote event, the =event.application= contains the ID of the node, the event has been initially created.</LI>
+</UL>
+
+
+<P>While the =event.distribute= must be set by the sender of an event (if the event should be distributed), the =event.application= property is maintained by the event mechanism. Therefore a client sending an event should <B>never</B> set this information by itself. This will confuse the local event handlers and result in unexpected behaviour. On remote events the =event.application= is set by the event distribution mechanism.</P>
+
+<H2><A name="EventingandJobs-EventDistributionAcrossApplicationNodes%28Cluster%29"></A>Event Distribution Across Application Nodes (Cluster)</H2>
+
+<P>The (local) event admin is the service distributing events locally. The Sling Distributing Event Handler is a registered event handler that is listening for events to be distributed. It distributes the events to remote application notes, the JCR repository is used for distribution. The distributing event handler writes the events into the repository, the distributing event handlers on other application nodes get notified through observation and then distribute the read events locally.</P>
+
+<P>As mentioned above, the client sending an event has to mark an event to be distributed in a cluster by setting the =event.distribute= in the event properties (through ~EventUtil).<BR>
+The existance of this flag allows to register an event handler for all events having this flag. The event handler will add the =event.application= information and write the event into the repository. All other application nodes have an observer registered and get notified each time a new event is added to the repository. They'll read the event from the repository, clear the =event.distribute= and send this event locally and asynchronously.</P>
+
+<P>An event handler receiving such an event can distinguish it by checking the =event.application= property. If the property is not available, it is a local event - if the property is available it is a remote event.</P>
+
+<P>This distribution mechanism has the advantage that the application nodes do not need to know each other and the distribution mechanism is independent from the used event admin implementation. Defining the filter for the =event.distribute= is also very simple.</P>
+
+<H3><A name="EventingandJobs-StoringEventsintheRepository"></A>Storing Events in the Repository</H3>
+
+<P>Distributable events are stored in the repository, the repository will have a specific area (path) where all events are stored. </P>
+
+<P>Each event is stored as a separate node with the following properties:</P>
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> <B>Property Name</B>     </TD>
+<TD class="confluenceTd"> <B>Description</B> </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:topic=       </TD>
+<TD class="confluenceTd"> The topic of the event </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:application= </TD>
+<TD class="confluenceTd"> The identifier of the application node where the event was created </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:created=     </TD>
+<TD class="confluenceTd"> The date and time when the event has been created (stored in the repository)</TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:properties=  </TD>
+<TD class="confluenceTd"> Serialized properties (except the =event.distribute=, but including the =event.application=) </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+<P>Each application is periodically removing old events from the repository (using the scheduler).</P>
+
+<H2><A name="EventingandJobs-Jobs%28GuaranteeofProcessing%29"></A>Jobs (Guarantee of Processing)</H2>
+
+<P>In general, the eventing mechanism has no knowledge about the contents of an event. Therefore, it can't decide if an event must be processed by a node. As the event mechanism is a &quot;fire event and forget about it&quot; algorithm, there is no way for an event admin to tell if someone has processed the event.</P>
+
+<P>On the other hand, there are use cases where the guarantee of processing is a must and usually this comes with the requirement of processing this event exactly once. Typical examples are sending notification emails (or sms) or post processing of content (like thumbnail generation of images or documents).</P>
+
+<P>We will call these events jobs to make clear that someone has to do something with the event (do the job). We will use a special topic =org/apache/sling/event/job= to indicate that the event contains a job, the real topic of the event is stored in the =event.job.topic= property. When a job event (event with the topic =org/apache/sling/event/job=) is received, a new event with the topic from the property =event.job.topic= is fired.</P>
+
+<P>The event must have the following properties:</P>
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> <B>Property Name</B>   </TD>
+<TD class="confluenceTd"> <B>Description</B> </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event.job.topic= </TD>
+<TD class="confluenceTd"> The topic of the job </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event.job.id=    </TD>
+<TD class="confluenceTd"> A unique identifier for this job (optional) </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+<P>The job event handler listens for all job events (all events with the topic =org/apache/sling/event/job=). The event handler will write the job event into the repository (into the job area), lock it, create a new event with the topic from the property =event.job.topic= and send the job event through the event admin. When the job is finished, the event listener will unlock the node from the repository.</P>
+
+<P>To avoid timeouts and black listing of event handlers, the job event handler does not assume that the job has been processed if the event could be sent successfully. It is the task of the event handler to notify the job event handler that it has processed the job. In addition, the job processing should be done in the background. The =EventUtil= class has a helper method for this: =processJob(Event, JobProcessor)=. The event handler must implement the =JobProcessor= interface which consists of a single =process(Event)= method. When the event handler receives a job event, it calls =EventUtil.processJob(event, this)= and returns. The =process(Event)= method is now called in the background and when it finishes, the job event handler is notified that the job is completed.</P>
+
+<P>If the event handler wants to do the background processing by itself or does not need background processing at all, it must signal completition of the job by call =EventUtil.finishedJob(event)=.</P>
+
+<P>By default an application node is queuing the jobs which means that only one job is processed at a time. If a job can be run in parallel on one application node, the property =event.job.parallel= should be set with any value.</P>
+
+<P>The job id is optional and can be used to update or reactivate jobs.</P>
+
+<H3><A name="EventingandJobs-StoringJobsintheRepository"></A>Storing Jobs in the Repository</H3>
+
+<P>Jobs are stored in the repository in order to ensure that exactly one single application node is processing the job. The repository will have a specific area (path) where all jobs are stored. In order to distinguish a job which occured twice and a job which is generated at the same time on several nodes, each job can be uniquely identified by its topic (property =event.job.topic=) and the =event.job.id= property. It is up to the client who is creating the event to ensure that the =event.job.id= property is unqiue <B>and</B> identical on all application nodes. If the job id is not provided for the job, then it is up to the client to ensure that the job even is only fired once.</P>
+
+<P>When the job event listener tries to write a job into the repository it will check if the repository already contains a job with the given topic =event.job.topic= and =event.job.id= property. If the event has already been written by some other application node, it's not written again. If the event has been written by the same node, it will be set to active again (=event:active= will be set to true and =event:created= will be updated).</P>
+
+<P>Each job is stored as a separate node with the following properties:</P>
+<DIV class="table-wrap">
+<TABLE class="confluenceTable"><TBODY>
+<TR>
+<TD class="confluenceTd"> <B>Property Name</B>     </TD>
+<TD class="confluenceTd"> <B>Description</B> </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:topic=       </TD>
+<TD class="confluenceTd"> The topic of the job </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:application= </TD>
+<TD class="confluenceTd"> The identifier of the node where the job was created </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:processor=   </TD>
+<TD class="confluenceTd"> The identifier of the node which processed the job </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:active=      </TD>
+<TD class="confluenceTd"> Indicates if this job is active and should be processed(unlocked) or is currently processed (locked) </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:created=     </TD>
+<TD class="confluenceTd"> The date and time when the event has been created (stored in the repository)</TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:id=          </TD>
+<TD class="confluenceTd"> The unique identifier of this job (optional).</TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:properties=  </TD>
+<TD class="confluenceTd"> Serialized properties </TD>
+</TR>
+<TR>
+<TD class="confluenceTd"> =event:finished=    </TD>
+<TD class="confluenceTd"> The date and time when the job has been finished </TD>
+</TR>
+</TBODY></TABLE>
+</DIV>
+
+
+
+<P>The failover of an application node is accomplished by locking and the =event:active= flag. If a job is locked in the repository a session scoped lock is used. If this application node dies, the lock dies as well. Each application node observes the JCR locking properties and therefore gets aware of unlocked event nodes with the active flag set to true. If an application node finds such a node, it locks it, updates the =event:application= information and processes it accordingly. In this case the event gets the additional property =org/apache/sling/job/retry=. </P>
+
+<P>Each application is periodically removing old jobs from the repository (using the scheduler).</P>
+
+<H3><A name="EventingandJobs-DistributionofJobs"></A>Distribution of Jobs</H3>
+
+<P>A job event is an event like any other. Therefore it is up to the client generating the event to decide if the event should be distributed. If the event is distributed, it will be distributed with a set =event.application= on the remote nodes. If the job event handler receives a job with the =event.application= property set, it will not try to write it into the repository. It will just broadcast this event asynchronously as a ~FYI event.</P>
+
+<P>If a job event is created simultanously on all application nodes, the event will not be distributed. The application node that actually has the lock on the stored job in the repository will clear the =event.application= when sending the event locally. All other application nodes will use the =event.application= stored in the repository when broadcasting the event locally.</P>
+
+<H2><A name="EventingandJobs-UsagePatterns"></A>Usage Patterns</H2>
+
+<P>Based on some usage patterns, we discuss the functionality of the eventing mechanism.</P>
+
+<H3><A name="EventingandJobs-SendingUserGeneratedEvents"></A>Sending User Generated Events</H3>
+
+<P>If a user action results in an event, the event is only created on one single node in the cluster. The event object is generated and delivered to the OSGi event admin. If the =event.distribute= is not explicitly set, the event is only distributed localled.</P>
+
+<P>If the =event.distribute= is the, the cluster event handler will write the event into the repository. All nodes in the cluster observe the repository area where all events are stored. If a new event is written into that area, each application node will get notified. It will create the event based on the information in the repository, clear the =event.distribute= and publish the event.</P>
+
+<P>The flow can be described as follows:</P>
+<OL>
+	<LI>Client code generates event using OSGi API, if the =event.distribute= should be set, it is using the ~EventUtil.</LI>
+	<LI>Client code sends the event to the (local) event admin.</LI>
+	<LI>Event admin delivers the event locally.</LI>
+	<LI>Clustering event handler receives the event if =event.distribute= is present
+	<OL>
+		<LI>Event handler adds =event.application= and writes the event to the repository</LI>
+		<LI>Remote repository observers get notified through JCR observation about the new event. They distribute the event locally with the =event.application= (from the node where the event occured first) and cleared =event.distribute=.</LI>
+	</OL>
+	</LI>
+</OL>
+
+
+<H3><A name="EventingandJobs-ProcessingJCREvents"></A>Processing JCR Events</H3>
+
+<P>JCR events are environment generated events and therefore are sent by the repository to each node in the cluster. In general, it is advisable to not built the application on the low level repository events but to use application events. Therefore the observer of the JCR event should create an OSGi event based on the changes in the repository. A decision has to be made if the event should be a job or a plain event.</P>
+
+<P>The flow can be described as follows:</P>
+<OL>
+	<LI>Client registers for JCR observation</LI>
+	<LI>JCR notifies the client for changes</LI>
+	<LI>Client generates OSGi event based on the JCR events (the =event.distribute= will not be set), it decides if it sends this event as a job.</LI>
+	<LI>Client code sends the event to the (local) event admin</LI>
+	<LI>Event admin publishes the event locally</LI>
+	<LI>The distribution event handler does not set see the event as the =event.distribute= is not set.</LI>
+	<LI>The job event handler gets the event if it has the job topic
+	<OL>
+		<LI>The job event handler adds the =event.application= property and tries to write the job to the repository
+		<OL>
+			<LI>If no job with the topic and =id= property is in the repository, the event will be written and locked.</LI>
+			<LI>If an event with the topic and =id= property is in the repository then:
+			<OL>
+				<LI>If the =event.application= equals the current application node, the event is set to active (=event:active=) in the repository again and locked</LI>
+				<LI>If the =event.application= does not equal the current application node, the event is not distributed locally.</LI>
+			</OL>
+			</LI>
+			<LI>If the job could be locked in the repository, the job event handler delivers the job locally and synchronously and it unlocks the job and sets =event:active= to false afterwards.</LI>
+		</OL>
+		</LI>
+	</OL>
+	</LI>
+</OL>
+
+
+<H3><A name="EventingandJobs-SendingScheduledEvents"></A>Sending Scheduled Events</H3>
+
+<P>Scheduled events are OSGi events that have been created by the environemnt. They are generated on each application node of the cluster through an own scheduler instance. Sending these events works the same as sending events based on JCR events (see above).</P>
+
+<P>In most use cases a scheduler will send job events to ensure that exactly one application node is processing the event.</P>
+
+<H3><A name="EventingandJobs-ReceivingOSGiEvents"></A>Receiving OSGi Events</H3>
+
+<P>If you want to receive OSGi events, you can just follow the specification: receive it via a custom event handler which is registered on bundle start - a filter can be specified as a configuration property of the handler. </P>
+
+<P>As we follow the principle of distributing each event to every registered handler, the handler has to decide if it will process the event. In order to avoid multiple processing of this event in a clustered environment, the event handler should check the =event.application= property. If it is not set, it's a local event and the handler should process the event. If the =event.application= is set, it's a remote event and the handler should not process the event. This is a general rule of thumb - however, it's up to the handler to make its decision either on =event.application= or any other information.</P>
+
+<P>It is advisable to perform the local event check even in a non clustered environment as it makes the migration to a cluster later on much easier and there is nearly no performance overhead caused by the check.</P>
+
+<P>The ~EventUtil class provides an utility method =isLocalEvent(Event)= which checks the existance of the =event.application= property and returns =true= if it is absend.</P>
+
+<H2><A name="EventingandJobs-Scheduler"></A>Scheduler</H2>
+
+<P>Each Sling based application will contain a scheduler service (which is based on the Quartz open source project).</P>
+
+<H2><A name="EventingandJobs-UseCases"></A>Use Cases</H2>
+
+<H3><A name="EventingandJobs-PostProcessing%28BusinessProcesses%29"></A>Post Processing (Business Processes)</H3>
+
+<P>A typical example for post processing (or running a business process) is sending an email or creating thumbnails and extracting meta data from the content (like we do in DAM), which we will discuss here.</P>
+
+<P>An appropriate JCR observer will be registered. This observer detects when new content is put into the repository or when content is changed. In these cases it creates appropriate =CONTENT_ADDED=, =CONTENT_UPDATED= OSGi events from the JCR events. In order to ensure that these actions get processed accordingly, the event is send as a job (with the special job topic, the =topic= and =id= property).</P>
+
+<P>The event admin now delivers these jobs to the registered handlers. The job event handler gets notified and (simplified version) sends the contained event synchronously. One of the handlers for these events is the post processing service in DAM. The job mechanism ensures that exactly one application node is post processing and that the process has to be finished even if the application node dies during execution.</P>
+
+<H2><A name="EventingandJobs-Scheduling"></A>Scheduling</H2>
+
+<P>The scheduler is a service which uses the open source Quartz library. The scheduler has methods to start jobs periodically or with a cron definition. In addition, a service either implementing =java.lang.Runnable= or =org.quartz.job= is started through the whiteboard pattern <B>if</B> it either contains a configuration property =scheduler.expression= or =scheduler.period=. The job is started with the ~PID of the service - if the service has no PID, the configuration property =scheduler.name= must be set.</P>
+        <DIV class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
+Last modified by jck on 2010-08-19 06:04:37.0
+        </DIV>
+        <DIV class="trademarkFooter">
+Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project logo are trademarks of The Apache Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their respective owners.
+        </DIV>
+    </DIV>
+  </BODY>
+</HTML>
+