Coverage report

  %line %branch
org.apache.stratum.scheduler.Scheduler
78% 
97% 

 1  
 package org.apache.stratum.scheduler;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation or its licensors,
 5  
  * as applicable.
 6  
  *
 7  
  * Licensed under the Apache License, Version 2.0 (the "License");
 8  
  * you may not use this file except in compliance with the License.
 9  
  * You may obtain a copy of the License at
 10  
  *
 11  
  *     http://www.apache.org/licenses/LICENSE-2.0
 12  
  *
 13  
  * Unless required by applicable law or agreed to in writing, software
 14  
  * distributed under the License is distributed on an "AS IS" BASIS,
 15  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 16  
  * See the License for the specific language governing permissions and
 17  
  * limitations under the License.
 18  
  */
 19  
 
 20  
 import java.io.FileInputStream;
 21  
 import java.util.List;
 22  
 import java.util.Properties;
 23  
 
 24  
 import org.apache.commons.betwixt.XMLIntrospector;
 25  
 import org.apache.commons.betwixt.io.BeanReader;
 26  
 import org.apache.commons.betwixt.strategy.DecapitalizeNameMapper;
 27  
 import org.apache.commons.betwixt.strategy.DefaultPluralStemmer;
 28  
 import org.apache.commons.configuration.Configuration;
 29  
 import org.apache.commons.configuration.PropertiesConfiguration;
 30  
 import org.apache.commons.lang.exception.NestableException;
 31  
 import org.apache.log4j.Category;
 32  
 import org.apache.stratum.lifecycle.Configurable;
 33  
 import org.apache.stratum.lifecycle.Startable;
 34  
 
 35  
 import org.quartz.CronTrigger;
 36  
 import org.quartz.JobDetail;
 37  
 import org.quartz.SchedulerException;
 38  
 import org.quartz.impl.StdScheduler;
 39  
 import org.quartz.impl.StdSchedulerFactory;
 40  
 
 41  
 /**
 42  
  * This class is the Scheduler component that implements a Quartz scheduler.
 43  
  *
 44  
  * @author <a href="mailto:john@zenplex.com">John Thorhauer</a>
 45  
  * @version $Id: Scheduler.java 264731 2005-08-30 08:04:32Z henning $
 46  
  */
 47  10
 public class Scheduler
 48  
         implements Configurable, Startable
 49  
 {
 50  
     /** Log4j category used for logging. */
 51  30
     private static Category log = Category.getInstance(Scheduler.class);
 52  2
 
 53  
     /** TODO: DOCUMENT ME! */
 54  10
     private static String confLocation = "/projects/jakarta-turbine-stratum/src/test-conf/Scheduler.properties";
 55  
 
 56  
     /** TODO: DOCUMENT ME! */
 57  
     private StdSchedulerFactory schedulerFactory;
 58  
 
 59  6
     /** TODO: DOCUMENT ME! */
 60  
     protected StdScheduler scheduler;
 61  
 
 62  2
     /**
 63  
      * Just a command line test tool to start the scheduler from the command line.  If looks for the properties file in the
 64  
      * following location: /projects/jakarta-turbine-stratum/src/test-conf/Scheduler.properties
 65  
      *
 66  
      * @param args command line arguments
 67  
      *
 68  
      * @throws Exception
 69  
      */
 70  
     public static void main(String [] args)
 71  
             throws Exception
 72  
     {
 73  0
         Scheduler sched = new Scheduler();
 74  0
         PropertiesConfiguration conf = new PropertiesConfiguration(confLocation);
 75  0
         sched.configure(conf);
 76  0
         sched.start();
 77  0
     }
 78  
 
 79  
     /**
 80  
      * start the scheduler.
 81  
      *
 82  
      * @throws Exception
 83  
      */
 84  
     public void start()
 85  
             throws Exception
 86  
     {
 87  10
         scheduler.start();
 88  10
     }
 89  
 
 90  
     /**
 91  
      * stop the scheduler.
 92  2
      *
 93  2
      * @throws Exception
 94  
      */
 95  
     public void stop()
 96  
             throws Exception
 97  
     {
 98  10
         scheduler.shutdown();
 99  10
     }
 100  
 
 101  
     /**
 102  2
      * Configure the Scheduler
 103  2
      *
 104  
      * @param configuration the configuration
 105  
      *
 106  
      * @throws NestableException
 107  
      */
 108  
     public void configure(Configuration configuration)
 109  
             throws NestableException
 110  
     {
 111  10
         String xmlLocation = configuration.getString("scheduler.config.location");
 112  
 
 113  2
         try
 114  
         {
 115  
             // get scheduler factory and initialize it with
 116  
             // settings from the .xml file
 117  10
             SchedulerConfig schedConfig = getSchedulerConfig(xmlLocation);
 118  
             Properties factoryProps;
 119  2
 
 120  
             try
 121  
             {
 122  10
                 factoryProps = getFactoryProps(schedConfig);
 123  
             }
 124  2
             catch (NullPointerException ex)
 125  
             {
 126  0
                 log.error("Error loading properties to initialize"
 127  
                         + " Scheduler Factory. Please make sure the following"
 128  
                         + " elements are properly filled out in the"
 129  
                         + " Scheduler .xml config file:\n"
 130  
                         + "   <instanceName></instanceName>\n"
 131  
                         + "    <threadPoolConfig>\n"
 132  
                         + "     <className></className>\n"
 133  
                         + "     <threadCount></threadCount>\n"
 134  
                         + "     <threadPriority></threadPriority>\n"
 135  
                         + "   </threadPoolConfig>\n"
 136  
                         + "   <jobStoreConfig>\n"
 137  
                         + "     <className></className>\n"
 138  
                         + "   </jobStoreConfig>");
 139  
 
 140  0
                 return;
 141  10
             }
 142  
 
 143  10
             schedulerFactory = new StdSchedulerFactory();
 144  10
             schedulerFactory.initialize(factoryProps);
 145  2
 
 146  10
             scheduler = (StdScheduler) schedulerFactory.getScheduler();
 147  2
 
 148  12
             List jobConfigs = schedConfig.getJobConfigs();
 149  
 
 150  12
             if (!jobConfigs.isEmpty())
 151  
             {
 152  32
                 for (int i = 0; i < jobConfigs.size(); i++)
 153  
                 {
 154  22
                     JobConfig jobConf = (JobConfig) jobConfigs.get(i);
 155  20
                     String jobName = (String) jobConf.getName();
 156  6
 
 157  20
                     String jobGroup = jobConf.getGroup();
 158  24
                     String jobClassName = jobConf.getClassName();
 159  4
 
 160  20
                     JobDetail job = new JobDetail(jobName, jobGroup, Class.forName(jobClassName));
 161  4
 
 162  4
                     //get the trigger for this job if it exists
 163  22
                     TriggerConfig triggerConf = getTriggerConfig(schedConfig, job.getName());
 164  2
 
 165  22
                     if (triggerConf != null)
 166  
                     {
 167  20
                         job.getName();
 168  2
 
 169  22
                         String triggerGroup = triggerConf.getGroup();
 170  22
                         String triggerName = triggerConf.getName();
 171  22
                         String cronExpression =
 172  2
                             triggerConf.getSeconds() + " " + triggerConf.getMinutes() + " " + triggerConf.getHours() + " "
 173  2
                             + triggerConf.getDayOfMonth() + " " + triggerConf.getMonth() + " " + triggerConf.getDayOfWeek();
 174  2
 
 175  24
                         CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroup, jobName, jobGroup, cronExpression);
 176  4
 
 177  22
                         scheduler.scheduleJob(job, cronTrigger);
 178  20
                         triggerConf = null;
 179  20
                         cronTrigger = null;
 180  
                     }
 181  
                 }
 182  
             }
 183  
         }
 184  2
         catch (SchedulerException ex)
 185  2
         {
 186  0
             log.error("Error Initializing Scheduler:  " + ex.toString(), ex);
 187  
         }
 188  0
         catch (ClassNotFoundException ex)
 189  
         {
 190  2
             log.error("Error Loading class:  " + ex.toString(), ex);
 191  4
         }
 192  4
         catch (Exception ex)
 193  2
         {
 194  0
             log.error("Error:  " + ex.toString(), ex);
 195  10
         }
 196  10
     }
 197  
 
 198  
     /**
 199  
      * DOCUMENT ME!
 200  
      *
 201  
      * @param schdConf Scheduler Config
 202  
      *
 203  
      * @return TODO: DOCUMENT ME!
 204  
      */
 205  
     private Properties getFactoryProps(SchedulerConfig schdConf)
 206  
     {
 207  10
         Properties props = new Properties();
 208  1
 
 209  12
         props.put("org.quartz.scheduler.instanceName", schdConf.getInstanceName());
 210  1
 
 211  10
         ThreadPoolConfig threadPool = schdConf.getThreadPoolConfig();
 212  10
         props.put("org.quartz.threadPool.class", threadPool.getClassName());
 213  10
         props.put("org.quartz.threadPool.threadCount", threadPool.getThreadCount());
 214  10
         props.put("org.quartz.threadPool.threadPriority", threadPool.getThreadPriority());
 215  
 
 216  11
         JobStoreConfig jobStore = schdConf.getJobStoreConfig();
 217  11
         props.put("org.quartz.jobStore.class", jobStore.getClassName());
 218  1
 
 219  11
         return props;
 220  1
     }
 221  1
 
 222  
     /**
 223  1
      * Loads the SchedulerConfig object from the xml file
 224  3
      *
 225  2
      * @param xmlLocation of the xml file
 226  1
      *
 227  2
      * @return TODO: DOCUMENT ME!
 228  2
      *
 229  2
      * @throws Exception
 230  2
      */
 231  1
     private SchedulerConfig getSchedulerConfig(String xmlLocation)
 232  1
             throws Exception
 233  1
     {
 234  10
         FileInputStream in = new FileInputStream(xmlLocation);
 235  
 
 236  
         // create a new BeanReader
 237  10
         BeanReader reader = createBeanReader();
 238  1
 
 239  12
         SchedulerConfig schedConf = (SchedulerConfig) reader.parse(in);
 240  1
 
 241  11
         return schedConf;
 242  1
     }
 243  1
 
 244  1
     /**
 245  1
      * Creates a beanreader
 246  1
      *
 247  
      * @return beanreader
 248  1
      *
 249  2
      * @throws Exception
 250  1
      */
 251  1
     private BeanReader createBeanReader()
 252  1
             throws Exception
 253  1
     {
 254  11
         BeanReader reader = new BeanReader();
 255  10
         reader.setXMLIntrospector(createXMLIntrospector());
 256  10
         reader.registerBeanClass(SchedulerConfig.class);
 257  
 
 258  10
         return reader;
 259  
     }
 260  
 
 261  
     /**
 262  
      * Loads Xml Introspector with needed values
 263  
      *
 264  
      * @return introspector
 265  1
      */
 266  1
     private XMLIntrospector createXMLIntrospector()
 267  
     {
 268  11
         XMLIntrospector introspector = new XMLIntrospector();
 269  1
 
 270  1
         // set elements for attributes to true
 271  12
         introspector.getConfiguration().setAttributesForPrimitives(false);
 272  1
 
 273  
         // wrap collections in an XML element
 274  
         //introspector.setWrapCollectionsInElement(true);
 275  
         // turn bean elements first letter into lower case
 276  10
         introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
 277  9
         introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
 278  1
 
 279  9
         introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer());
 280  1
 
 281  9
         return introspector;
 282  1
     }
 283  2
 
 284  2
     private TriggerConfig getTriggerConfig(SchedulerConfig schedConf, String jobName)
 285  4
     {
 286  21
         TriggerConfig trig = null;
 287  18
         List triggers = schedConf.getTriggerConfigs();
 288  3
 
 289  27
         for (int i = 0; i < triggers.size(); i++)
 290  3
         {
 291  27
             TriggerConfig tmpTrig = (TriggerConfig) triggers.get(i);
 292  3
 
 293  27
             if (tmpTrig.getJobName().equals(jobName))
 294  2
             {
 295  19
                 trig = tmpTrig;
 296  3
 
 297  18
                 return trig;
 298  1
             }
 299  1
         }
 300  
 
 301  0
         return trig;
 302  
     }
 303  
 }

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