incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r852510 - in /websites/staging/sling/trunk/content: ./ documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
Date Fri, 01 Mar 2013 08:57:32 GMT
Author: buildbot
Date: Fri Mar  1 08:57:32 2013
New Revision: 852510

Log:
Staging update by buildbot for sling

Modified:
    websites/staging/sling/trunk/content/   (props changed)
    websites/staging/sling/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html

Propchange: websites/staging/sling/trunk/content/
------------------------------------------------------------------------------
--- cms:source-revision (original)
+++ cms:source-revision Fri Mar  1 08:57:32 2013
@@ -1 +1 @@
-1448208
+1451530

Modified: websites/staging/sling/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
==============================================================================
--- websites/staging/sling/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
(original)
+++ websites/staging/sling/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
Fri Mar  1 08:57:32 2013
@@ -124,7 +124,15 @@ Sling makes a distinction between events
 
 <p>You can refer to the <a href="/apidocs/sling6/org/apache/sling/api/SlingConstants.html">org.apache.sling.api.SlingConstants</a>
class in the Javadocs to know about other events available in Sling.</p>
 <h2 id="sending-job-events">Sending Job Events</h2>
-<p>To send a job event the service needs to implement the <strong>org.osgi.service.event.EventHandler</strong>
and <strong>org.apache.sling.event.JobProcessor</strong> interfaces:</p>
+<p>To send an event the following code can be used: 
+    #!java
+    public void sendEvent() {
+        final Dictionary<String, Object> props = new Hashtable<String, Object>();
+        props.put(JobUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC);
+        props.put("resourcePath", RESOURCE_PATH);
+        final Event myEvent = new Event(JobUtil.TOPIC_JOB, props);
+        eventAdmin.sendEvent(myEvent);</p>
+<p>However, for our example, to send a job event the service needs to implement the
<strong>org.osgi.service.event.EventHandler</strong> and <strong>org.apache.sling.event.JobProcessor</strong>
interfaces:</p>
 <table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1</pre></div></td><td
class="code"><div class="codehilite"><pre><span class="kd">public</span>
<span class="kd">class</span> <span class="nc">DropBoxService</span>
<span class="kd">implements</span> <span class="n">JobProcessor</span><span
class="o">,</span> <span class="n">EventHandler</span> <span class="o">{</span>
 </pre></div>
 </td></tr></table>
@@ -157,60 +165,68 @@ Sling makes a distinction between events
 </td></tr></table>
 
 <p>The <strong>org.apache.sling.event.JobProcessor#process(Event event)</strong>
method needs to be implemented:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13
-14
-15
-16
-17
-18
-19
-20
-21
-22
-23
-24
-25
-26</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="kd">public</span> <span class="kt">boolean</span> <span class="nf">process</span><span
class="o">(</span><span class="n">Event</span> <span class="n">event</span><span
class="o">)</span> <span class="o">{</span>
-
-    <span class="c1">// get the resource event information</span>
-    <span class="n">String</span> <span class="n">propPath</span>
<span class="o">=</span> <span class="o">(</span><span class="n">String</span><span
class="o">)</span> <span class="n">event</span><span class="o">.</span><span
class="na">getProperty</span><span class="o">(</span><span class="n">SlingConstants</span><span
class="o">.</span><span class="na">PROPERTY_PATH</span><span class="o">);</span>
-    <span class="n">String</span> <span class="n">propResType</span>
<span class="o">=</span> <span class="o">(</span><span class="n">String</span><span
class="o">)</span> <span class="n">event</span><span class="o">.</span><span
class="na">getProperty</span><span class="o">(</span><span class="n">SlingConstants</span><span
class="o">.</span><span class="na">PROPERTY_RESOURCE_TYPE</span><span
class="o">);</span>
+<p>Its logic is as follows:</p>
+<ul>
+<li>The OSGI event is analyzed.</li>
+<li>If the event is a file that has been added to <em>/tmp/dropbox</em>:</li>
+<li>
+<ul>
+<li>An event is created with 2 properties:</li>
+</ul>
+</li>
+<li>
+<ul>
+<li>
+<ul>
+<li>A property to set the event as a job event.</li>
+</ul>
+</li>
+</ul>
+</li>
+<li>
+<ul>
+<li>
+<ul>
+<li>A property for the file path.</li>
+</ul>
+</li>
+</ul>
+</li>
+<li>
+<ul>
+<li>The job event is sent to all the listeners that subscribe to the topic of the event.</li>
+</ul>
+<h1 id="java">!java</h1>
+<p>public boolean process(Event event) {</p>
+<div class="codehilite"><pre><span class="sr">//</span> <span
class="n">get</span> <span class="n">the</span> <span class="n">resource</span>
<span class="n">event</span> <span class="n">information</span>
+<span class="n">String</span> <span class="n">propPath</span> <span
class="o">=</span> <span class="p">(</span><span class="n">String</span><span
class="p">)</span> <span class="n">event</span><span class="o">.</span><span
class="n">getProperty</span><span class="p">(</span><span class="n">SlingConstants</span><span
class="o">.</span><span class="n">PROPERTY_PATH</span><span class="p">);</span>
+<span class="n">String</span> <span class="n">propResType</span>
<span class="o">=</span> <span class="p">(</span><span class="n">String</span><span
class="p">)</span> <span class="n">event</span><span class="o">.</span><span
class="n">getProperty</span><span class="p">(</span><span class="n">SlingConstants</span><span
class="o">.</span><span class="n">PROPERTY_RESOURCE_TYPE</span><span
class="p">);</span>
 
-    <span class="c1">// an event is sent if a file is added to /tmp/dropbox</span>
-    <span class="k">if</span> <span class="o">(</span><span class="n">propPath</span><span
class="o">.</span><span class="na">startsWith</span><span class="o">(</span><span
class="s">&quot;/tmp/dropbox&quot;</span><span class="o">)</span>
<span class="o">&amp;&amp;</span> <span class="n">propResType</span><span
class="o">.</span><span class="na">equals</span><span class="o">(</span><span
class="s">&quot;nt:file&quot;</span><span class="o">))</span>
<span class="o">{</span>
+<span class="sr">//</span> <span class="n">an</span> <span class="n">event</span>
<span class="n">is</span> <span class="n">sent</span> <span class="k">if</span>
<span class="n">a</span> <span class="n">file</span> <span class="n">is</span>
<span class="n">added</span> <span class="n">to</span> <span class="sr">/tmp/</span><span
class="n">dropbox</span>
+<span class="k">if</span> <span class="p">(</span><span class="n">propPath</span><span
class="o">.</span><span class="n">startsWith</span><span class="p">(</span><span
class="s">&quot;/tmp/dropbox&quot;</span><span class="p">)</span>
<span class="o">&amp;&amp;</span> <span class="n">propResType</span><span
class="o">.</span><span class="n">equals</span><span class="p">(</span><span
class="s">&quot;nt:file&quot;</span><span class="p">))</span>
<span class="p">{</span>
 
-        <span class="c1">// configure the job event</span>
-        <span class="kd">final</span> <span class="n">Dictionary</span><span
class="o">&lt;</span><span class="n">String</span><span class="o">,</span>
<span class="n">Object</span><span class="o">&gt;</span> <span
class="n">props</span> <span class="o">=</span> <span class="k">new</span>
<span class="n">Hashtable</span><span class="o">&lt;</span><span
class="n">String</span><span class="o">,</span> <span class="n">Object</span><span
class="o">&gt;();</span>
-        <span class="n">props</span><span class="o">.</span><span
class="na">put</span><span class="o">(</span><span class="n">EventUtil</span><span
class="o">.</span><span class="na">PROPERTY_JOB_TOPIC</span><span
class="o">,</span> <span class="n">JOB_TOPIC</span><span class="o">);</span>
-        <span class="n">props</span><span class="o">.</span><span
class="na">put</span><span class="o">(</span><span class="s">&quot;resourcePath&quot;</span><span
class="o">,</span> <span class="n">propPath</span><span class="o">);</span>
+    <span class="sr">//</span> <span class="n">configure</span> <span
class="n">the</span> <span class="n">job</span> <span class="n">event</span>
+    <span class="n">final</span> <span class="n">Dictionary</span><span
class="o">&lt;</span><span class="n">String</span><span class="p">,</span>
<span class="n">Object</span><span class="o">&gt;</span> <span
class="n">props</span> <span class="o">=</span> <span class="k">new</span>
<span class="n">Hashtable</span><span class="o">&lt;</span><span
class="n">String</span><span class="p">,</span> <span class="n">Object</span><span
class="o">&gt;</span><span class="p">();</span>
+    <span class="n">props</span><span class="o">.</span><span
class="n">put</span><span class="p">(</span><span class="n">EventUtil</span><span
class="o">.</span><span class="n">PROPERTY_JOB_TOPIC</span><span class="p">,</span>
<span class="n">JOB_TOPIC</span><span class="p">);</span>
+    <span class="n">props</span><span class="o">.</span><span
class="n">put</span><span class="p">(</span><span class="s">&quot;resourcePath&quot;</span><span
class="p">,</span> <span class="n">propPath</span><span class="p">);</span>
 
-        <span class="c1">// create the job event</span>
-        <span class="n">Event</span> <span class="n">dropboxJobEvent</span>
<span class="o">=</span> <span class="k">new</span> <span class="n">Event</span><span
class="o">(</span><span class="n">EventUtil</span><span class="o">.</span><span
class="na">TOPIC_JOB</span><span class="o">,</span> <span class="n">props</span><span
class="o">);</span>
+    <span class="sr">//</span> <span class="n">create</span> <span
class="n">the</span> <span class="n">job</span> <span class="n">event</span>
+    <span class="n">Event</span> <span class="n">dropboxJobEvent</span>
<span class="o">=</span> <span class="k">new</span> <span class="n">Event</span><span
class="p">(</span><span class="n">EventUtil</span><span class="o">.</span><span
class="n">TOPIC_JOB</span><span class="p">,</span> <span class="n">props</span><span
class="p">);</span>
 
-        <span class="c1">// deliver the job event</span>
-        <span class="n">eventAdmin</span><span class="o">.</span><span
class="na">sendEvent</span><span class="o">(</span><span class="n">dropboxJobEvent</span><span
class="o">);</span>
+    <span class="sr">//</span> <span class="n">deliver</span> <span
class="n">the</span> <span class="n">job</span> <span class="n">event</span>
+    <span class="n">eventAdmin</span><span class="o">.</span><span
class="n">sendEvent</span><span class="p">(</span><span class="n">dropboxJobEvent</span><span
class="p">);</span>
 
-        <span class="n">log</span><span class="o">.</span><span
class="na">info</span><span class="o">(</span><span class="s">&quot;the
dropbox job has been sent: {}&quot;</span><span class="o">,</span> <span
class="n">propPath</span><span class="o">);</span>
-    <span class="o">}</span>
+    <span class="nb">log</span><span class="o">.</span><span class="n">info</span><span
class="p">(</span><span class="s">&quot;the dropbox job has been sent:
{}&quot;</span><span class="p">,</span> <span class="n">propPath</span><span
class="p">);</span>
+<span class="p">}</span>
 
-    <span class="c1">// all set and done</span>
-    <span class="k">return</span> <span class="kc">true</span><span
class="o">;</span>
-<span class="o">}</span>
+<span class="sr">//</span> <span class="n">all</span> <span class="n">set</span>
<span class="ow">and</span> <span class="n">done</span>
+<span class="k">return</span> <span class="n">true</span><span
class="p">;</span>
 </pre></div>
-</td></tr></table>
 
+
+<p>}</p>
+</li>
+</ul>
 <p>The complete code for the <strong>DropBoxService</strong> service is
available <a href="DropBoxService.java">here</a>.</p>
 <h2 id="listening-to-job-events">Listening to Job Events</h2>
 <p>Now that you have implemented a service that sends a job event when a file is uploaded
to <strong>/tmp/dropbox</strong>, you will implement the service <strong>DropBoxEventHandler</strong>
that listens to those job events and moves the files to a location according to their MIME
types.</p>
@@ -228,34 +244,24 @@ Sling makes a distinction between events
 </td></tr></table>
 
 <p>Some class fields need to be defined:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>
1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
-10
-11
-12
-13</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="cm">/** Default log. */</span>
-<span class="kd">protected</span> <span class="kd">final</span> <span
class="n">Logger</span> <span class="n">log</span> <span class="o">=</span>
<span class="n">LoggerFactory</span><span class="o">.</span><span
class="na">getLogger</span><span class="o">(</span><span class="k">this</span><span
class="o">.</span><span class="na">getClass</span><span class="o">());</span>
-
-<span class="nd">@Reference</span>
-<span class="kd">private</span> <span class="n">SlingRepository</span>
<span class="n">repository</span><span class="o">;</span>
-
-<span class="nd">@Reference</span>    
-<span class="kd">private</span> <span class="n">JcrResourceResolverFactory</span>
<span class="n">resolverFactory</span><span class="o">;</span>
-
-<span class="kd">private</span> <span class="kd">final</span> <span
class="kd">static</span> <span class="n">String</span> <span class="n">IMAGES_PATH</span>
<span class="o">=</span> <span class="s">&quot;/dropbox/images/&quot;</span><span
class="o">;</span>
-<span class="kd">private</span> <span class="kd">final</span> <span
class="kd">static</span> <span class="n">String</span> <span class="n">MUSIC_PATH</span>
<span class="o">=</span> <span class="s">&quot;/dropbox/music/&quot;</span><span
class="o">;</span>
-<span class="kd">private</span> <span class="kd">final</span> <span
class="kd">static</span> <span class="n">String</span> <span class="n">MOVIES_PATH</span>
<span class="o">=</span> <span class="s">&quot;/dropbox/movies/&quot;</span><span
class="o">;</span>
-<span class="kd">private</span> <span class="kd">final</span> <span
class="kd">static</span> <span class="n">String</span> <span class="n">OTHER_PATH</span>
<span class="o">=</span> <span class="s">&quot;/dropbox/other/&quot;</span><span
class="o">;</span>
-</pre></div>
-</td></tr></table>
-
+<ul>
+<li>The default log.</li>
+<li>The references to the SlingRepository and the JcrResourceResolverFactory services,
which are used in the implementation.</li>
+<li>
+<p>The destination paths of the files.</p>
+<h1 id="java_1">!java</h1>
+<p>/*<em> Default log. </em>/
+protected final Logger log = LoggerFactory.getLogger(this.getClass());</p>
+<p>@Reference
+private SlingRepository repository;</p>
+<p>@Reference  <br />
+private JcrResourceResolverFactory resolverFactory;</p>
+<p>private final static String IMAGES_PATH = "/dropbox/images/";
+private final static String MUSIC_PATH = "/dropbox/music/";
+private final static String MOVIES_PATH = "/dropbox/movies/";
+private final static String OTHER_PATH = "/dropbox/other/";</p>
+</li>
+</ul>
 <p>The <strong>org.osgi.service.event.EventHandler#handleEvent(Event event)</strong>
method needs to be implemented:</p>
 <table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
 2
@@ -358,7 +364,7 @@ Sling makes a distinction between events
 
 <p>The complete code for the <strong>DropBoxEventHandler</strong> service
is available <a href="DropBoxEventHandler.java">here</a>.</p>
       <div class="timestamp" style="margin-top: 30px; font-size: 80%; text-align: right;">
-        Rev. 1345850 by fmeschbe on Mon, 4 Jun 2012 07:40:42 +0000
+        Rev. 1451530 by cziegeler on Fri, 1 Mar 2013 08:57:20 +0000
       </div>
       <div class="trademarkFooter"> 
         Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project



Mime
View raw message