01: package org.theospi.portfolio.admin.service;
02:
03: import org.quartz.*;
04: import org.sakaiproject.api.app.scheduler.SchedulerManager;
05: import org.sakaiproject.component.cover.ComponentManager;
06: import org.sakaiproject.tool.api.Session;
07: import org.sakaiproject.tool.cover.SessionManager;
08: import org.theospi.portfolio.admin.intf.SakaiIntegrationPlugin;
09: import org.theospi.portfolio.admin.model.IntegrationOption;
10:
11: import java.util.Date;
12: import java.util.Iterator;
13: import java.util.Map;
14:
15: /**
16: * Created by IntelliJ IDEA.
17: * User: John Ellis
18: * Date: Mar 2, 2006
19: * Time: 10:52:35 PM
20: * To change this template use File | Settings | File Templates.
21: */
22: public class PluginOptionJob implements Job {
23:
24: public final static String PLUGIN = "org.theospi.portfolio.admin.service.PluginOptionJob.job";
25: public final static String INTERVAL = "org.theospi.portfolio.admin.service.PluginOptionJob.interval";
26:
27: public void execute(JobExecutionContext jobExecutionContext)
28: throws JobExecutionException {
29: Session sakaiSession = SessionManager.getCurrentSession();
30: String userId = sakaiSession.getUserId();
31:
32: try {
33: sakaiSession.setUserId("admin");
34: sakaiSession.setUserEid("admin");
35: Map details = jobExecutionContext.getJobDetail()
36: .getJobDataMap();
37: SchedulerManager manager = (SchedulerManager) ComponentManager
38: .get(SchedulerManager.class);
39: String pluginId = (String) details.get(PLUGIN);
40: SakaiIntegrationPlugin plugin = (SakaiIntegrationPlugin) ComponentManager
41: .get(pluginId);
42: long millis = Long
43: .parseLong((String) details.get(INTERVAL));
44:
45: boolean done = true;
46: for (Iterator i = plugin.getPotentialIntegrations()
47: .iterator(); i.hasNext();) {
48: if (!plugin.executeOption((IntegrationOption) i.next())) {
49: done = false;
50: break;
51: }
52: }
53:
54: if (!done) {
55: try {
56: schedule(manager, pluginId, millis);
57: } catch (SchedulerException e) {
58: throw new JobExecutionException(e);
59: }
60: }
61: } finally {
62: sakaiSession.setUserEid(userId);
63: sakaiSession.setUserId(userId);
64: }
65: }
66:
67: public static void schedule(SchedulerManager manager,
68: String pluginId, long millis) throws SchedulerException {
69: JobDetail detail = new JobDetail(truncateTriggerName(pluginId
70: + "." + System.currentTimeMillis()),
71: truncateTriggerName(PluginOptionJob.class.toString()),
72: PluginOptionJob.class);
73: detail.getJobDataMap().put(PLUGIN, pluginId);
74: detail.getJobDataMap().put(INTERVAL, millis + "");
75: manager.getScheduler().scheduleJob(
76: detail,
77: new SimpleTrigger(truncateTriggerName(pluginId
78: + ".trigger." + System.currentTimeMillis()),
79: PluginOptionJob.class.toString(), new Date(
80: System.currentTimeMillis() + millis)));
81: }
82:
83: protected static String truncateTriggerName(String s) {
84: if (s.length() > 80) {
85: return s.substring(s.length() - 80);
86: }
87: return s;
88: }
89: }
|