Coverage Report - org.apache.onami.scheduler.SchedulerProvider
 
Classes in this File Line Coverage Branch Coverage Complexity
SchedulerProvider
85%
18/21
70%
7/10
1.833
 
 1  
 package org.apache.onami.scheduler;
 2  
 
 3  
 /*
 4  
  * Licensed to the Apache Software Foundation (ASF) under one
 5  
  * or more contributor license agreements.  See the NOTICE file
 6  
  * distributed with this work for additional information
 7  
  * regarding copyright ownership.  The ASF licenses this file
 8  
  * to you under the Apache License, Version 2.0 (the
 9  
  * "License"); you may not use this file except in compliance
 10  
  * with the License.  You may obtain a copy of the License at
 11  
  *
 12  
  *   http://www.apache.org/licenses/LICENSE-2.0
 13  
  *
 14  
  * Unless required by applicable law or agreed to in writing,
 15  
  * software distributed under the License is distributed on an
 16  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 17  
  * KIND, either express or implied.  See the License for the
 18  
  * specific language governing permissions and limitations
 19  
  * under the License.
 20  
  */
 21  
 
 22  
 import java.util.Set;
 23  
 
 24  
 import javax.inject.Inject;
 25  
 import javax.inject.Provider;
 26  
 
 27  
 import org.quartz.JobListener;
 28  
 import org.quartz.Scheduler;
 29  
 import org.quartz.SchedulerException;
 30  
 import org.quartz.SchedulerListener;
 31  
 import org.quartz.TriggerListener;
 32  
 import org.quartz.impl.StdSchedulerFactory;
 33  
 import org.quartz.spi.JobFactory;
 34  
 
 35  
 /**
 36  
  * Provides a {@code Scheduler} instance.
 37  
  */
 38  5
 final class SchedulerProvider
 39  
     implements Provider<Scheduler>
 40  
 {
 41  
 
 42  
     /**
 43  
      * The {@code Scheduler} instance will be provided.
 44  
      */
 45  
     private final Scheduler scheduler;
 46  
 
 47  
     /**
 48  
      * Initialized a new {@code Provider&lt;Scheduler&gt;} instance.
 49  
      *
 50  
      * @throws SchedulerException If any error occurs
 51  
      */
 52  
     @Inject
 53  
     public SchedulerProvider( SchedulerConfiguration schedulerConfiguration )
 54  
         throws SchedulerException
 55  5
     {
 56  5
         StdSchedulerFactory schedulerFactory = new StdSchedulerFactory();
 57  
 
 58  5
         if ( schedulerConfiguration.getProperties() != null )
 59  
         {
 60  1
             schedulerFactory.initialize( schedulerConfiguration.getProperties() );
 61  
         }
 62  
 
 63  5
         this.scheduler = schedulerFactory.getScheduler();
 64  
 
 65  5
         if ( !schedulerConfiguration.startManually() )
 66  
         {
 67  4
             scheduler.start();
 68  
         }
 69  5
     }
 70  
 
 71  
     /**
 72  
      * Sets the {@code JobFactory} instance (it will be a {@link InjectorJobFactory} instance).
 73  
      *
 74  
      * @param jobFactory The {@code JobFactory} instance.
 75  
      * @throws SchedulerException If any error occurs
 76  
      */
 77  
     @Inject
 78  
     public void setJobFactory( JobFactory jobFactory )
 79  
         throws SchedulerException
 80  
     {
 81  5
         scheduler.setJobFactory( jobFactory );
 82  5
     }
 83  
 
 84  
     /**
 85  
      * Sets the {@code JobListener}s.
 86  
      *
 87  
      * @param jobListeners The {@code JobListener}s
 88  
      * @throws SchedulerException If any error occurs
 89  
      */
 90  
     @com.google.inject.Inject( optional = true )
 91  
     public void addJobListeners( Set<JobListener> jobListeners )
 92  
         throws SchedulerException
 93  
     {
 94  5
         for ( JobListener jobListener : jobListeners )
 95  
         {
 96  0
             scheduler.getListenerManager().addJobListener( jobListener );
 97  
         }
 98  5
     }
 99  
 
 100  
     /**
 101  
      * Sets the {@code SchedulerListener}s.
 102  
      *
 103  
      * @param schedulerListeners The {@code SchedulerListener}s
 104  
      * @throws SchedulerException If any error occurs
 105  
      */
 106  
     @com.google.inject.Inject( optional = true )
 107  
     public void addSchedulerListeners( Set<SchedulerListener> schedulerListeners )
 108  
         throws SchedulerException
 109  
     {
 110  5
         for ( SchedulerListener schedulerListener : schedulerListeners )
 111  
         {
 112  0
             scheduler.getListenerManager().addSchedulerListener( schedulerListener );
 113  
         }
 114  5
     }
 115  
 
 116  
     /**
 117  
      * Sets the {@code TriggerListener}s.
 118  
      *
 119  
      * @param triggerListeners The {@code TriggerListener}s
 120  
      * @throws SchedulerException If any error occurs
 121  
      */
 122  
     @com.google.inject.Inject( optional = true )
 123  
     public void addTriggerListeners( Set<TriggerListener> triggerListeners )
 124  
         throws SchedulerException
 125  
     {
 126  5
         for ( TriggerListener triggerListener : triggerListeners )
 127  
         {
 128  0
             scheduler.getListenerManager().addTriggerListener( triggerListener );
 129  
         }
 130  5
     }
 131  
 
 132  
     /**
 133  
      * {@inheritDoc}
 134  
      */
 135  
     public Scheduler get()
 136  
     {
 137  5
         return scheduler;
 138  
     }
 139  
 
 140  
 }