1e613c58b068c32d97500009699b2316007829dc
[vid.git] / vid-app-common / src / main / java / org / onap / vid / job / command / JobCommandFactory.java
1 package org.onap.vid.job.command;
2
3 import org.onap.vid.exceptions.GenericUncheckedException;
4 import org.onap.vid.job.Job;
5 import org.onap.vid.job.JobCommand;
6 import org.springframework.context.ApplicationContext;
7 import org.springframework.stereotype.Component;
8
9 import javax.inject.Inject;
10 import java.util.function.Function;
11
12 @Component
13 public class JobCommandFactory {
14
15     final Function<Class<? extends JobCommand>, JobCommand> jobFactory;
16
17     @Inject
18     public JobCommandFactory(ApplicationContext applicationContext) {
19         this.jobFactory = (jobType -> {
20             final Object commandBean = applicationContext.getBean(jobType);
21
22             if (!(commandBean instanceof JobCommand)) {
23                 throw new GenericUncheckedException(commandBean.getClass() + " is not a JobCommand");
24             }
25
26             return (JobCommand) commandBean;
27         });
28     }
29
30     public JobCommandFactory(Function<Class<? extends JobCommand>, JobCommand> jobFactory) {
31         this.jobFactory = jobFactory;
32     }
33
34     public JobCommand toCommand(Job job) {
35
36         final JobCommand command = jobFactory.apply(job.getType().getCommandClass());
37         command.init(job.getUuid(), job.getData());
38
39         return command;
40     }
41
42
43 }