Commits

timemachine committed b7aa2f5

Clean up services.

Comments (0)

Files changed (6)

src/main/java/timemachine/scheduler/service/JobListenerNotifier.java

 	private static Logger logger = LoggerFactory.getLogger(JobListenerNotifier.class);
 	private List<JobListener> jobListeners = new ArrayList<JobListener>();
 	
+	public void setJobListeners(List<JobListener> jobListeners) {
+		this.jobListeners = jobListeners;
+	}
+	
 	public void addJobListener(JobListener jobListener) {
 		jobListeners.add(jobListener);
 	}

src/main/java/timemachine/scheduler/service/MemoryDataStore.java

 	private Map<Long, JobDef> jobStore = new HashMap<Long, JobDef>();
 	private Map<Long, SchedulerInfo> schedulerInfoStore = new HashMap<Long, SchedulerInfo>();
 	private ScheduleStore scheduleStore = new ScheduleStore();
-	private JobListenerNotifier jobListenerNotifierService;
+	private JobListenerNotifier jobListenerNotifier;
 
-	public void setJobListenerNotifier(JobListenerNotifier jobListenerNotifierService) {
-		this.jobListenerNotifierService = jobListenerNotifierService;
+	public void setJobListenerNotifier(JobListenerNotifier jobListenerNotifier) {
+		this.jobListenerNotifier = jobListenerNotifier;
 	}
 
 	public void setDataIdGenerator(IdGenerator dataIdGenerator) {
 		logger.debug("New {} stored.", jobDef);
 		for (Schedule schedule : jobDef.getSchedules())
 			storeSchedule(schedule);
-		jobListenerNotifierService.onJobDefAdded(jobDef);
+		jobListenerNotifier.onJobDefAdded(jobDef);
 	}
 
 	synchronized private void storeSchedule(Schedule schedule) {
 		scheduleStore.add(schedule);
 		logger.debug("New {} stored for {}, taskClass={}, nextRun={}, desc={}",
 				new Object[] { schedule, jobDef, jobDef.getJobTaskClassName(), schedule.getNextRun(), schedule.getDesc() });
-		jobListenerNotifierService.onScheduleAdded(schedule);
+		jobListenerNotifier.onScheduleAdded(schedule);
 	}
 
 	synchronized private void deleteSchedule(Schedule schedule) {
 		scheduleStore.remove(schedule);
 		logger.debug("{} deleted", schedule);
-		jobListenerNotifierService.onScheduleDeleted(schedule);
+		jobListenerNotifier.onScheduleDeleted(schedule);
 	}
 
 	@Override
 			deleteSchedule(schedule);
 		}
 		jobStore.remove(jobDef.getId());
-		jobListenerNotifierService.onJobDefDeleted(jobDef);
+		jobListenerNotifier.onJobDefDeleted(jobDef);
 		logger.debug("{} deleted", jobDef);
 	}
 
 			scheduleStore.stage(schedule);
 			Date missedRun = schedule.getNextRun();
 			schedule.onMissedRun();
-			jobListenerNotifierService.onJobMissedRun(schedule);
+			jobListenerNotifier.onJobMissedRun(schedule);
 			logger.debug("{} missed run: {}, nextRun: {}", new Object[] { schedule, missedRun, schedule.getNextRun() });
 			scheduleStore.unstage(schedule);
 		}

src/main/java/timemachine/scheduler/service/PollingScheduleRunner.java

 	private ThreadPool jobTaskThreadPool;
 	private Scheduler scheduler;
 	private AtomicBoolean paused = new AtomicBoolean(false);
-	private JobListenerNotifier jobListenerNotifierService;
+	private JobListenerNotifier jobListenerNotifier;
 	private long pollingInterval; // In millis sec
 	private int maxSchedulesPerInterval;
 	
 			logger.debug("Creating jobTask instance " + className);
 			try {
 				JobTask jobTask = jobTaskFactory.createJobTaskInstance(className);			
-				jobListenerNotifierService.onJobRunBefore(jobContext);
+				jobListenerNotifier.onJobRunBefore(jobContext);
 
 				logger.debug("Updating scheduler before run of {}", jobContext);
 				dataStore.updateScheduleBeforeRun(scheduler, schedule);
 				jobTask.run(jobContext);
-				jobListenerNotifierService.onJobRunAfter(jobContext);
+				jobListenerNotifier.onJobRunAfter(jobContext);
 				logger.debug("JobDef id={} with {} ran sucessful.", jobDef.getId(), schedule);
 			} catch (Exception e) {
 				logger.error("Failed to run jobDef {}", jobDef, e);
-				jobListenerNotifierService.onJobRunException(jobContext, e);
+				jobListenerNotifier.onJobRunException(jobContext, e);
 			}
 
 			logger.debug("Updating scheduler after run of {}", jobContext);
 		this.pollingInterval = pollingInterval;
 	}
 
