001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.scheduling.quartz;
018
019import org.quartz.Job;
020import org.quartz.Scheduler;
021import org.quartz.SchedulerException;
022import org.quartz.spi.JobFactory;
023import org.quartz.spi.TriggerFiredBundle;
024
025/**
026 * JobFactory implementation that supports {@link java.lang.Runnable}
027 * objects as well as standard Quartz {@link org.quartz.Job} instances.
028 *
029 * <p>Compatible with Quartz 2.1.4 and higher, as of Spring 4.1.
030 *
031 * @author Juergen Hoeller
032 * @since 2.0
033 * @see DelegatingJob
034 * @see #adaptJob(Object)
035 */
036public class AdaptableJobFactory implements JobFactory {
037
038        @Override
039        public Job newJob(TriggerFiredBundle bundle, Scheduler scheduler) throws SchedulerException {
040                try {
041                        Object jobObject = createJobInstance(bundle);
042                        return adaptJob(jobObject);
043                }
044                catch (Exception ex) {
045                        throw new SchedulerException("Job instantiation failed", ex);
046                }
047        }
048
049        /**
050         * Create an instance of the specified job class.
051         * <p>Can be overridden to post-process the job instance.
052         * @param bundle the TriggerFiredBundle from which the JobDetail
053         * and other info relating to the trigger firing can be obtained
054         * @return the job instance
055         * @throws Exception if job instantiation failed
056         */
057        protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
058                return bundle.getJobDetail().getJobClass().newInstance();
059        }
060
061        /**
062         * Adapt the given job object to the Quartz Job interface.
063         * <p>The default implementation supports straight Quartz Jobs
064         * as well as Runnables, which get wrapped in a DelegatingJob.
065         * @param jobObject the original instance of the specified job class
066         * @return the adapted Quartz Job instance
067         * @throws Exception if the given job could not be adapted
068         * @see DelegatingJob
069         */
070        protected Job adaptJob(Object jobObject) throws Exception {
071                if (jobObject instanceof Job) {
072                        return (Job) jobObject;
073                }
074                else if (jobObject instanceof Runnable) {
075                        return new DelegatingJob((Runnable) jobObject);
076                }
077                else {
078                        throw new IllegalArgumentException("Unable to execute job class [" + jobObject.getClass().getName() +
079                                        "]: only [org.quartz.Job] and [java.lang.Runnable] supported.");
080                }
081        }
082
083}