| 28 | import org.apache.nutch.util.NutchTool; |
| 29 | |
| 30 | public class RAMJobManager implements JobManager { |
| 31 | private JobFactory jobFactory; |
| 32 | private NutchServerPoolExecutor executor; |
| 33 | private ConfManager configManager; |
| 34 | |
| 35 | public RAMJobManager(JobFactory jobFactory, NutchServerPoolExecutor executor, |
| 36 | ConfManager configManager) { |
| 37 | this.jobFactory = jobFactory; |
| 38 | this.executor = executor; |
| 39 | this.configManager = configManager; |
| 40 | } |
| 41 | |
| 42 | @Override |
| 43 | public Collection<JobInfo> list(String crawlId, State state) { |
| 44 | if (state == null || state == State.ANY) { |
| 45 | return executor.getAllJobs(); |
| 46 | } |
| 47 | if (state == State.RUNNING || state == State.IDLE) { |
| 48 | return executor.getJobRunning(); |
| 49 | } |
| 50 | return executor.getJobHistory(); |
| 51 | } |
| 52 | |
| 53 | @Override |
| 54 | public JobInfo get(String crawlId, String jobId) { |
| 55 | return executor.getInfo(jobId); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public String create(JobConfig jobConfig) { |
| 60 | if (jobConfig.getArgs() == null) { |
| 61 | throw new IllegalArgumentException("Arguments cannot be null!"); |
| 62 | } |
| 63 | |
| 64 | Configuration conf = cloneConfiguration(jobConfig.getConfId()); |
| 65 | NutchTool tool = createTool(jobConfig, conf); |
| 66 | JobWorker worker = new JobWorker(jobConfig, conf, tool); |
| 67 | |
| 68 | executor.execute(worker); |
| 69 | executor.purge(); |
| 70 | return worker.getInfo().getId(); |
| 71 | } |
| 72 | |
| 73 | private Configuration cloneConfiguration(String confId) { |
| 74 | Configuration conf = configManager.get(confId); |
| 75 | if (conf == null) { |
| 76 | throw new IllegalArgumentException("Unknown confId " + confId); |
| 77 | } |
| 78 | return new Configuration(conf); |
| 79 | } |
| 80 | |
| 81 | private NutchTool createTool(JobConfig jobConfig, Configuration conf) { |
| 82 | if (StringUtils.isNotBlank(jobConfig.getJobClassName())) { |
| 83 | return jobFactory |
| 84 | .createToolByClassName(jobConfig.getJobClassName(), conf); |
| 85 | } |
| 86 | |
| 87 | return jobFactory.createToolByType(jobConfig.getType(), conf); |
nothing calls this directly
no outgoing calls
no test coverage detected