incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From build...@apache.org
Subject svn commit: r857064 - in /websites/staging/sling/trunk/content: ./ documentation/tutorials-how-tos/how-to-manage-events-in-sling.html
Date Wed, 03 Apr 2013 07:49:46 GMT
Author: buildbot
Date: Wed Apr  3 07:49:46 2013
New Revision: 857064

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 Wed Apr  3 07:49:46 2013
@@ -1 +1 @@
-1462288
+1463855

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
Wed Apr  3 07:49:46 2013
@@ -116,53 +116,48 @@ Sling makes a distinction between events
 </ul>
 <h2 id="listening-to-osgi-events">Listening to OSGI Events</h2>
 <p>To listen to the specific OSGI event <strong>resource added<em> the
property </em>event.topics* needs to be set to </strong>org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED**
in the class annotations.</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
-2</pre></div></td><td class="code"><div class="codehilite"><pre>
<span class="nd">@Property</span><span class="o">(</span><span
class="n">name</span><span class="o">=</span><span class="s">&quot;event.topics&quot;</span><span
class="o">,</span>
+<div class="codehilite"><pre> <span class="nd">@Property</span><span
class="o">(</span><span class="n">name</span><span class="o">=</span><span
class="s">&quot;event.topics&quot;</span><span class="o">,</span>
     <span class="n">value</span><span class="o">=</span><span
class="n">org</span><span class="o">.</span><span class="na">apache</span><span
class="o">.</span><span class="na">sling</span><span class="o">.</span><span
class="na">api</span><span class="o">.</span><span class="na">SlingConstants</span><span
class="o">.</span><span class="na">TOPIC_RESOURCE_ADDED</span><span
class="o">)</span>
 </pre></div>
-</td></tr></table>
+
 
 <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 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>To send an event the following code can be used: </p>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kt">void</span> <span class="nf">sendEvent</span><span class="o">()</span>
<span class="o">{</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">JobUtil</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">RESOURCE_PATH</span><span class="o">);</span>
+    <span class="kd">final</span> <span class="n">Event</span> <span
class="n">myEvent</span> <span class="o">=</span> <span class="k">new</span>
<span class="n">Event</span><span class="o">(</span><span class="n">JobUtil</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="n">eventAdmin</span><span class="o">.</span><span
class="na">sendEvent</span><span class="o">(</span><span class="n">myEvent</span><span
class="o">);</span>
+</pre></div>
+
+
 <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>
+<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>
+
 
 <p>To send the job event the Event Admin service needs to be referenced:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
-2</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="nd">@Reference</span>
+<div class="codehilite"><pre><span class="nd">@Reference</span>
 <span class="kd">private</span> <span class="n">EventAdmin</span>
<span class="n">eventAdmin</span><span class="o">;</span>
 </pre></div>
-</td></tr></table>
+
 
 <p>The job topic for dropbox job events needs to be defined:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
-2</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="cm">/** The job topic for dropbox job events. */</span>
+<div class="codehilite"><pre><span class="cm">/** The job topic for dropbox
job events. */</span>
 <span class="kd">public</span> <span class="kd">static</span> <span
class="kd">final</span> <span class="n">String</span> <span class="n">JOB_TOPIC</span>
<span class="o">=</span> <span class="s">&quot;com/sling/eventing/dropbox/job&quot;</span><span
class="o">;</span>
 </pre></div>
-</td></tr></table>
+
 
 <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
-3
-4
-5</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="kd">public</span> <span class="kt">void</span> <span class="nf">handleEvent</span><span
class="o">(</span><span class="n">Event</span> <span class="n">event</span><span
class="o">)</span> <span class="o">{</span>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kt">void</span> <span class="nf">handleEvent</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="k">if</span> <span class="o">(</span><span class="n">EventUtil</span><span
class="o">.</span><span class="na">isLocal</span><span class="o">(</span><span
class="n">event</span><span class="o">))</span> <span class="o">{</span>
         <span class="n">EventUtil</span><span class="o">.</span><span
class="na">processJob</span><span class="o">(</span><span class="n">event</span><span
class="o">,</span> <span class="k">this</span><span class="o">);</span>
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
-</td></tr></table>
+
 
 <p>The <strong>org.apache.sling.event.JobProcessor#process(Event event)</strong>
method needs to be implemented:</p>
 <p>Its logic is as follows:</p>
@@ -196,8 +191,8 @@ Sling makes a distinction between events
 <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>
+<p>:::java
+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>
@@ -231,17 +226,16 @@ Sling makes a distinction between events
 <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>
 <p>To listen to the job events that have been defined before the property <strong>event.topics</strong>
needs to be set to <strong>mypackage.DropBoxService.JOB_TOPIC</strong> in the
class annotations:</p>
-<table class="codehilitetable"><tr><td class="linenos"><div class="linenodiv"><pre>1
-2</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="nd">@Property</span><span class="o">(</span><span class="n">name</span><span
class="o">=</span><span class="s">&quot;event.topics&quot;</span><span
class="o">,</span>
+<div class="codehilite"><pre><span class="nd">@Property</span><span
class="o">(</span><span class="n">name</span><span class="o">=</span><span
class="s">&quot;event.topics&quot;</span><span class="o">,</span>
     <span class="n">value</span><span class="o">=</span><span
class="n">mypackage</span><span class="o">.</span><span class="na">DropBoxService</span><span
class="o">.</span><span class="na">JOB_TOPIC</span><span class="o">)</span>
 </pre></div>
-</td></tr></table>
+
 
 <h2 id="handling-job-events">Handling Job Events</h2>
 <p>To move the files 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">DropBoxEventHandler</span>
<span class="kd">implements</span> <span class="n">JobProcessor</span><span
class="o">,</span> <span class="n">EventHandler</span> <span class="o">{</span>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kd">class</span> <span class="nc">DropBoxEventHandler</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>
+
 
 <p>Some class fields need to be defined:</p>
 <ul>
@@ -249,8 +243,8 @@ Sling makes a distinction between events
 <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>/
+<p>:::java
+/*<em> Default log. </em>/
 protected final Logger log = LoggerFactory.getLogger(this.getClass());</p>
 <p>@Reference
 private SlingRepository repository;</p>
@@ -263,17 +257,13 @@ private final static String OTHER_PATH =
 </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
-3
-4
-5</pre></div></td><td class="code"><div class="codehilite"><pre><span
class="kd">public</span> <span class="kt">void</span> <span class="nf">handleEvent</span><span
class="o">(</span><span class="n">Event</span> <span class="n">event</span><span
class="o">)</span> <span class="o">{</span>
+<div class="codehilite"><pre><span class="kd">public</span> <span
class="kt">void</span> <span class="nf">handleEvent</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="k">if</span> <span class="o">(</span><span class="n">EventUtil</span><span
class="o">.</span><span class="na">isLocal</span><span class="o">(</span><span
class="n">event</span><span class="o">))</span> <span class="o">{</span>
         <span class="n">EventUtil</span><span class="o">.</span><span
class="na">processJob</span><span class="o">(</span><span class="n">event</span><span
class="o">,</span> <span class="k">this</span><span class="o">);</span>
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
-</td></tr></table>
+
 
 <p>The <strong>org.apache.sling.event.JobProcessor#process(Event event)</strong>
method needs to be implemented.</p>
 <p>Its logic is as follows:</p>
@@ -284,44 +274,7 @@ private final static String OTHER_PATH =
 <li>The file is moved to the new location.</li>
 </ul>
 <p>or in Java Code:</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
-27
-28
-29
-30
-31
-32
-33
-34
-35
-36
-37
-38</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>
+<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="n">Session</span> <span class="n">adminSession</span>
<span class="o">=</span> <span class="kc">null</span><span class="o">;</span>
     <span class="k">try</span> <span class="o">{</span>
         <span class="n">String</span> <span class="n">resourcePath</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="s">&quot;resourcePath&quot;</span><span
class="o">);</span>
@@ -334,14 +287,11 @@ private final static String OTHER_PATH =
             <span class="n">String</span> <span class="n">destDir</span><span
class="o">;</span>
             <span class="k">if</span> <span class="o">(</span><span
class="n">mimeType</span><span class="o">.</span><span class="na">equals</span><span
class="o">(</span><span class="s">&quot;image/png&quot;</span><span
class="o">))</span> <span class="o">{</span>
                 <span class="n">destDir</span> <span class="o">=</span>
<span class="n">IMAGES_PATH</span><span class="o">;</span>
-            <span class="o">}</span>
-            <span class="k">else</span> <span class="nf">if</span>
<span class="o">(</span><span class="n">mimeType</span><span class="o">.</span><span
class="na">equals</span><span class="o">(</span><span class="s">&quot;audio/mpeg&quot;</span><span
class="o">))</span> <span class="o">{</span>
+            <span class="o">}</span> <span class="k">else</span>
<span class="k">if</span> <span class="o">(</span><span class="n">mimeType</span><span
class="o">.</span><span class="na">equals</span><span class="o">(</span><span
class="s">&quot;audio/mpeg&quot;</span><span class="o">))</span>
<span class="o">{</span>
                 <span class="n">destDir</span> <span class="o">=</span>
<span class="n">MUSIC_PATH</span><span class="o">;</span>
-            <span class="o">}</span>
-            <span class="k">else</span> <span class="nf">if</span>
<span class="o">(</span><span class="n">mimeType</span><span class="o">.</span><span
class="na">equals</span><span class="o">(</span><span class="s">&quot;video/x-msvideo&quot;</span><span
class="o">))</span> <span class="o">{</span>
+            <span class="o">}</span> <span class="k">else</span>
<span class="k">if</span> <span class="o">(</span><span class="n">mimeType</span><span
class="o">.</span><span class="na">equals</span><span class="o">(</span><span
class="s">&quot;video/x-msvideo&quot;</span><span class="o">))</span>
<span class="o">{</span>
                 <span class="n">destDir</span> <span class="o">=</span>
<span class="n">MOVIES_PATH</span><span class="o">;</span>
-            <span class="o">}</span>
-            <span class="k">else</span> <span class="o">{</span>
+            <span class="o">}</span> <span class="k">else</span>
<span class="o">{</span>
                 <span class="n">destDir</span> <span class="o">=</span>
<span class="n">OTHER_PATH</span><span class="o">;</span>
             <span class="o">}</span>
             <span class="n">adminSession</span><span class="o">.</span><span
class="na">move</span><span class="o">(</span><span class="n">resourcePath</span><span
class="o">,</span> <span class="n">destDir</span> <span class="o">+</span>
<span class="n">resourceName</span><span class="o">);</span>
@@ -360,11 +310,11 @@ private final static String OTHER_PATH =
     <span class="o">}</span>
 <span class="o">}</span>
 </pre></div>
-</td></tr></table>
+
 
 <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. 1451530 by cziegeler on Fri, 1 Mar 2013 08:57:20 +0000
+        Rev. 1463855 by fmeschbe on Wed, 3 Apr 2013 07:49:22 +0000
       </div>
       <div class="trademarkFooter"> 
         Apache Sling, Sling, Apache, the Apache feather logo, and the Apache Sling project



Mime
View raw message