You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by fm...@apache.org on 2013/04/03 09:49:23 UTC

svn commit: r1463855 - /sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext

Author: fmeschbe
Date: Wed Apr  3 07:49:22 2013
New Revision: 1463855

URL: http://svn.apache.org/r1463855
Log:
formatting

Modified:
    sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext

Modified: sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext
URL: http://svn.apache.org/viewvc/sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext?rev=1463855&r1=1463854&r2=1463855&view=diff
==============================================================================
--- sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext (original)
+++ sling/site/trunk/content/documentation/tutorials-how-tos/how-to-manage-events-in-sling.mdtext Wed Apr  3 07:49:22 2013
@@ -36,7 +36,7 @@ The second one, called **DropBoxEventHan
 
 To listen to the specific OSGI event **resource added* the property *event.topics* needs to be set to **org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED** in the class annotations.
 
-    #!java
+     :::java
      @Property(name="event.topics",
         value=org.apache.sling.api.SlingConstants.TOPIC_RESOURCE_ADDED)
 
@@ -46,7 +46,8 @@ You can refer to the [org.apache.sling.a
 ## Sending Job Events
 
 To send an event the following code can be used: 
-    #!java
+
+    :::java
     public void sendEvent() {
         final Dictionary<String, Object> props = new Hashtable<String, Object>();
         props.put(JobUtil.PROPERTY_JOB_TOPIC, JOB_TOPIC);
@@ -56,28 +57,27 @@ To send an event the following code can 
 
 However, for our example, to send a job event the service needs to implement the **org.osgi.service.event.EventHandler** and **org.apache.sling.event.JobProcessor** interfaces:
 
-
-    #!java
+    :::java
     public class DropBoxService implements JobProcessor, EventHandler {
 
 
 To send the job event the Event Admin service needs to be referenced:
 
-    #!java
+    :::java
     @Reference
     private EventAdmin eventAdmin;
 
 	
 The job topic for dropbox job events needs to be defined:
 
-    #!java
+    :::java
     /** The job topic for dropbox job events. */
     public static final String JOB_TOPIC = "com/sling/eventing/dropbox/job";
 
 
 The **org.osgi.service.event.EventHandler#handleEvent(Event event)** method needs to be implemented:
 
-    #!java
+    :::java
     public void handleEvent(Event event) {
         if (EventUtil.isLocal(event)) {
             EventUtil.processJob(event, this);
@@ -96,7 +96,7 @@ Its logic is as follows:
 * * * A property for the file path.
 * * The job event is sent to all the listeners that subscribe to the topic of the event.
 
-    #!java
+    :::java
     public boolean process(Event event) {
     
         // get the resource event information
@@ -133,7 +133,7 @@ Now that you have implemented a service 
 
 To listen to the job events that have been defined before the property **event.topics** needs to be set to **mypackage.DropBoxService.JOB_TOPIC** in the class annotations:
 
-    #!java
+    :::java
     @Property(name="event.topics",
         value=mypackage.DropBoxService.JOB_TOPIC)
 
@@ -143,7 +143,7 @@ To listen to the job events that have be
 To move the files the service needs to implement the **org.osgi.service.event.EventHandler** and **org.apache.sling.event.JobProcessor** interfaces:
 
 
-    #!java
+    :::java
     public class DropBoxEventHandler implements JobProcessor, EventHandler {
 
 
@@ -153,7 +153,7 @@ Some class fields need to be defined:
 * The references to the SlingRepository and the JcrResourceResolverFactory services, which are used in the implementation.
 * The destination paths of the files.
 
-    #!java
+    :::java
     /** Default log. */
     protected final Logger log = LoggerFactory.getLogger(this.getClass());
 
@@ -171,12 +171,12 @@ Some class fields need to be defined:
     
 The **org.osgi.service.event.EventHandler#handleEvent(Event event)** method needs to be implemented:
 
-    #!java
-	public void handleEvent(Event event) {
-	    if (EventUtil.isLocal(event)) {
-	        EventUtil.processJob(event, this);
-	    }
-	}
+    :::java
+    public void handleEvent(Event event) {
+        if (EventUtil.isLocal(event)) {
+            EventUtil.processJob(event, this);
+        }
+    }
 
     
 The **org.apache.sling.event.JobProcessor#process(Event event)** method needs to be implemented.
@@ -190,45 +190,42 @@ Its logic is as follows:
 
 or in Java Code:
 
-    #!java
-	public boolean process(Event event) {
-		Session adminSession = null;
-		try {
-			String resourcePath = (String) event.getProperty("resourcePath");
-			String resourceName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
-        	adminSession = repository.loginAdministrative(null);
-	        ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);
-	        Resource res = resourceResolver.getResource(resourcePath);
-	        if (ResourceUtil.isA(res, "nt:file")) {
-	        	String mimeType = res.getResourceMetadata().getContentType();
-	        	String destDir;
-	        	if (mimeType.equals("image/png")) {
-	        		destDir = IMAGES_PATH;
-	        	}
-	        	else if (mimeType.equals("audio/mpeg")) {
-	        		destDir = MUSIC_PATH;
-	        	}
-	        	else if (mimeType.equals("video/x-msvideo")) {
-	        		destDir = MOVIES_PATH;
-	        	}
-	        	else {
-	        		destDir = OTHER_PATH;
-	        	}
-        		adminSession.move(resourcePath, destDir + resourceName);
-	        	adminSession.save();
-	        	log.info("The file {} has been moved to {}", resourceName, destDir);
-	        }
-	        return true;
-		} catch (RepositoryException e) {
-			log.error("RepositoryException: " + e);
-			return false;
+    :::java
+    public boolean process(Event event) {
+        Session adminSession = null;
+        try {
+            String resourcePath = (String) event.getProperty("resourcePath");
+            String resourceName = resourcePath.substring(resourcePath.lastIndexOf("/") + 1);
+            adminSession = repository.loginAdministrative(null);
+            ResourceResolver resourceResolver = resolverFactory.getResourceResolver(adminSession);
+            Resource res = resourceResolver.getResource(resourcePath);
+            if (ResourceUtil.isA(res, "nt:file")) {
+                String mimeType = res.getResourceMetadata().getContentType();
+                String destDir;
+                if (mimeType.equals("image/png")) {
+                    destDir = IMAGES_PATH;
+                } else if (mimeType.equals("audio/mpeg")) {
+                    destDir = MUSIC_PATH;
+                } else if (mimeType.equals("video/x-msvideo")) {
+                    destDir = MOVIES_PATH;
+                } else {
+                    destDir = OTHER_PATH;
+                }
+                adminSession.move(resourcePath, destDir + resourceName);
+                adminSession.save();
+                log.info("The file {} has been moved to {}", resourceName, destDir);
+            }
+            return true;
+        } catch (RepositoryException e) {
+            log.error("RepositoryException: " + e);
+            return false;
         } finally {
             if (adminSession != null && adminSession.isLive()) {
-            	adminSession.logout();
-            	adminSession = null;
+                adminSession.logout();
+                adminSession = null;
             }
         }
-	}
+    }
 
     
 The complete code for the **DropBoxEventHandler** service is available [here](DropBoxEventHandler.java).