cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From un...@apache.org
Subject svn commit: rev 54673 - in cocoon/branches/BRANCH_2_1_X/src/blocks/cron: conf java/org/apache/cocoon/components/cron
Date Tue, 12 Oct 2004 15:42:12 GMT
Author: unico
Date: Tue Oct 12 08:42:11 2004
New Revision: 54673

Added:
   cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobExecutor.java
   cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobScheduler.java
Modified:
   cocoon/branches/BRANCH_2_1_X/src/blocks/cron/conf/cron.xconf
   cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobExecutor.java
   cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java
Log:
Factor out Cocoon specific setup within executor to allow for the possibility 
of running Jobs ouside Cocoon environment (default remains like before)


Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/cron/conf/cron.xconf
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/cron/conf/cron.xconf	(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/cron/conf/cron.xconf	Tue Oct 12 08:42:11 2004
@@ -19,7 +19,7 @@
 <xconf xpath="/cocoon" unless="component[@role='org.apache.cocoon.components.cron.JobScheduler']">
 
     <component role="org.apache.cocoon.components.cron.JobScheduler"
-               class="org.apache.cocoon.components.cron.QuartzJobScheduler"
+               class="org.apache.cocoon.components.cron.CocoonQuartzJobScheduler"
                logger="cron">
       <!--+
           | Definitions for a thread pool used to schedule jobs

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobExecutor.java
==============================================================================
--- (empty file)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobExecutor.java
Tue Oct 12 08:42:11 2004
@@ -0,0 +1,80 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.cocoon.components.cron;
+
+import java.net.MalformedURLException;
+
+import org.apache.avalon.framework.component.WrapperComponentManager;
+import org.apache.avalon.framework.context.ContextException;
+import org.apache.avalon.framework.service.ServiceException;
+import org.apache.cocoon.Constants;
+import org.apache.cocoon.Processor;
+import org.apache.cocoon.components.CocoonComponentManager;
+import org.apache.cocoon.environment.background.BackgroundEnvironment;
+import org.quartz.JobDataMap;
+import org.quartz.JobExecutionException;
+
+/**
+ * This component is resposible to launch a {@link CronJob}s in a Quart Scheduler.
+ *
+ * @author <a href="mailto:giacomo@apache.org">Giacomo Pati</a>
+ * @version CVS $Id: QuartzJobExecutor.java 47533 2004-09-30 03:15:55Z vgritsenko $
+ *
+ * @since 2.1.1
+ */
+public class CocoonQuartzJobExecutor extends QuartzJobExecutor {
+
+    private Object m_key;
+    private BackgroundEnvironment m_env;
+    private Processor m_processor;
+
+    protected void setup(JobDataMap data) throws JobExecutionException {
+        super.setup(data);
+        org.apache.cocoon.environment.Context envContext;
+        try {
+            envContext =
+                (org.apache.cocoon.environment.Context) m_context.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
+        } catch (ContextException e) {
+        	throw new JobExecutionException(e);
+        }
+        
+        try {
+            m_env = new BackgroundEnvironment(m_logger, envContext);
+        } catch (MalformedURLException e) {
+            // Unlikely to happen
+            throw new JobExecutionException(e);
+        }
+
+        try {
+            m_processor = (Processor) m_manager.lookup(Processor.ROLE);
+        } catch (ServiceException e) {
+            throw new JobExecutionException(e);
+        }
+
+        m_key = CocoonComponentManager.startProcessing(m_env);
+        CocoonComponentManager.enterEnvironment(m_env, new WrapperComponentManager(m_manager),
m_processor);
+    }
+
+    protected void release(JobDataMap data) {
+        super.release(data);
+        CocoonComponentManager.leaveEnvironment();
+        CocoonComponentManager.endProcessing(m_env, m_key);
+        if (m_manager != null) {
+            m_manager.release(m_processor);
+        }
+    }
+
+}

Added: cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobScheduler.java
==============================================================================
--- (empty file)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/CocoonQuartzJobScheduler.java
Tue Oct 12 08:42:11 2004
@@ -0,0 +1,37 @@
+/*
+ * Copyright 1999-2004 The Apache Software Foundation.
+ *
+ * Licensed 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.cocoon.components.cron;
+
+import org.quartz.Job;
+import org.quartz.JobDataMap;
+import org.quartz.JobDetail;
+
+/**
+ * QuartzJobScheduler that runs jobs within a Cocoon Environment.
+ */
+public class CocoonQuartzJobScheduler extends QuartzJobScheduler {
+
+    protected JobDetail createJobDetail(String name, JobDataMap jobDataMap) {
+        JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, CocoonQuartzJobExecutor.class);
+        detail.setJobDataMap(jobDataMap);
+        return detail;
+    }
+
+    protected Job createJobExecutor() {
+        return new CocoonQuartzJobExecutor();
+    }
+
+}

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobExecutor.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobExecutor.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobExecutor.java
Tue Oct 12 08:42:11 2004
@@ -15,28 +15,18 @@
  */
 package org.apache.cocoon.components.cron;
 