-	public void setJobListenerNotifier(JobListenerNotifier jobListenerNotifierService) {
-		this.jobListenerNotifierService = jobListenerNotifierService;
+	public void setJobListenerNotifier(JobListenerNotifier jobListenerNotifier) {
+		this.jobListenerNotifier = jobListenerNotifier;
 	}
 	
 	@Override

src/main/java/timemachine/scheduler/service/SchedulerEngine.java

 
 	@Override
 	public boolean isPaused() {
+		ensureInited();
 		return scheduleRunnerService.isPaused();
 	}
 
 
 	@Override
 	public int getJobDefCount() {
+		ensureInited();
 		return dataStoreService.getCount(JobDef.class);
 	}
 
 	@Override
 	public int getScheduleCount() {
+		ensureInited();
 		return dataStoreService.getCount(Schedule.class);
 	}
 
 	@Override
 	public List<JobDef> findAllJobDefs() {
+		ensureInited();
 		return dataStoreService.findAll(JobDef.class);
 	}
 
 	@Override
 	public List<JobDef> findJobDefsByName(String searchName) {
+		ensureInited();
 		return dataStoreService.findJobsByName(searchName);
 	}
 
 	@Override
 	public List<Schedule> findSchedulesByState(State state) {
+		ensureInited();
 		return dataStoreService.findSchedulesByState(state);
 	}
 
 	/** The order of the schedules returned not ordered. */
 	@Override
 	public List<Schedule> findAllSchedules() {
+		ensureInited();
 		return dataStoreService.findAll(Schedule.class);
 	}
 
 	@Override
 	public List<Schedule> findSchedulesByName(String searchName) {
+		ensureInited();
 		return dataStoreService.findSchedulesByName(searchName);
 	}
 

src/main/java/timemachine/scheduler/service/ScriptingService.java

 import org.slf4j.LoggerFactory;
 
 import timemachine.scheduler.ConfigPropsListener;
+import timemachine.scheduler.CoreServicesListener;
 import timemachine.scheduler.Props;
 import timemachine.scheduler.Scheduler;
 import timemachine.scheduler.SchedulerException;
  * </pre>
  * @author Zemian Deng <saltnlight5@gmail.com>
  */
-public class ScriptingService extends AbstractService implements SchedulerListener, ConfigPropsListener {
+public class ScriptingService extends AbstractService implements SchedulerListener, ConfigPropsListener,
+		CoreServicesListener {
 	private static final Logger logger = LoggerFactory.getLogger(ScriptingService.class);
 
 	private ScriptEngine scriptEngine;
 	private Scheduler scheduler;
 	private Props configProps;
-	private DataStore dataStore;
-	private ThreadPool threadPool;
-	private ScheduleRunner scheduleRunner;
+	private CoreServices coreServices;
 
 	private String scriptEngineName;
 	private String initScript;
 		bindings.put("scriptingService", this);
 		bindings.put("scheduler", scheduler);
 		bindings.put("configProps", configProps);
-		bindings.put("dataStore", dataStore);
-		bindings.put("threadPool", threadPool);
-		bindings.put("scheduleRunner", scheduleRunner);
+		bindings.put("coreServices", coreServices);
+
 		bindings.put("initScript", initScript);
 		bindings.put("startScript", startScript);
 		bindings.put("stopScript", stopScript);
 	public void onConfigProps(Props configProps) {
 		this.configProps = configProps;
 	}
+
+	@Override
+	public void onCoreServices(CoreServices coreServices) {
+		this.coreServices = coreServices;
+	}
 }

src/main/java/timemachine/scheduler/service/SimpleClassLoaderService.java

 public class SimpleClassLoaderService extends AbstractService implements ClassLoaderService, SystemService {
 	private ClassLoader classLoader;
 	
+	public void setClassLoader(ClassLoader classLoader) {
+		this.classLoader = classLoader;
+	}
+	
 	public SimpleClassLoaderService() {
 		classLoader = getClass().getClassLoader();
 	}