You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2007/09/26 17:49:39 UTC

svn commit: r579701 - in /incubator/sling/trunk/event: pom.xml src/main/java/org/apache/sling/event/EventUtil.java src/main/java/org/apache/sling/event/impl/TimedEventHandler.java

Author: cziegeler
Date: Wed Sep 26 08:49:26 2007
New Revision: 579701

URL: http://svn.apache.org/viewvc?rev=579701&view=rev
Log:
Events can be scheduled by using a cron config, a period or a fixed date.

Modified:
    incubator/sling/trunk/event/pom.xml
    incubator/sling/trunk/event/src/main/java/org/apache/sling/event/EventUtil.java
    incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java

Modified: incubator/sling/trunk/event/pom.xml
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/pom.xml?rev=579701&r1=579700&r2=579701&view=diff
==============================================================================
--- incubator/sling/trunk/event/pom.xml (original)
+++ incubator/sling/trunk/event/pom.xml Wed Sep 26 08:49:26 2007
@@ -102,5 +102,10 @@
             <artifactId>sling-jackrabbit-api</artifactId>
             <version>2.0.0-incubator-SNAPSHOT</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.sling</groupId>
+            <artifactId>sling-scheduler</artifactId>
+            <version>2.0.0-incubator-SNAPSHOT</version>
+        </dependency>
     </dependencies>
 </project>

Modified: incubator/sling/trunk/event/src/main/java/org/apache/sling/event/EventUtil.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/src/main/java/org/apache/sling/event/EventUtil.java?rev=579701&r1=579700&r2=579701&view=diff
==============================================================================
--- incubator/sling/trunk/event/src/main/java/org/apache/sling/event/EventUtil.java (original)
+++ incubator/sling/trunk/event/src/main/java/org/apache/sling/event/EventUtil.java Wed Sep 26 08:49:26 2007
@@ -55,6 +55,15 @@
     /** The real topic of the event. */
     public static final String PROPERTY_TIMED_EVENT_TOPIC = "event.topic.timed";
 
+    /** The scheduler expression for the timed event. */
+    public static final String PROPERTY_TIMED_EVENT_SCHEDULE = "event.topic.scheduler";
+
+    /** The period for the timed event. */
+    public static final String PROPERTY_TIMED_EVENT_PERIOD = "event.topic.period";
+
+    /** The date for the timed event. */
+    public static final String PROPERTY_TIMED_EVENT_DATE = "event.topic.date";
+
     /**
      * Create a distributable event.
      * A distributable event is distributed across the cluster.

Modified: incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java?rev=579701&r1=579700&r2=579701&view=diff
==============================================================================
--- incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java (original)
+++ incubator/sling/trunk/event/src/main/java/org/apache/sling/event/impl/TimedEventHandler.java Wed Sep 26 08:49:26 2007
@@ -18,6 +18,13 @@
  */
 package org.apache.sling.event.impl;
 
+import java.io.Serializable;
+import java.util.Date;
+import java.util.Dictionary;
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
 import javax.jcr.RepositoryException;
@@ -26,20 +33,31 @@
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 
+import org.apache.sling.event.EventUtil;
+import org.apache.sling.scheduler.Job;
+import org.apache.sling.scheduler.JobContext;
+import org.apache.sling.scheduler.Scheduler;
 import org.osgi.service.component.ComponentContext;
 import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
 
 
 /**
  * An event handler for timed events.
  *
- * scr.component inherit="true"
+ * @scr.component inherit="true"
  * @scr.property name="event.topics" value="org/apache/sling/event/timed"
  * @scr.property name="repository.path" value="/sling/timed-events"
  */
 public abstract class TimedEventHandler
-    extends AbstractRepositoryEventHandler{
+    extends AbstractRepositoryEventHandler
+    implements Job {
+
+    /** @scr.reference */
+    protected Scheduler scheduler;
 
+    /** @scr.reference */
+    protected EventAdmin eventAdmin;
 
     /**
      * @see org.apache.sling.event.impl.AbstractRepositoryEventHandler#activate(org.osgi.service.component.ComponentContext)
@@ -48,7 +66,7 @@
     throws RepositoryException {
         super.activate(context);
         // load timed events from repository
-        this.loadEvents();
+        //this.loadEvents();
     }
 
     /**
@@ -95,7 +113,7 @@
                             // lock failed which means that the node is locked by someone else, so we don't have to requeue
                         }
                         if ( lock != null ) {
-                            this.processEvent(info.event, eventNode);
+                            this.processEvent(info.event);
                         }
                     }
                 } catch (RepositoryException e) {
@@ -106,8 +124,52 @@
         }
     }
 
-    protected void processEvent(Event event, Node eventNode) {
-        // TODO
+    protected boolean processEvent(Event event) {
+        if ( this.scheduler != null ) {
+            final Map<String, Serializable> config = new HashMap<String, Serializable>();
+            try {
+                final Hashtable properties = new Hashtable();
+                config.put("topic", (String)event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_TOPIC));
+                final String[] names = event.getPropertyNames();
+                if ( names != null ) {
+                    for(int i=0; i<names.length; i++) {
+                        properties.put(names[i], event.getProperty(names[i]));
+                    }
+                }
+                config.put("config", properties);
+                // if the event contains a job id we'll use that as the name
+                String jobName = null;
+                if ( event.getProperty(EventUtil.PROPERTY_JOB_ID) != null ) {
+                    jobName = "Timed job " + event.getProperty(EventUtil.PROPERTY_JOB_ID);
+                }
+                // first, check for expression
+                final String expression = (String) event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_SCHEDULE);
+                if ( expression != null ) {
+                    this.scheduler.addJob(jobName, this, config, expression, false);
+                } else {
+                    // check for period next
+                    final Long period = (Long) event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_PERIOD);
+                    if ( period != null ) {
+                        this.scheduler.addPeriodicJob(jobName, this, config, period, false);
+                    } else {
+                        // then we check for a fixed date
+                        final Date date = (Date) event.getProperty(EventUtil.PROPERTY_TIMED_EVENT_DATE);
+                        if ( date != null ) {
+                            this.scheduler.fireJobAt(jobName, this, config, date);
+                        } else {
+                            // no information, so fire the job once now
+                            this.scheduler.fireJob(this, config);
+                            return true;
+                        }
+                    }
+                }
+            } catch (Exception e) {
+                this.ignoreException(e);
+            }
+        } else {
+            this.logger.error("No scheduler available to start timed event " + event);
+        }
+        return false;
     }
 
     public void onEvent(EventIterator events) {
@@ -115,9 +177,25 @@
 
     }
 
+    /**
+     * @see org.osgi.service.event.EventHandler#handleEvent(org.osgi.service.event.Event)
+     */
     public void handleEvent(Event event) {
-        // TODO Auto-generated method stub
+        this.processEvent(event);
 
+    }
+
+    /**
+     * @see org.apache.sling.scheduler.Job#execute(org.apache.sling.scheduler.JobContext)
+     */
+    public void execute(JobContext context) {
+        final String topic = (String) context.getConfiguration().get("topic");
+        final Dictionary properties = (Dictionary) context.getConfiguration().get("config");
+        if ( this.eventAdmin != null ) {
+            this.eventAdmin.postEvent(new Event(topic, properties));
+        } else {
+            this.logger.warn("Unable to send timed event as no event admin service is available.");
+        }
     }
 
     /**