<commit_before>/* Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.grails.plugins.quartz;

import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.beans.BeansException;
import org.quartz.spi.TriggerFiredBundle;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import java.lang.reflect.Method;

/**
 * Job factory which retrieves Job instances from ApplicationContext.
 *
 * @author <NAME> (<EMAIL>)
 * 
 * @since 0.3.2
 */
public class GrailsJobFactory extends AdaptableJobFactory implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        String grailsJobName = (String) bundle.getJobDetail().getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER);
        //Object job = applicationContext.getBean(grailsJobName);
         if(grailsJobName != null) {
            Object job = applicationContext.getBean(grailsJobName);
            if(bundle.getJobDetail().getJobClass().equals(StatefulGrailsTaskClassJob.class)) {
                return new StatefulGrailsTaskClassJob(job);
            }
            return new GrailsTaskClassJob(job);
        } else {
            return super.createJobInstance(bundle);
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
	 * Quartz Job implementation that invokes execute() on the GrailsTaskClass instance.
	 */
	public class GrailsTaskClassJob implements Job {
        Object job;

        public GrailsTaskClassJob(Object job) {
            this.job = job;
        }

        public void execute(final JobExecutionContext context) throws JobExecutionException {
			try {
                // trying to invoke execute(context) method
                Method method = ReflectionUtils.findMethod(job.getClass(), "execute", new Class[] {Object.class});
                if(method != null) {
                    ReflectionUtils.invokeMethod(method, job, new Object[] {context});
                } else {
                    // falling back to execute() method
                    ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(job.getClass(), "execute"), job);
                }
			}
			catch (Exception e) {
				throw new JobExecutionException(e.getMessage(), e);
			}
		}
	}

	/**
	 * Extension of the GrailsTaskClassJob, implementing the StatefulJob interface.
	 * Quartz checks whether or not jobs are stateful and if so,<commit_msg>Fixed another error with 'def execute(JobExecutionContext context)' method<commit_after>/* Copyright 2006-2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.codehaus.groovy.grails.plugins.quartz;

import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import org.springframework.beans.BeansException;
import org.quartz.spi.TriggerFiredBundle;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.StatefulJob;

import java.lang.reflect.Method;

/**
 * Job factory which retrieves Job instances from ApplicationContext.
 *
 * @author <NAME> (<EMAIL>)
 * 
 * @since 0.3.2
 */
public class GrailsJobFactory extends AdaptableJobFactory implements ApplicationContextAware {
    private ApplicationContext applicationContext;

    protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
        String grailsJobName = (String) bundle.getJobDetail().getJobDataMap().get(JobDetailFactoryBean.JOB_NAME_PARAMETER);
        //Object job = applicationContext.getBean(grailsJobName);
         if(grailsJobName != null) {
            Object job = applicationContext.getBean(grailsJobName);
            if(bundle.getJobDetail().getJobClass().equals(StatefulGrailsTaskClassJob.class)) {
                return new StatefulGrailsTaskClassJob(job);
            }
            return new GrailsTaskClassJob(job);
        } else {
            return super.createJobInstance(bundle);
        }
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

    /**
	 * Quartz Job implementation that invokes execute() on the GrailsTaskClass instance.
	 */
	public class GrailsTaskClassJob implements Job {
      Object job;

      public GrailsTaskClassJob(Object job) {
          this.job = job;
      }

      public void execute(final JobExecutionContext context) throws JobExecutionException {
          try {
              Method method = ReflectionUtils.findMethod(job.getClass(), GrailsTaskClassProperty.EXECUTE, new Class[]{JobExecutionContext.class});
              if(method != null) {
                  ReflectionUtils.invokeMethod(method, job, new JobExecutionContext[] {context});
              } else if((method = ReflectionUtils.findMethod(job.getClass(), "execute", new Class[] {Object.class})) != null) {
                ReflectionUtils.invokeMethod(method, job, new Object[] {context});
              } else {
                  // falling back to execute() method
                  ReflectionUtils.invokeMethod(ReflectionUtils.findMethod(job.getClass(), "execute"), job);
              }
          }
          catch (Exception e) {
            throw new JobExecutionException(e.getMessage(), e);
          }
  		}
	}

	/**
	 * Extension of the GrailsTaskClassJob, implementing the StatefulJob interface.
	 * Quartz checks whether or not jobs are stateful and if so,
{'fraction_non_alphanumeric': 0.0847267106927076, 'fraction_numerical': 0.004215259238443164, 'mean_word_length': 3.332318928788801, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2406381', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1756, 'n_words': 519}