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 07:58:27 UTC

svn commit: r579482 - in /incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler: EventJob.java Job.java JobContext.java Scheduler.java impl/QuartzJobExecutor.java impl/QuartzScheduler.java

Author: cziegeler
Date: Tue Sep 25 22:58:26 2007
New Revision: 579482

URL: http://svn.apache.org/viewvc?rev=579482&view=rev
Log:
Refactor new job interface to get a job context (which allows us to extend it over time)
Fix scheduler and finish event job implementation.

Added:
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java   (with props)
Modified:
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java
    incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java

Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java?rev=579482&r1=579481&r2=579482&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/EventJob.java Tue Sep 25 22:58:26 2007
@@ -18,10 +18,13 @@
 
 import java.util.Dictionary;
 
+import org.osgi.service.event.Event;
+import org.osgi.service.event.EventAdmin;
+
 /**
  * This is an utility implementation for sending timed events.
  */
-public class EventJob implements Runnable {
+public class EventJob implements Job {
 
     protected final String eventTopic;
 
@@ -33,10 +36,11 @@
     }
 
     /**
-     * @see java.lang.Runnable#run()
+     * @see org.apache.sling.scheduler.Job#execute(org.apache.sling.scheduler.JobContext)
      */
-    public void run() {
-        // TODO Auto-generated method stub
-
+    public void execute(JobContext context) {
+        final EventAdmin eventAdmin = (EventAdmin) context.getServiceLocator().getService(EventAdmin.class.getName());
+        final Event event = new Event(this.eventTopic, this.eventProperties);
+        eventAdmin.sendEvent(event);
     }
 }

Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java?rev=579482&r1=579481&r2=579482&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Job.java Tue Sep 25 22:58:26 2007
@@ -16,13 +16,8 @@
  */
 package org.apache.sling.scheduler;
 
-import java.util.Map;
-
-import org.apache.sling.core.ServiceLocator;
 
 public interface Job {
 
-    void execute(String         name,
-                 Map            configuration,
-                 ServiceLocator locator);
+    void execute(JobContext context);
 }

Added: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java?rev=579482&view=auto
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java (added)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java Tue Sep 25 22:58:26 2007
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sling.scheduler;
+
+import java.io.Serializable;
+import java.util.Map;
+
+import org.apache.sling.core.ServiceLocator;
+
+public interface JobContext {
+
+    /**
+     * Get the name of the scheduled job.
+     * @return
+     */
+    String getName();
+
+    Map<String, Serializable> getConfiguration();
+
+    /**
+     * Get the service locator.
+     * @return
+     */
+    ServiceLocator getServiceLocator();
+}

Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/JobContext.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision url

Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java?rev=579482&r1=579481&r2=579482&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/Scheduler.java Tue Sep 25 22:58:26 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.scheduler;
 
+import java.io.Serializable;
 import java.util.Date;
 import java.util.Map;
 import java.util.NoSuchElementException;
@@ -39,7 +40,7 @@
      * @param schedulingExpression The time specification using a scheduling expression.
      * @param canRunConcurrently Whether this job can run even if previous scheduled runs are still running.
      */
-    void addJob(String name, Object job, Map<Object, Object> config, String schedulingExpression, boolean canRunConcurrently)
+    void addJob(String name, Object job, Map<String, Serializable> config, String schedulingExpression, boolean canRunConcurrently)
     throws Exception;
 
     /**
@@ -54,7 +55,7 @@
      * @param period Every period seconds this job is started.
      * @param canRunConcurrently Whether this job can run even if previous scheduled runs are still running.
      */
-    void addPeriodicJob(String name, Object job, Map<Object, Object> config, long period, boolean canRunConcurrently)
+    void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently)
     throws Exception;
 
     /**
@@ -63,7 +64,7 @@
      * @param job The job to execute (either {@link Job} or {@link Runnable}).
      * @param config An optional configuration object - this configuration is only passed to the job the job implements {@link Job}.
      */
-    void fireJob(Object job, Map<Object, Object> config)
+    void fireJob(Object job, Map<String, Serializable> config)
     throws Exception;
 
     /**
@@ -76,7 +77,7 @@
      * @param config An optional configuration object - this configuration is only passed to the job the job implements {@link Job}.
      * @param date The date this job should be run.
      */
-    void fireJobAt(String name, Object job, Map<Object, Object> config, Date date)
+    void fireJobAt(String name, Object job, Map<String, Serializable> config, Date date)
     throws Exception;
 
     /**

Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java?rev=579482&r1=579481&r2=579482&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzJobExecutor.java Tue Sep 25 22:58:26 2007
@@ -16,8 +16,13 @@
  */
 package org.apache.sling.scheduler.impl;
 
+import java.io.Serializable;
 import java.util.Map;
 
+import org.apache.sling.core.ServiceLocator;
+import org.apache.sling.core.util.ServiceLocatorImpl;
+import org.apache.sling.scheduler.JobContext;
+import org.osgi.framework.BundleContext;
 import org.quartz.Job;
 import org.quartz.JobDataMap;
 import org.quartz.JobExecutionContext;
@@ -49,12 +54,23 @@
 
         this.setup(data);
 