-import org.apache.avalon.framework.component.WrapperComponentManager;
+import java.util.Map;
+
 import org.apache.avalon.framework.container.ContainerUtil;
 import org.apache.avalon.framework.context.Context;
-import org.apache.avalon.framework.context.ContextException;
 import org.apache.avalon.framework.logger.Logger;
 import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.avalon.framework.service.ServiceException;
 import org.apache.avalon.framework.service.ServiceManager;
-
-import org.apache.cocoon.Constants;
-import org.apache.cocoon.Processor;
-import org.apache.cocoon.components.CocoonComponentManager;
-import org.apache.cocoon.environment.background.BackgroundEnvironment;
-
 import org.quartz.Job;
 import org.quartz.JobDataMap;
 import org.quartz.JobExecutionContext;
 import org.quartz.JobExecutionException;
 
-import java.net.MalformedURLException;
-import java.util.Map;
-
 /**
  * This component is resposible to launch a {@link CronJob}s in a Quart Scheduler.
  *
@@ -47,116 +37,99 @@
  */
 public class QuartzJobExecutor implements Job {
 
+    protected Logger m_logger;
+    protected Context m_context;
+    protected ServiceManager m_manager;
+
     /* (non-Javadoc)
      * @see org.quartz.Job#execute(org.quartz.JobExecutionContext)
      */
-    public void execute(final JobExecutionContext context)
-    throws JobExecutionException {
+    public void execute(final JobExecutionContext context) throws JobExecutionException {
+
         final JobDataMap data = context.getJobDetail().getJobDataMap();
-        // data.put(QuartzJobScheduler.DATA_MAP_JOB_EXECUTION_CONTEXT, context);
 
-        final Logger logger = (Logger)data.get(QuartzJobScheduler.DATA_MAP_LOGGER);
-        final String name = (String)data.get(QuartzJobScheduler.DATA_MAP_NAME);
-        final Boolean canRunConcurrentlyB = ((Boolean)data.get(QuartzJobScheduler.DATA_MAP_RUN_CONCURRENT));
+        final String name = (String) data.get(QuartzJobScheduler.DATA_MAP_NAME);
+
+        m_logger = (Logger) data.get(QuartzJobScheduler.DATA_MAP_LOGGER);
+        m_context = (Context) data.get(QuartzJobScheduler.DATA_MAP_CONTEXT);
+        m_manager = (ServiceManager) data.get(QuartzJobScheduler.DATA_MAP_MANAGER);
+        
+        final Boolean canRunConcurrentlyB = ((Boolean) data.get(QuartzJobScheduler.DATA_MAP_RUN_CONCURRENT));
         final boolean canRunConcurrently = ((canRunConcurrentlyB == null) ? true : canRunConcurrentlyB.booleanValue());
 
         if (!canRunConcurrently) {
-            Boolean isRunning = (Boolean)data.get(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING);
+            Boolean isRunning = (Boolean) data.get(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING);
             if (Boolean.TRUE.equals(isRunning)) {
-                logger.warn("Cron job name '" + name +
+                m_logger.warn("Cron job name '" + name +
                             " already running but configured to not allow concurrent runs.
Will discard this scheduled run");
                 return;
             }
         }
 
-        if (logger.isInfoEnabled()) {
-            logger.info("Executing cron job named '" + name + "'");
-        }
-
-        Context appContext = (Context) data.get(QuartzJobScheduler.DATA_MAP_CONTEXT);
-        ServiceManager manager = (ServiceManager)data.get(QuartzJobScheduler.DATA_MAP_MANAGER);
-        org.apache.cocoon.environment.Context envContext;
-        try {
-            envContext =
-                (org.apache.cocoon.environment.Context) appContext.get(Constants.CONTEXT_ENVIRONMENT_CONTEXT);
-        } catch (ContextException e) {
-        	throw new JobExecutionException(e);
+        if (m_logger.isInfoEnabled()) {
+            m_logger.info("Executing cron job named '" + name + "'");
         }
 
-        BackgroundEnvironment env;
-        try {
-            env = new BackgroundEnvironment(logger, envContext);
-        } catch (MalformedURLException e) {
-            // Unlikely to happen
-            throw new JobExecutionException(e);
-        }
+        setup(data);
 
         Object job = null;
         String jobrole = null;
-
-        Processor processor;
-        try {
-            processor = (Processor)manager.lookup(Processor.ROLE);
-        } catch (ServiceException e) {
-            throw new JobExecutionException(e);
-        }
-
-        Object key = CocoonComponentManager.startProcessing(env);
-        CocoonComponentManager.enterEnvironment(env, new WrapperComponentManager(manager),
processor);
         boolean release = false;
         boolean dispose = false;
         try {
-            jobrole = (String)data.get(QuartzJobScheduler.DATA_MAP_ROLE);
+            jobrole = (String) data.get(QuartzJobScheduler.DATA_MAP_ROLE);
 
             if (null == jobrole) {
                 job = data.get(QuartzJobScheduler.DATA_MAP_OBJECT);
-                ContainerUtil.enableLogging(job, logger);
-                ContainerUtil.contextualize(job, appContext);
-                ContainerUtil.service(job, manager);
+                ContainerUtil.enableLogging(job, m_logger);
+                ContainerUtil.contextualize(job, m_context);
+                ContainerUtil.service(job, m_manager);
                 dispose = true;
             } else {
-                job = manager.lookup(jobrole);
+                job = m_manager.lookup(jobrole);
                 release = true;
             }
 
             if (job instanceof ConfigurableCronJob) {
-                final Parameters params = (Parameters)data.get(QuartzJobScheduler.DATA_MAP_PARAMETERS);
-                final Map objects = (Map)data.get(QuartzJobScheduler.DATA_MAP_OBJECTMAP);
-                ((ConfigurableCronJob)job).setup(params, objects);
+                final Parameters params = (Parameters) data.get(QuartzJobScheduler.DATA_MAP_PARAMETERS);
+                final Map objects = (Map) data.get(QuartzJobScheduler.DATA_MAP_OBJECTMAP);
+                ((ConfigurableCronJob) job).setup(params, objects);
             }
 
-            data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.TRUE);
-
             if (job instanceof Job) {
-                ((Job)job).execute(context);
+                ((Job) job).execute(context);
             } else if (job instanceof CronJob) {
-                ((CronJob)job).execute(name);
+                ((CronJob) job).execute(name);
             } else if (job instanceof Runnable) {
-                ((Runnable)job).run();
+                ((Runnable) job).run();
             } else {
-                logger.error("job named '" + name + "' is of invalid class: " + job.getClass().getName());
+                m_logger.error("job named '" + name + "' is of invalid class: " + job.getClass().getName());
             }
         } catch (final Throwable t) {
-            logger.error("Cron job name '" + name + "' died.", t);
+            m_logger.error("Cron job name '" + name + "' died.", t);
 
             if (t instanceof JobExecutionException) {
                 throw (JobExecutionException) t;
             }
         } finally {
-            data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.FALSE);
 
-            CocoonComponentManager.leaveEnvironment();
-            CocoonComponentManager.endProcessing(env, key);
+            release(data);
 
-            if (manager != null) {
-                manager.release(processor);
-                if (release) {
-                    manager.release(job);
-                }
+            if (m_manager != null && release) {
+                m_manager.release(job);
             }
             if (dispose) {
             	ContainerUtil.dispose(job);
             }
         }
     }
+
+    protected void setup(JobDataMap data) throws JobExecutionException {
+        data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.TRUE);
+    }
+
+    protected void release(JobDataMap data) {
+        data.put(QuartzJobScheduler.DATA_MAP_KEY_ISRUNNING, Boolean.FALSE);
+    }
+
 }

Modified: cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java
==============================================================================
--- cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java
(original)
+++ cocoon/branches/BRANCH_2_1_X/src/blocks/cron/java/org/apache/cocoon/components/cron/QuartzJobScheduler.java
Tue Oct 12 08:42:11 2004
@@ -546,16 +546,9 @@
         } catch (final SchedulerException ignored) {
         }
 
-        initDataMap(jobDataMap, name, canRunConcurrently);
-        if (null != params) {
-            jobDataMap.put(DATA_MAP_PARAMETERS, params);
-        }
-        if (null != objects) {
-            jobDataMap.put(DATA_MAP_OBJECTMAP, objects);
-        }
-
-        final JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, QuartzJobExecutor.class);
-        detail.setJobDataMap(jobDataMap);
+        initDataMap(jobDataMap, name, canRunConcurrently, params, objects);
+        
+        final JobDetail detail = createJobDetail(name, jobDataMap);
 
         if (getLogger().isInfoEnabled()) {
             getLogger().info("Adding CronJob '" + trigger.getFullName() + "'");
@@ -576,14 +569,27 @@
         }
     }
 
