}
public void execute(JobExecutionContext ctx) throws JobExecutionException {
JobDetail jobDetail = ctx.getJobDetail();
JobDataMap data = jobDetail.getJobDataMap();
final JCRSessionFactory sessionFactory = JCRSessionFactory.getInstance();
try {
String userKey = data.getString(JOB_USERKEY);
if ((userKey!= null) && (!userKey.equals(JahiaLoginModule.SYSTEM))) {
JahiaUser user = ServicesRegistry.getInstance().getJahiaUserManagerService().lookupUserByKey((String) data.get(JOB_USERKEY));
if (user != null) {
sessionFactory.setCurrentUser(user);
logger.debug("Executing job as user {}", userKey);
} else {
logger.warn("Unable to lookup job user for key {}", userKey);
}
} else {
logger.debug("Executing job as system user");
}
String langKey = data.getString(JOB_CURRENT_LOCALE);
if (langKey != null) {
sessionFactory.setCurrentLocale(LanguageCodeConverters.languageCodeToLocale(langKey));
logger.debug("Executing job with locale {}", langKey);
}
// do execute job
executeJahiaJob(ctx);
} catch (Exception e) {
logger.error("Error executing job " + jobDetail.getKey(), e);
throw new JobExecutionException(e);
} finally {
try {
this.postExecution(ctx);
} finally {
sessionFactory.setCurrentUser(null);
sessionFactory.setCurrentLocale(null);
sessionFactory.closeAllSessions();
}
}
}