Coverage report

  %line %branch
org.apache.turbine.services.schedule.TurbineNonPersistentSchedulerService
80% 
100% 

 1  
 package org.apache.turbine.services.schedule;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License")
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *     http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import java.util.List;
 20  
 import java.util.Vector;
 21  
 
 22  
 import javax.servlet.ServletConfig;
 23  
 
 24  
 import org.apache.commons.configuration.Configuration;
 25  
 
 26  
 import org.apache.commons.lang.StringUtils;
 27  
 
 28  
 import org.apache.commons.logging.Log;
 29  
 import org.apache.commons.logging.LogFactory;
 30  
 
 31  
 import org.apache.turbine.services.InitializationException;
 32  
 import org.apache.turbine.util.TurbineException;
 33  
 
 34  
 /**
 35  
  * Service for a cron like scheduler that uses the
 36  
  * TurbineResources.properties file instead of the database.
 37  
  * The methods that operate on jobs ( get,add,update,remove )
 38  
  * only operate on the queue in memory and changes are not reflected
 39  
  * to the properties file which was used to initilize the jobs.
 40  
  * An example is given below.  The job names are the class names that
 41  
  * extend ScheduledJob.
 42  
  *
 43  
  * <PRE>
 44  
  *
 45  
  * services.SchedulerService.scheduler.jobs=scheduledJobName,scheduledJobName2
 46  
  *
 47  
  * services.SchedulerService.scheduler.job.scheduledJobName.ID=1
 48  
  * services.SchedulerService.scheduler.job.scheduledJobName.SECOND=-1
 49  
  * services.SchedulerService.scheduler.job.scheduledJobName.MINUTE=-1
 50  
  * services.SchedulerService.scheduler.job.scheduledJobName.HOUR=7
 51  
  * services.SchedulerService.scheduler.job.scheduledJobName.WEEKDAY=-1
 52  
  * services.SchedulerService.scheduler.job.scheduledJobName.DAY_OF_MONTH=-1
 53  
  *
 54  
  * services.SchedulerService.scheduler.job.scheduledJobName2.ID=1
 55  
  * services.SchedulerService.scheduler.job.scheduledJobName2.SECOND=-1
 56  
  * services.SchedulerService.scheduler.job.scheduledJobName2.MINUTE=-1
 57  
  * services.SchedulerService.scheduler.job.scheduledJobName2.HOUR=7
 58  
  * services.SchedulerService.scheduler.job.scheduledJobName2.WEEKDAY=-1
 59  
  * services.SchedulerService.scheduler.job.scheduledJobName2.DAY_OF_MONTH=-1
 60  
  *
 61  
  * </PRE>
 62  
  *
 63  
  * Based on TamboraSchedulerService written by John Thorhauer.
 64  
  *
 65  
  * @author <a href="mailto:ekkerbj@netscpae.net">Jeff Brekke</a>
 66  
  * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
 67  
  * @author <a href="mailto:quintonm@bellsouth.net">Quinton McCombs</a>
 68  
  * @version $Id: TurbineNonPersistentSchedulerService.java 264148 2005-08-29 14:21:04Z henning $
 69  
  */
 70  
 public class TurbineNonPersistentSchedulerService
 71  
         extends TurbineSchedulerService
 72  
 {
 73  
     /** Logging */
 74  2
     private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
 75  
 
 76  
     /**
 77  
      * Constructor.
 78  
      *
 79  
      * @exception TurbineException a generic exception.
 80  
      */
 81  
     public TurbineNonPersistentSchedulerService()
 82  
             throws TurbineException
 83  
     {
 84  2
         super();
 85  2
     }
 86  
 
 87  
     /**
 88  
      * Called the first time the Service is used.<br>
 89  
      *
 90  
      * Load all the jobs from cold storage.  Add jobs to the queue
 91  
      * (sorted in ascending order by runtime) and start the scheduler
 92  
      * thread.
 93  
      */
 94  
     public void init()
 95  
             throws InitializationException
 96  
     {
 97  2
         Configuration conf = getConfiguration();
 98  
 
 99  
         try
 100  
         {
 101  2
             scheduleQueue = new JobQueue();
 102  2
             mainLoop = new MainLoop();
 103  
 
 104  2
             List jobProps = conf.getList("scheduler.jobs");
 105  2
             List jobs = new Vector();
 106  
             // If there are scheduler.jobs defined then set up a job vector
 107  
             // for the scheduleQueue
 108  2
             if (!jobProps.isEmpty())
 109  
             {
 110  4
                 for (int i = 0; i < jobProps.size(); i++)
 111  
                 {
 112  2
                     String jobName = (String) jobProps.get(i);
 113  2
                     String jobPrefix = "scheduler.job." + jobName;
 114  
 
 115  2
                     String jobId = conf.getString(jobPrefix + ".ID", null);
 116  2
                     if (StringUtils.isEmpty(jobId))
 117  
                     {
 118  0
                         throw new Exception(
 119  
                                 "There is an error in the TurbineResources.properties file. \n"
 120  
                                 + jobPrefix + ".ID is not found.\n");
 121  
                     }
 122  
 
 123  2
                     int sec = conf.getInt(jobPrefix + ".SECOND", -1);
 124  2
                     int min = conf.getInt(jobPrefix + ".MINUTE", -1);
 125  2
                     int hr = conf.getInt(jobPrefix + ".HOUR", -1);
 126  2
                     int wkday = conf.getInt(jobPrefix + ".WEEKDAY", -1);
 127  2
                     int dayOfMonth = conf.getInt(jobPrefix + ".DAY_OF_MONTH", -1);
 128  
 
 129  2
                     JobEntry je = new JobEntry(
 130  
                             sec,
 131  
                             min,
 132  
                             hr,
 133  
                             wkday,
 134  
                             dayOfMonth,
 135  
                             jobName);
 136  2
                     je.setJobId(Integer.parseInt(jobId));
 137  2
                     jobs.add(je);
 138  
 
 139  
                 }
 140  
             }
 141  
 
 142  2
             if (jobs != null && jobs.size() > 0)
 143  
             {
 144  2
                 scheduleQueue.batchLoad(jobs);
 145  
             }
 146  
 
 147  2
             setEnabled(getConfiguration().getBoolean("enabled", true));
 148  2
             restart();
 149  
 
 150  2
             setInit(true);
 151  1
         }
 152  0
         catch (Exception e)
 153  
         {
 154  0
             String errorMessage = "Could not initialize the scheduler service";
 155  0
             log.error(errorMessage, e);
 156  0
             throw new InitializationException(errorMessage, e);
 157  1
         }
 158  2
     }
 159  
 
 160  
     /**
 161  
      * Called the first time the Service is used.<br>
 162  
      *
 163  
      * Load all the jobs from cold storage.  Add jobs to the queue
 164  
      * (sorted in ascending order by runtime) and start the scheduler
 165  
      * thread.
 166  
      *
 167  
      * @param config A ServletConfig.
 168  
      * @deprecated use init() instead.
 169  
      */
 170  
     public void init(ServletConfig config)
 171  
             throws InitializationException
 172  
     {
 173  0
         init();
 174  0
     }
 175  
 
 176  
     /**
 177  
      * This method returns the job element from the internal queue.
 178  
      *
 179  
      * @param oid The int id for the job.
 180  
      * @return A JobEntry.
 181  
      * @exception TurbineException could not retrieve job
 182  
      */
 183  
     public JobEntry getJob(int oid)
 184  
             throws TurbineException
 185  
     {
 186  2
         JobEntry je = new JobEntry();
 187  2
         je.setJobId(oid);
 188  2
         return scheduleQueue.getJob(je);
 189  
     }
 190  
 
 191  
     /**
 192  
      * Add a new job to the queue.
 193  
      *
 194  
      * @param je A JobEntry with the job to add.
 195  
      * @throws TurbineException job could not be added
 196  
      */
 197  
     public void addJob(JobEntry je)
 198  
             throws TurbineException
 199  
     {
 200  2
         updateJob(je);
 201  2
     }
 202  
 
 203  
     /**
 204  
      * Remove a job from the queue.
 205  
      *
 206  
      * @param je A JobEntry with the job to remove.
 207  
      */
 208  
     public void removeJob(JobEntry je)
 209  
     {
 210  
         // Remove from the queue.
 211  2
         scheduleQueue.remove(je);
 212  2
         restart();
 213  2
     }
 214  
 
 215  
     /**
 216  
      * Add/update a job
 217  
      *
 218  
      * @param je A JobEntry with the job to modify
 219  
      * @throws TurbineException job could not be updated
 220  
      */
 221  
     public void updateJob(JobEntry je)
 222  
             throws TurbineException
 223  
     {
 224  
         try
 225  
         {
 226  2
             je.calcRunTime();
 227  
 
 228  
             // Update the queue.
 229  2
             scheduleQueue.modify(je);
 230  2
             restart();
 231  1
         }
 232  0
         catch (Exception e)
 233  
         {
 234  0
             String errorMessage = "Problem updating Scheduled Job: " + je.getTask();
 235  0
             log.error(errorMessage, e);
 236  0
             throw new TurbineException(errorMessage, e);
 237  1
         }
 238  2
     }
 239  
 }

This report is generated by jcoverage, Maven and Maven JCoverage Plugin.