package com.nhl.bootique.job.runtime;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import com.google.inject.Binder;
import com.google.inject.Provides;
import com.google.inject.multibindings.MapBinder;
import com.nhl.bootique.BQBinder;
import com.nhl.bootique.ConfigModule;
import com.nhl.bootique.config.ConfigurationFactory;
import com.nhl.bootique.env.Environment;
import com.nhl.bootique.job.Job;
import com.nhl.bootique.job.command.ExecCommand;
import com.nhl.bootique.job.command.ListCommand;
import com.nhl.bootique.job.command.ScheduleCommand;
import com.nhl.bootique.job.lock.LocalLockHandler;
import com.nhl.bootique.job.lock.LockHandler;
import com.nhl.bootique.job.lock.LockType;
import com.nhl.bootique.job.lock.zookeeper.ZkClusterLockHandler;
import com.nhl.bootique.job.scheduler.Scheduler;
import com.nhl.bootique.job.scheduler.SchedulerFactory;

public class JobModule extends ConfigModule {

	private Collection<Class<? extends Job>> jobTypes = new HashSet<>();

	public JobModule() {
	}

	public JobModule(String configPrefix) {
		super(configPrefix);
	}

	@Override
	protected String defaultConfigPrefix() {
		// main config sets up Scheduler , so renaming default config prefix
		return "scheduler";
	}

	@SafeVarargs
	public final JobModule jobs(Class<? extends Job>... jobTypes) {
		Arrays.asList(jobTypes).forEach(jt -> this.jobTypes.add(jt));
		return this;
	}

	@Override
	public void configure(Binder binder) {
		BQBinder.contributeTo(binder).commandTypes(ExecCommand.class, ListCommand.class, ScheduleCommand.class);

		JobBinder.contributeTo(binder).jobTypes(jobTypes);

		MapBinder<LockType, LockHandler> lockHandlers = MapBinder.newMapBinder(binder, LockType.class,
				LockHandler.class);
		lockHandlers.addBinding(LockType.local).to(LocalLockHandler.class);
		lockHandlers.addBinding(LockType.clustered).to(ZkClusterLockHandler.class);
	}

	@Provides
	public Scheduler createScheduler(Set<Job> jobs, Environment environment, Map<LockType, LockHandler> jobRunners,
			ConfigurationFactory configFactory) {
		return configFactory.config(SchedulerFactory.class, configPrefix).createScheduler(jobs, environment,
				configFactory, jobRunners);
	}
}

@@ -254,26 +254,114 @@
 ect.
-multibindings.MapB
+Singleton;%0Aimport com.google.inject.multibindings.MapBinder;%0Aimport com.google.inject.multibindings.Multib
 inde
@@ -389,22 +389,26 @@
 tique.BQ
-Binder
+CoreModule
 ;%0Aimport
@@ -431,32 +431,73 @@
 e.ConfigModule;%0A
+import com.nhl.bootique.command.Command;%0A
 import com.nhl.b
@@ -1650,16 +1650,60 @@
 %7B%0A%09%09
-BQBinder
+%0A%09%09Multibinder%3CCommand%3E commandBinder = BQCoreModule
 .con
@@ -1709,26 +1709,32 @@
 ntribute
-To
+Commands
 (binder)
 .command
@@ -1729,75 +1729,221 @@
 der)
-.
+;%0A%09%09
 command
-Types(ExecCommand.class, ListCommand.class, ScheduleCommand
+Binder.addBinding().to(ExecCommand.class).in(Singleton.class);%0A%09%09commandBinder.addBinding().to(ListCommand.class).in(Singleton.class);%0A%09%09commandBinder.addBinding().to(ScheduleCommand.class).in(Singleton
 .cla

Upgrade Bootique to 0.12 #1