+
+        final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
+
         try {
-            final Object job = data.get(QuartzScheduler.DATA_MAP_OBJECT);
-            final Map<Object, Object> configuration = (Map<Object, Object>) data.get(QuartzScheduler.DATA_MAP_CONFIGURATION);
-            final String name = (String) data.get(QuartzScheduler.DATA_MAP_NAME);
             if (job instanceof org.apache.sling.scheduler.Job) {
-                ((org.apache.sling.scheduler.Job) job).execute(name, configuration, null);
+                final BundleContext bundleContext = (BundleContext)data.get(QuartzScheduler.DATA_MAP_BUNDLE_CONTEXT);
+                final ServiceLocatorImpl serviceLocator = new ServiceLocatorImpl(bundleContext);
+
+                final Map<String, Serializable> configuration = (Map<String, Serializable>) data.get(QuartzScheduler.DATA_MAP_CONFIGURATION);
+                final String name = (String) data.get(QuartzScheduler.DATA_MAP_NAME);
+
+                try {
+                    final JobContext jobCtx = new JobContextImpl(name, configuration, serviceLocator);
+                    ((org.apache.sling.scheduler.Job) job).execute(jobCtx);
+                } finally {
+                    serviceLocator.clear();
+                }
             } else if (job instanceof Runnable) {
                 ((Runnable) job).run();
             }
@@ -77,4 +93,37 @@
         data.put(QuartzScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.FALSE);
     }
 
+    public static final class JobContextImpl implements JobContext {
+
+        protected final Map<String, Serializable> configuration;
+        protected final String name;
+        protected final ServiceLocator serviceLocator;
+
+        public JobContextImpl(String name, Map<String, Serializable> config, ServiceLocator locator) {
+            this.name = name;
+            this.configuration = config;
+            this.serviceLocator = locator;
+        }
+
+        /**
+         * @see org.apache.sling.scheduler.JobContext#getConfiguration()
+         */
+        public Map<String, Serializable> getConfiguration() {
+            return this.configuration;
+        }
+
+        /**
+         * @see org.apache.sling.scheduler.JobContext#getName()
+         */
+        public String getName() {
+            return this.name;
+        }
+
+        /**
+         * @see org.apache.sling.scheduler.JobContext#getServiceLocator()
+         */
+        public ServiceLocator getServiceLocator() {
+            return this.serviceLocator;
+        }
+    }
 }

Modified: incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java
URL: http://svn.apache.org/viewvc/incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java?rev=579482&r1=579481&r2=579482&view=diff
==============================================================================
--- incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java (original)
+++ incubator/sling/trunk/scheduler/src/main/java/org/apache/sling/scheduler/impl/QuartzScheduler.java Tue Sep 25 22:58:26 2007
@@ -16,6 +16,7 @@
  */
 package org.apache.sling.scheduler.impl;
 
+import java.io.Serializable;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.Date;
@@ -70,6 +71,9 @@
     /** Map key for the configuration. */
     static final String DATA_MAP_CONFIGURATION = "QuartzJobScheduler.Configuration";
 
+    /** Map key for the bundle context. */
+    static final String DATA_MAP_BUNDLE_CONTEXT = "QuartzJobScheduler.BundleContext";
+
     protected org.quartz.Scheduler scheduler;
 
     protected final List<Object[]> registeredJobs = new ArrayList<Object[]>();
@@ -146,7 +150,7 @@
      */
     protected void scheduleJob(String name,
                                final Object job,
-                               final Map<Object, Object>    config,
+                               final Map<String, Serializable>    config,
                                final Trigger trigger,
                                final boolean canRunConcurrently)
     throws Exception {
@@ -184,7 +188,7 @@
      */
     protected JobDataMap initDataMap(String  jobName,
                                      Object  job,
-                                     Map<Object, Object> config,
+                                     Map<String, Serializable> config,
                                      boolean concurent) {
         final JobDataMap jobDataMap = new JobDataMap();
 
@@ -226,7 +230,7 @@
      */
     public void addJob(String name,
                        Object job,
-                       Map<Object, Object>    config,
+                       Map<String, Serializable>    config,
                        String schedulingExpression,
                        boolean canRunConcurrently)
     throws Exception {
@@ -243,7 +247,7 @@
     /**
      * @see org.apache.sling.core.scheduler.Scheduler#addPeriodicJob(java.lang.String, java.lang.Object, java.util.Map, long, boolean)
      */
-    public void addPeriodicJob(String name, Object job, Map<Object, Object> config, long period, boolean canRunConcurrently)
+    public void addPeriodicJob(String name, Object job, Map<String, Serializable> config, long period, boolean canRunConcurrently)
     throws Exception {
         final long ms = period * 1000;
         if ( name == null ) {
@@ -259,7 +263,7 @@
     /**
      * @see org.apache.sling.core.scheduler.Scheduler#fireJob(java.lang.Object, java.util.Map)
      */
-    public void fireJob(Object job, Map<Object, Object> config)
+    public void fireJob(Object job, Map<String, Serializable> config)
     throws Exception {
         this.checkJob(job);
         final String name = job.getClass().getName();
@@ -274,7 +278,7 @@
     /**
      * @see org.apache.sling.core.scheduler.Scheduler#fireJobAt(java.lang.String, java.lang.Object, java.util.Map, java.util.Date)
      */
-    public void fireJobAt(String name, Object job, Map<Object, Object> config, Date date) throws Exception {
+    public void fireJobAt(String name, Object job, Map<String, Serializable> config, Date date) throws Exception {
         if ( name == null ) {
             name = "Sling Quartz Scheduler " + UUID.randomUUID().toString();
         }