}
Object job = null;
String jobrole = null;
ServiceManager manager = (ServiceManager)data.get(QuartzJobScheduler.DATA_MAP_MANAGER);
org.apache.cocoon.environment.Context envContext =
(org.apache.cocoon.environment.Context)data.get(QuartzJobScheduler.DATA_MAP_ENV_CONTEXT);
BackgroundEnvironment env;
try {
env = new BackgroundEnvironment(logger, envContext, manager);
} catch (MalformedURLException mue) {
// Unlikely to happen
throw new JobExecutionException(mue);
}
CocoonComponentManager.enterEnvironment(env, env.getManager(), env.getProcessor());
boolean release = false;
try {
jobrole = (String)data.get(QuartzJobScheduler.DATA_MAP_ROLE);
if (null == jobrole) {
job = data.get(QuartzJobScheduler.DATA_MAP_OBJECT);
} else {
job = 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);
}
data.put(DATA_MAP_KEY_ISRUNNING, Boolean.TRUE);
if (job instanceof Job) {
((Job)job).execute(context);
} else if (job instanceof CronJob) {
((CronJob)job).execute(name);
} else if (job instanceof Runnable) {
((Runnable)job).run();
} else {
logger.error("job named '" + name + "' is of invalid class: " + job.getClass().getName());
}
} catch (final Throwable t) {
logger.error("Cron job name '" + name + " died.", t);
if (t instanceof JobExecutionException) {
throw (JobExecutionException)t;
}
} finally {
data.put(DATA_MAP_KEY_ISRUNNING, Boolean.FALSE);
CocoonComponentManager.leaveEnvironment();
if (release && null != manager) {
manager.release(job);
}
}
}