001/*
002 * geordi
003 *
004 * Copyright (C) 2018 Richard "Shred" Körber
005 *   https://github.com/shred/geordi
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
015 */
016package org.shredzone.geordi.util;
017
018import javax.inject.Inject;
019import javax.inject.Singleton;
020
021import org.quartz.Job;
022import org.quartz.Scheduler;
023import org.quartz.SchedulerException;
024import org.quartz.spi.JobFactory;
025import org.quartz.spi.TriggerFiredBundle;
026
027import com.google.inject.Injector;
028
029/**
030 * A Quartz {@link JobFactory} that uses Guice for dependency injection.
031 */
032@Singleton
033public class GuiceJobFactory implements JobFactory {
034
035    private final Injector guice;
036
037    @Inject
038    public GuiceJobFactory(Injector guice) {
039        this.guice = guice;
040    }
041
042    @Override
043    public Job newJob(TriggerFiredBundle triggerFiredBundle, Scheduler scheduler)
044            throws SchedulerException {
045        try {
046            return guice.getInstance(triggerFiredBundle.getJobDetail().getJobClass());
047        } catch (Exception ex) {
048            throw new UnsupportedOperationException(ex);
049        }
050    }
051
052}