-    private JobDataMap initDataMap(JobDataMap jobDataMap, String jobName, boolean concurent)
{
+    protected JobDataMap initDataMap(JobDataMap jobDataMap, String jobName, boolean concurent,

+                                     Parameters params, Map objects) {
         jobDataMap.put(DATA_MAP_NAME, jobName);
         jobDataMap.put(DATA_MAP_LOGGER, getLogger());
         jobDataMap.put(DATA_MAP_CONTEXT, this.context);
         jobDataMap.put(DATA_MAP_MANAGER, this.manager);
         jobDataMap.put(DATA_MAP_RUN_CONCURRENT, concurent? Boolean.TRUE: Boolean.FALSE);
+        if (null != params) {
+            jobDataMap.put(DATA_MAP_PARAMETERS, params);
+        }
+        if (null != objects) {
+            jobDataMap.put(DATA_MAP_OBJECTMAP, objects);
+        }
         return jobDataMap;
     }
+    
+    protected JobDetail createJobDetail(String name, JobDataMap jobDataMap) {
+        final JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, QuartzJobExecutor.class);
+        detail.setJobDataMap(jobDataMap);
+        return detail;
+    }
 
     /**
      * Create a ThreadPool
@@ -762,14 +768,13 @@
             if (job instanceof CronJob) {
                 JobDataMap jobDataMap = new JobDataMap();
                 jobDataMap.put(DATA_MAP_OBJECT, job);
-                initDataMap(jobDataMap, name, true);
+                initDataMap(jobDataMap, name, true, null, null);
 
-                JobDetail detail = new JobDetail(name, DEFAULT_QUARTZ_JOB_GROUP, QuartzJobExecutor.class);
-                detail.setJobDataMap(jobDataMap);
+                final JobDetail detail = createJobDetail(name, jobDataMap);
 
                 TriggerFiredBundle trigger = new TriggerFiredBundle(detail, null, null, false,
null, null, null, null);
 
-                final QuartzJobExecutor executor = new QuartzJobExecutor();
+                final Job executor = createJobExecutor();
                 final JobExecutionContext context = new JobExecutionContext(this.scheduler,
trigger, executor);
 
                 this.executor.execute(new Runnable() {
@@ -797,6 +802,10 @@
         return false;
     }
 
+    protected Job createJobExecutor() {
+        return new QuartzJobExecutor();
+    }
+    
     /**
      * A ThreadPool for the Quartz Scheduler based on Doug Leas concurrency utilities PooledExecutor
      *

Mime
View raw message