/*
 * Axelor Business Solutions
 *
 * Copyright (C) 2005-2019 Axelor (<http://axelor.com>).
 *
 * This program is free software: you can redistribute it and/or  modify
 * it under the terms of the GNU Affero General Public License, version 3,
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.axelor.quartz;

import java.util.Properties;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.core.QuartzScheduler;
import org.quartz.core.QuartzSchedulerResources;
import org.quartz.impl.StdSchedulerFactory;

/** Custom {@link StdSchedulerFactory} to use {@link GuiceJobRunShellFactory}. */
public class GuiceSchedulerFactory extends StdSchedulerFactory {

  public GuiceSchedulerFactory(Properties props) throws SchedulerException {
    super(props);
  }

  @Override
  protected Scheduler instantiate(QuartzSchedulerResources rsrcs, QuartzScheduler qs) {
    rsrcs.setJobRunShellFactory(new GuiceJobRunShellFactory(rsrcs));
    return super.instantiate(rsrcs, qs);
  }
}

@@ -802,32 +802,38 @@
 quartz.Scheduler
+Config
 Exception;%0Aimpor
@@ -827,16 +827,97 @@
 eption;%0A
+import org.quartz.SchedulerException;%0Aimport org.quartz.core.JobRunShellFactory;%0A
 import o
@@ -1398,17 +1398,64 @@
     
-rsrcs.set
+Scheduler scheduler = super.instantiate(rsrcs, qs);%0A    
 JobR
@@ -1468,17 +1468,24 @@
 lFactory
-(
+ jrsf = 
 new Guic
@@ -1514,50 +1514,194 @@
 rcs)
-)
 ;%0A    r
-eturn super.instantiate(rsrcs, qs)
+srcs.setJobRunShellFactory(jrsf);%0A    try %7B%0A      jrsf.initialize(scheduler);%0A    %7D catch (SchedulerConfigException e) %7B%0A      // this should not happen%0A    %7D%0A    return scheduler
 ;%0A  

Fix NPE when using quartz job context