incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Managing events
Date Mon, 16 Aug 2010 12:05:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/9/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true" type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Managing+events">Managing events</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~jck">Jean-Christophe KAUTZMANN</a>
    </h4>
        <br/>
                         <h4>Changes (135)</h4>
                                 
    
<div id="page-diffs">
            <table class="diff" cellpadding="0" cellspacing="0">
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Sling distinguishes between 2 types of events: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> standard events: no garantee of processing <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> job events: garantee of processing. Someone has to do something with the event (do the job). <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The event mechanism is leveraging the OSGi Event Admin Specification (OSGi Compendium 113). <br>The OSGi API is very simple and leightweight - sending an event is just generating the event object and calling the event admin. Receiving the event is implementing a single interface and declaring through properties which topics one is interested in. For more details please refer to the following javadocs: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> package org.osgi.service.event of the OSGI API (http://www.osgi.org/javadoc/r4v42/org/osgi/service/event/package-summary.html). <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> package org.apache.sling.event of the Sling API (http://sling.apache.org/apidocs/sling5/org/apache/sling/event/package-summary.html) <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-unchanged" >You can learn more in the &quot;Eventing, Jobs and Scheduling&quot; section (http://sling.apache.org/site/eventing-and-jobs.html). <br></td></tr>
            <tr><td class="diff-unchanged" > <br>To get started with the Sling eventing API, you will implement here a service that listens to files posted to /tmp/dropbox and moves them to the appropriate locations depending on the mime-type: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> images (.png) are moved to /dropbox/images/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> music (.mp3) are moved to /dropbox/music/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> movies (.avi) are moved to /dropbox/movies/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> otherwise the files are moved to /dropbox/other/ <br></td></tr>
            <tr><td class="diff-unchanged" > <br>To do that, you will implement 2 services. The first one, called OsgiDropBoxService: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> listens to osgi event &quot;resource added&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> sends a job event <br></td></tr>
            <tr><td class="diff-unchanged" >The second one, called DropBoxEventHandler: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> listens to job event &quot;file added to /tmp/dropbox&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> moves the file according to its extension <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Listening to osgi event &quot;resource added&quot; <br> <br>To listen to the specific osgi event &quot;resource added&quot;: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The service needs to implement the interface org.osgi.service.event.EventHandler and its handleEvent(Event event) method. <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The property &quot;event.topics&quot; needs to be set to &quot;org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED&quot; in the class annotations. <br></td></tr>
            <tr><td class="diff-unchanged" >To get a list of possible events available in Sling (resource added, removed or changed), refer to the javadocs for org.apache.sling.api.SlingConstants. <br> <br>To send a job event: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The service needs to implement the interface org.apache.sling.event.JobProcessor and its process(org.osgi.service.event.Event job) method. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The first part of the code looks as follows: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">/**</span> <span class="diff-added-words"style="background-color: #dfd;">{code}</span> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">/*\* <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> The &lt;code&gt;OsgiDropBoxService&lt;/code&gt; is listening content added to /tmp/dropbox by using OSGI events <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> * @scr.component  immediate=&quot;true&quot; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;\* @scr.component&amp;nbsp; immediate=&quot;true&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> @scr.service interface=&quot;org.osgi.service.event.EventHandler&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> @scr.property name=&quot;event.topics&quot; valueRef=&quot;org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;</span>*/</span> <br></td></tr>
            <tr><td class="diff-unchanged" >public class OsgiDropBoxService implements JobProcessor, EventHandler { <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">{code} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The Event Admin service is needed to send the job event: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	/**  <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;/*\* <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;</span> * The OSGI event admin used for sending events <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> * @scr.reference <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	 */ <br>	private EventAdmin eventAdmin; <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp; \*/ <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;private EventAdmin eventAdmin; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp; <br></td></tr>
            <tr><td class="diff-unchanged" >The job topic for dropbox job events needs to be defined: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">    /** The job topic for dropbox job events. */ <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp; /*\* The job topic for dropbox job events. \*/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> public static final String JOB_TOPIC = &quot;com/sling/eventing/dropbox/job&quot;; <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The org.osgi.service.event.EventHandler#handleEvent(Event event) method needs to be implemented: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbs</span>p<span class="diff-added-chars"style="background-color: #dfd;">;&amp;nbsp; &amp;nbsp;p</span>ublic</span> void handleEvent(Event event) { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> if (EventUtil.isLocal(event)) { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> EventUtil.processJob(event, this); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	    } <br>	} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The org.apache.sling.event.JobProcessor#process(Event event) method needs to be implemented. <br>The logic is as follows: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> the OSGI event is analyzed <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> if the event is a file that has been added to &quot;/tmp/dropbox&quot;: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;\-</span> an event is created with 2 properties: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;\-</span> one to flag the event as a job event <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		- the file path <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;\- the file path <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;\-</span> the job event is sent to all the listeners that subscribe to the topic of the event. <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbs</span>p<span class="diff-added-chars"style="background-color: #dfd;">;&amp;nbsp; &amp;nbsp;p</span>ublic</span> boolean process(Event event) { <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		String propPath = (String) event.getProperty(SlingConstants.PROPERTY_PATH); <br>		String propResType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE); <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String propPath = (String) event.getProperty(SlingConstants.PROPERTY_PATH); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String propResType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>//</span> an event is sent if a file is added to /tmp/dropbox <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		if (propPath.startsWith(&quot;/tmp/dropbox&quot;) &amp;&amp; propResType.equals(&quot;nt:file&quot;)) { <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if (propPath.startsWith(&quot;/tmp/dropbox&quot;) &amp;&amp; propResType.equals(&quot;nt:file&quot;)) { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>final</span> Dictionary&lt;String, Object&gt; props = new Hashtable&lt;String, Object&gt;(); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> props.put(EventUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">    		props.put(&quot;resourcePath&quot;, propPath); <br>    		Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props); <br>    		eventAdmin.sendEvent(dropboxJobEvent); <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;props.put(&quot;resourcePath&quot;, propPath); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;eventAdmin.sendEvent(dropboxJobEvent); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>log.info(&quot;the</span> dropbox job has been sent: {}&quot;, propPath); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		} <br>		return true; <br>	} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return true; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The whole service looks now as follows: <br>... <br> <br></td></tr>
            <tr><td class="diff-unchanged" >Now that you have a service that sends job events whenever a file is uploaded to &quot;/tmp/dropbox&quot;, <br></td></tr>
            <tr><td class="diff-unchanged" >you will create the service DropBoxEventHandler that listens to those job events and moves the files to a location <br>corresponding to their mime-types. <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>To listen to the job events that have been defined before: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The service also needs to implement the interface org.osgi.service.event.EventHandler and its handleEvent(Event event) method. <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The property &quot;event.topics&quot; needs to be set to &quot;com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC&quot; in the class annotations. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>To move the files: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> The service needs to implement the interface org.apache.sling.event.JobProcessor and its process(org.osgi.service.event.Event job) method. <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The first part of the code looks as follows: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words">/*<span class="diff-added-chars"style="background-color: #dfd;">\</span>*</span> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> * @scr.component  immediate=&quot;true&quot; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;\* @scr.component&amp;nbsp; immediate=&quot;true&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> @scr.service interface=&quot;org.osgi.service.event.EventHandler&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> @scr.property name=&quot;event.topics&quot; valueRef=&quot;com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;</span>*/</span> <br></td></tr>
            <tr><td class="diff-unchanged" > <br>public class DropBoxEventHandler implements JobProcessor, EventHandler { <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Several class fields are defined: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> for logging <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> to reference the SlingRepository and the JcrResourceResolverFactory services, which are used in the implementation <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> to define the destination path of the files <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">    /** Default log. */ <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp; /*\* Default log. \*/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> protected final Logger log = LoggerFactory.getLogger(this.getClass()); <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private Session adminSession; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> <br>    /** @scr.reference */ <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp; <br>&amp;nbsp;&amp;nbsp;&amp;nbsp; /*\* @scr.reference \*/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private SlingRepository repository; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> <br>    /** <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp; <br>&amp;nbsp;&amp;nbsp;&amp;nbsp; /*\* <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> * @scr.reference <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">     */ <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; \*/ <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private JcrResourceResolverFactory resolverFactory; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;"> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private final static String IMAGES_PATH = &quot;/dropbox/images/&quot;; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private final static String MUSIC_PATH = &quot;/dropbox/music/&quot;; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private final static String MOVIES_PATH = &quot;/dropbox/movies/&quot;; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> private final static String OTHER_PATH = &quot;/dropbox/other/&quot;; <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The org.osgi.service.event.EventHandler#handleEvent(Event event) method needs to be implemented: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbs</span>p<span class="diff-added-chars"style="background-color: #dfd;">;&amp;nbsp; &amp;nbsp;p</span>ublic</span> void handleEvent(Event event) { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> if (EventUtil.isLocal(event)) { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> EventUtil.processJob(event, this); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	    } <br>	} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The org.apache.sling.event.JobProcessor#process(Event event) method needs to be implemented. <br>The logic is as follows: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> the resource path is extracted from the job event property <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> the resource is obtained from the file path <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> if the resource is a file, the destination path is defined based on the file mime-type <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">-</span> <span class="diff-added-words"style="background-color: #dfd;">\-</span> the file is moved to the new location <br></td></tr>
            <tr><td class="diff-unchanged" > <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbs</span>p<span class="diff-added-chars"style="background-color: #dfd;">;&amp;nbsp; &amp;nbsp;p</span>ublic</span> boolean process(Event event) { <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		try { <br>			String resourcePath = (String) event.getProperty(&quot;resourcePath&quot;); <br>			String resourceName = resourcePath.substring(resourcePath.lastIndexOf(&quot;/&quot;) + 1); <br>        	adminSession = repository.loginAdministrative(null); <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;try { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String resourcePath = (String) event.getProperty(&quot;resourcePath&quot;); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String resourceName = resourcePath.substring(resourcePath.lastIndexOf(&quot;/&quot;) + 1); <br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;adminSession = repository.loginAdministrative(null); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> Resource res = resourceResolver.getResource(resourcePath); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> if (ResourceUtil.isA(res, &quot;nt:file&quot;)) { <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	        	String mimeType = res.getResourceMetadata().getContentType(); <br>	        	String destDir; <br>	        	if (mimeType.equals(&quot;image/png&quot;)) { <br>	        		destDir = IMAGES_PATH; <br>	        	} <br>	        	else if (mimeType.equals(&quot;audio/mpeg&quot;)) { <br>	        		destDir = MUSIC_PATH; <br>	        	} <br>	        	else if (mimeType.equals(&quot;video/x-msvideo&quot;)) { <br>	        		destDir = MOVIES_PATH; <br>	        	} <br>	        	else { <br>	        		destDir = OTHER_PATH; <br>	        	} <br>        		adminSession.move(resourcePath, destDir + resourceName); <br>	        	adminSession.save(); <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String mimeType = res.getResourceMetadata().getContentType(); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;String destDir; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;if (mimeType.equals(&quot;image/png&quot;)) { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;destDir = IMAGES_PATH; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;else if (mimeType.equals(&quot;audio/mpeg&quot;)) { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;destDir = MUSIC_PATH; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;else if (mimeType.equals(&quot;video/x-msvideo&quot;)) { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;destDir = MOVIES_PATH; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;else { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;destDir = OTHER_PATH; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;adminSession.move(resourcePath, destDir + resourceName); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;adminSession.save(); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>log.info(&quot;The</span> file {} has been moved to {}&quot;, resourceName, destDir); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	        } <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> return true; <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		} catch (RepositoryException e) { <br>			log.error(&quot;RepositoryException: &quot; + e); <br>			return false; <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;} catch (RepositoryException e) { <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;log.error(&quot;RepositoryException: &quot; + e); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;return false; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> } finally { <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> if (adminSession <span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">\</span>!=</span> null &amp;&amp; adminSession.isLive()) { <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">            	adminSession.logout(); <br>            	adminSession = null; <br>            } <br>        } <br>	} <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;adminSession.logout(); <br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;adminSession = null; <br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } <br>&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; } <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;} <br></td></tr>
            <tr><td class="diff-unchanged" > <br>The complete code for the service looks as follows: <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br>Sling sends OSGi events when resources are added, removed or changed <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	org.apache.sling.api.SlingConstants: <br>		TOPIC_RESOURCE_ADDED <br>		TOPIC_RESOURCE_REMOVED <br>		TOPIC_RESOURCE_CHANGED <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;org.apache.sling.api.SlingConstants: <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;TOPIC_RESOURCE_ADDED <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;TOPIC_RESOURCE_REMOVED <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;TOPIC_RESOURCE_CHANGED <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Web console: see all the events sent <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	http://localhost:4502/system/console/events <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;http://localhost:4502/system/console/events <br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br>How to listen to osgi events: <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">	subscribe to the event <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;subscribe to the event <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span> * @scr.property name=&quot;event.topics&quot; valueRef=&quot;org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED&quot; <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>the</span> service needs to implement JobProcessor and EventHandler (org.osgi.service.event.EventHandler, listener for events) <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		process(org.osgi.service.event.Event job) from JobProcessor <br>		handleEvent(Event event) from EventHandler <br> <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;process(org.osgi.service.event.Event job) from JobProcessor <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;handleEvent(Event event) from EventHandler <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; <br></td></tr>
            <tr><td class="diff-unchanged" >Send a job event <br> <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">		reference the osgi EventAdmin <br>		define the JOB_TOPIC <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;reference the osgi EventAdmin <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;define the JOB_TOPIC <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>define</span> the job and send it: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-changed-words"><span class="diff-added-chars"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;</span>final</span> Dictionary&lt;String, Object&gt; props = new Hashtable&lt;String, Object&gt;(); <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;</span> props.put(EventUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC); <br></td></tr>
            <tr><td class="diff-deleted-lines" style="color:#999;background-color:#fdd;text-decoration:line-through;">    		props.put(&quot;nodePath&quot;, propPath); <br>    		Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props); <br>    		eventAdmin.sendEvent(dropboxJobEvent); <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;props.put(&quot;nodePath&quot;, propPath); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props); <br>&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;eventAdmin.sendEvent(dropboxJobEvent); <br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br>Listen to a Job event <br>same as listening to an event, except: <br></td></tr>
            <tr><td class="diff-changed-lines" ><span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">*</span> <span class="diff-added-words"style="background-color: #dfd;">&amp;nbsp;\*</span> @scr.property name=&quot;event.topics&quot; valueRef=&quot;com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC&quot; <br></td></tr>
            <tr><td class="diff-unchanged" > <br>Complete code sample <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <p>Apache Sling provides some mechanisms and support for managing events. This page describes ...</p>

<p>Sling distinguishes between 2 types of events:<br/>
&#45; standard events: no garantee of processing<br/>
&#45; job events: garantee of processing. Someone has to do something with the event (do the job).</p>

<p>The event mechanism is leveraging the OSGi Event Admin Specification (OSGi Compendium 113).<br/>
The OSGi API is very simple and leightweight - sending an event is just generating the event object and calling the event admin. Receiving the event is implementing a single interface and declaring through properties which topics one is interested in. For more details please refer to the following javadocs:<br/>
&#45; package org.osgi.service.event of the OSGI API (<a href="http://www.osgi.org/javadoc/r4v42/org/osgi/service/event/package-summary.html" class="external-link" rel="nofollow">http://www.osgi.org/javadoc/r4v42/org/osgi/service/event/package-summary.html</a>).<br/>
&#45; package org.apache.sling.event of the Sling API (<a href="http://sling.apache.org/apidocs/sling5/org/apache/sling/event/package-summary.html" class="external-link" rel="nofollow">http://sling.apache.org/apidocs/sling5/org/apache/sling/event/package-summary.html</a>)</p>

<p>You can learn more in the "Eventing, Jobs and Scheduling" section (<a href="http://sling.apache.org/site/eventing-and-jobs.html" class="external-link" rel="nofollow">http://sling.apache.org/site/eventing-and-jobs.html</a>).</p>

<p>To get started with the Sling eventing API, you will implement here a service that listens to files posted to /tmp/dropbox and moves them to the appropriate locations depending on the mime-type:<br/>
&nbsp;&#42; images (.png) are moved to /dropbox/images/<br/>
&nbsp;&#42; music (.mp3) are moved to /dropbox/music/<br/>
&nbsp;&#42; movies (.avi) are moved to /dropbox/movies/<br/>
&nbsp;&#42; otherwise the files are moved to /dropbox/other/</p>

<p>To do that, you will implement 2 services. The first one, called OsgiDropBoxService:<br/>
&#45; listens to osgi event "resource added"<br/>
&#45; sends a job event<br/>
The second one, called DropBoxEventHandler:<br/>
&#45; listens to job event "file added to /tmp/dropbox"<br/>
&#45; moves the file according to its extension</p>

<p>Listening to osgi event "resource added"</p>

<p>To listen to the specific osgi event "resource added":<br/>
&#45; The service needs to implement the interface org.osgi.service.event.EventHandler and its handleEvent(Event event) method.<br/>
&#45; The property "event.topics" needs to be set to "org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED" in the class annotations.<br/>
To get a list of possible events available in Sling (resource added, removed or changed), refer to the javadocs for org.apache.sling.api.SlingConstants.</p>

<p>To send a job event:<br/>
&#45; The service needs to implement the interface org.apache.sling.event.JobProcessor and its process(org.osgi.service.event.Event job) method.</p>

<p>The first part of the code looks as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
/*\*
&amp;nbsp;\* The &lt;code&gt;OsgiDropBoxService&lt;/code&gt; is listening content added to /tmp/dropbox by using OSGI events
&amp;nbsp;\*
&amp;nbsp;\* @scr.component&amp;nbsp; immediate=<span class="code-quote">"<span class="code-keyword">true</span>"</span>
&amp;nbsp;\* @scr.service <span class="code-keyword">interface</span>=<span class="code-quote">"org.osgi.service.event.EventHandler"</span>
&amp;nbsp;\* @scr.property name=<span class="code-quote">"event.topics"</span> valueRef=<span class="code-quote">"org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED"</span>
&amp;nbsp;*/
<span class="code-keyword">public</span> class OsgiDropBoxService <span class="code-keyword">implements</span> JobProcessor, EventHandler {
</pre>
</div></div>

<p>The Event Admin service is needed to send the job event:<br/>
&nbsp;&nbsp; &nbsp;/*&#42;<br/>
&nbsp;&nbsp; &nbsp; * The OSGI event admin used for sending events<br/>
&nbsp;&nbsp;&nbsp;&nbsp; * @scr.reference<br/>
&nbsp;&nbsp; &nbsp; &#42;/<br/>
&nbsp;&nbsp; &nbsp;private EventAdmin eventAdmin;<br/>
&nbsp;&nbsp; &nbsp;<br/>
The job topic for dropbox job events needs to be defined:<br/>
&nbsp;&nbsp;&nbsp; /*&#42; The job topic for dropbox job events. &#42;/<br/>
&nbsp;&nbsp;&nbsp; public static final String JOB_TOPIC = "com/sling/eventing/dropbox/job";</p>

<p>The org.osgi.service.event.EventHandler#handleEvent(Event event) method needs to be implemented:<br/>
&nbsp;&nbsp; &nbsp;public void handleEvent(Event event) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if (EventUtil.isLocal(event)) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EventUtil.processJob(event, this);
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; }</span> 
<p>&nbsp;&nbsp; &nbsp;}</p>

<p>The org.apache.sling.event.JobProcessor#process(Event event) method needs to be implemented.<br/>
The logic is as follows:<br/>
&#45; the OSGI event is analyzed<br/>
&#45; if the event is a file that has been added to "/tmp/dropbox":<br/>
&nbsp;&nbsp; &nbsp;&#45; an event is created with 2 properties:<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#45; one to flag the event as a job event<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&#45; the file path<br/>
&nbsp;&nbsp; &nbsp;&#45; the job event is sent to all the listeners that subscribe to the topic of the event.</p>

<p>&nbsp;&nbsp; &nbsp;public boolean process(Event event) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String propPath = (String) event.getProperty(SlingConstants.PROPERTY_PATH);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String propResType = (String) event.getProperty(SlingConstants.PROPERTY_RESOURCE_TYPE);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;// an event is sent if a file is added to /tmp/dropbox<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (propPath.startsWith("/tmp/dropbox") &amp;&amp; propResType.equals("nt:file")) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;final Dictionary&lt;String, Object&gt; props = new Hashtable&lt;String, Object&gt;();<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; props.put(EventUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;props.put("resourcePath", propPath);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;eventAdmin.sendEvent(dropboxJobEvent);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.info("the dropbox job has been sent: {}", propPath);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;return true;<br/>
&nbsp;&nbsp; &nbsp;}</p>

<p>The whole service looks now as follows:<br/>
...</p>

<p>Now that you have a service that sends job events whenever a file is uploaded to "/tmp/dropbox",<br/>
you will create the service DropBoxEventHandler that listens to those job events and moves the files to a location<br/>
corresponding to their mime-types.</p>

<p>Listening to job events</p>

<p>To listen to the job events that have been defined before:<br/>
&#45; The service also needs to implement the interface org.osgi.service.event.EventHandler and its handleEvent(Event event) method.<br/>
&#45; The property "event.topics" needs to be set to "com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC" in the class annotations.</p>

<p>To move the files:<br/>
&#45; The service needs to implement the interface org.apache.sling.event.JobProcessor and its process(org.osgi.service.event.Event job) method.</p>

<p>The first part of the code looks as follows:<br/>
/*&#42;<br/>
&nbsp;&#42; @scr.component&nbsp; immediate="true"<br/>
&nbsp;&#42; @scr.service interface="org.osgi.service.event.EventHandler"<br/>
&nbsp;&#42; @scr.property name="event.topics" valueRef="com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC"<br/>
&nbsp;*/</p>

<p>public class DropBoxEventHandler implements JobProcessor, EventHandler {</p>



<p>Several class fields are defined:<br/>
&#45; for logging<br/>
&#45; to reference the SlingRepository and the JcrResourceResolverFactory services, which are used in the implementation<br/>
&#45; to define the destination path of the files</p>

<p>&nbsp;&nbsp;&nbsp; /*&#42; Default log. &#42;/<br/>
&nbsp;&nbsp;&nbsp; protected final Logger log = LoggerFactory.getLogger(this.getClass());</p>

<p>&nbsp;&nbsp;&nbsp; private Session adminSession;<br/>
&nbsp;&nbsp; &nbsp;<br/>
&nbsp;&nbsp;&nbsp; /*&#42; @scr.reference &#42;/<br/>
&nbsp;&nbsp;&nbsp; private SlingRepository repository;<br/>
&nbsp;&nbsp; &nbsp;<br/>
&nbsp;&nbsp;&nbsp; /*&#42;<br/>
&nbsp;&nbsp;&nbsp;&nbsp; * @scr.reference<br/>
&nbsp;&nbsp;&nbsp;&nbsp; &#42;/<br/>
&nbsp;&nbsp;&nbsp; private JcrResourceResolverFactory resolverFactory;<br/>
&nbsp;&nbsp; &nbsp;<br/>
&nbsp;&nbsp;&nbsp; private final static String IMAGES_PATH = "/dropbox/images/";<br/>
&nbsp;&nbsp;&nbsp; private final static String MUSIC_PATH = "/dropbox/music/";<br/>
&nbsp;&nbsp;&nbsp; private final static String MOVIES_PATH = "/dropbox/movies/";<br/>
&nbsp;&nbsp;&nbsp; private final static String OTHER_PATH = "/dropbox/other/";</p>

<p>The org.osgi.service.event.EventHandler#handleEvent(Event event) method needs to be implemented:<br/>
&nbsp;&nbsp; &nbsp;public void handleEvent(Event event) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if (EventUtil.isLocal(event)) </p></div>
<p>&nbsp;&nbsp; &nbsp;}</p>

<p>The org.apache.sling.event.JobProcessor#process(Event event) method needs to be implemented.<br/>
The logic is as follows:<br/>
&#45; the resource path is extracted from the job event property<br/>
&#45; the resource is obtained from the file path<br/>
&#45; if the resource is a file, the destination path is defined based on the file mime-type<br/>
&#45; the file is moved to the new location</p>

<p>&nbsp;&nbsp; &nbsp;public boolean process(Event event) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;try {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String resourcePath = (String) event.getProperty("resourcePath");<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String resourceName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1);<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;adminSession = repository.loginAdministrative(null);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Resource res = resourceResolver.getResource(resourcePath);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (ResourceUtil.isA(res, "nt:file")) {<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String mimeType = res.getResourceMetadata().getContentType();<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;String destDir;<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (mimeType.equals("image/png")) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;destDir = IMAGES_PATH;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}</span> </div>
<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (mimeType.equals("audio/mpeg")) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;destDir = MUSIC_PATH;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}</span> </div>
<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else if (mimeType.equals("video/x-msvideo")) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;destDir = MOVIES_PATH;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}</span> </div>
<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;else </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;destDir = OTHER_PATH;
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}</span> </div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;adminSession.move(resourcePath, destDir + resourceName);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;adminSession.save();<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.info("The file {} has been moved to {}", resourceName, destDir);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return true;<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} catch (RepositoryException e) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.error(&quot;RepositoryException}</span> </div>
<p> finally {<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (adminSession &#33;= null &amp;&amp; adminSession.isLive()) </p>
<div class="error"><span class="error">Unknown macro: {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;adminSession.logout();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;adminSession = null;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</span> </div>
<p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br/>
&nbsp;&nbsp; &nbsp;}</p>

<p>The complete code for the service looks as follows:<br/>
...</p>


<p>2 services</p>

<p>Sling sends OSGi events when resources are added, removed or changed<br/>
&nbsp;&nbsp; &nbsp;org.apache.sling.api.SlingConstants:<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TOPIC_RESOURCE_ADDED<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TOPIC_RESOURCE_REMOVED<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;TOPIC_RESOURCE_CHANGED</p>

<p>Web console: see all the events sent<br/>
&nbsp;&nbsp; &nbsp;<a href="http://localhost:4502/system/console/events" class="external-link" rel="nofollow">http://localhost:4502/system/console/events</a></p>


<p>How to listen to osgi events:<br/>
&nbsp;&nbsp; &nbsp;subscribe to the event<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; * @scr.property name="event.topics" valueRef="org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED"<br/>
&nbsp;&nbsp; &nbsp;the service needs to implement JobProcessor and EventHandler (org.osgi.service.event.EventHandler, listener for events)<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;process(org.osgi.service.event.Event job) from JobProcessor<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;handleEvent(Event event) from EventHandler<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;<br/>
Send a job event</p>

<p>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;reference the osgi EventAdmin<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;define the JOB_TOPIC<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;define the job and send it:<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;final Dictionary&lt;String, Object&gt; props = new Hashtable&lt;String, Object&gt;();<br/>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; props.put(EventUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;props.put("nodePath", propPath);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;Event dropboxJobEvent = new Event(EventUtil.TOPIC_JOB, props);<br/>
&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;eventAdmin.sendEvent(dropboxJobEvent);</p>


<p>Listen to a Job event<br/>
same as listening to an event, except:<br/>
&nbsp;&#42; @scr.property name="event.topics" valueRef="com.jck.sling.eventing.osgi.OsgiDropBoxService.JOB_TOPIC"</p>

<p>Complete code sample<br/>
...</p>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action" class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Managing+events">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=23335037&revisedVersion=3&originalVersion=2">View Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/SLINGxSITE/Managing+events?showComments=true&amp;showCommentArea=true#addcomment">Add Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message