View Javadoc

    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  public class Scheduler
    48          implements Configurable, Startable
    49  {
    50      /*** Log4j category used for logging. */
    51      private static Category log = Category.getInstance(Scheduler.class);
    52
    53      /*** TODO: DOCUMENT ME! */
    54      private static String confLocation = "/projects/jakarta-turbine-stratum/src/test-conf/Scheduler.properties";
    55
    56      /*** TODO: DOCUMENT ME! */
    57      private StdSchedulerFactory schedulerFactory;
    58
    59      /*** TODO: DOCUMENT ME! */
    60      protected StdScheduler scheduler;
    61
    62      /***
    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          Scheduler sched = new Scheduler();
    74          PropertiesConfiguration conf = new PropertiesConfiguration(confLocation);
    75          sched.configure(conf);
    76          sched.start();
    77      }
    78
    79      /***
    80       * start the scheduler.
    81       *
    82       * @throws Exception
    83       */
    84      public void start()
    85              throws Exception
    86      {
    87          scheduler.start();
    88      }
    89
    90      /***
    91       * stop the scheduler.
    92       *
    93       * @throws Exception
    94       */
    95      public void stop()
    96              throws Exception
    97      {
    98          scheduler.shutdown();
    99      }
    100
    101     /***
    102      * Configure the Scheduler
    103      *
    104      * @param configuration the configuration
    105      *
    106      * @throws NestableException
    107      */
    108     public void configure(Configuration configuration)
    109             throws NestableException
    110     {
    111         String xmlLocation = configuration.getString("scheduler.config.location");
    112
    113         try
    114         {
    115             // get scheduler factory and initialize it with
    116             // settings from the .xml file
    117             SchedulerConfig schedConfig = getSchedulerConfig(xmlLocation);
    118             Properties factoryProps;
    119
    120             try
    121             {
    122                 factoryProps = getFactoryProps(schedConfig);
    123             }
    124             catch (NullPointerException ex)
    125             {
    126                 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                 return;
    141             }
    142
    143             schedulerFactory = new StdSchedulerFactory();
    144             schedulerFactory.initialize(factoryProps);
    145
    146             scheduler = (StdScheduler) schedulerFactory.getScheduler();
    147
    148             List jobConfigs = schedConfig.getJobConfigs();
    149
    150             if (!jobConfigs.isEmpty())
    151             {
    152                 for (int i = 0; i < jobConfigs.size(); i++)
    153                 {
    154                     JobConfig jobConf = (JobConfig) jobConfigs.get(i);
    155                     String jobName = (String) jobConf.getName();
    156
    157                     String jobGroup = jobConf.getGroup();
    158                     String jobClassName = jobConf.getClassName();
    159
    160                     JobDetail job = new JobDetail(jobName, jobGroup, Class.forName(jobClassName));
    161
    162                     //get the trigger for this job if it exists
    163                     TriggerConfig triggerConf = getTriggerConfig(schedConfig, job.getName());
    164
    165                     if (triggerConf != null)
    166                     {
    167                         job.getName();
    168
    169                         String triggerGroup = triggerConf.getGroup();
    170                         String triggerName = triggerConf.getName();
    171                         String cronExpression =
    172                             triggerConf.getSeconds() + " " + triggerConf.getMinutes() + " " + triggerConf.getHours() + " "
    173                             + triggerConf.getDayOfMonth() + " " + triggerConf.getMonth() + " " + triggerConf.getDayOfWeek();
    174
    175                         CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroup, jobName, jobGroup, cronExpression);
    176
    177                         scheduler.scheduleJob(job, cronTrigger);
    178                         triggerConf = null;
    179                         cronTrigger = null;
    180                     }
    181                 }
    182             }
    183         }
    184         catch (SchedulerException ex)
    185         {
    186             log.error("Error Initializing Scheduler:  " + ex.toString(), ex);
    187         }
    188         catch (ClassNotFoundException ex)
    189         {
    190             log.error("Error Loading class:  " + ex.toString(), ex);
    191         }
    192         catch (Exception ex)
    193         {
    194             log.error("Error:  " + ex.toString(), ex);
    195         }
    196     }
    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         Properties props = new Properties();
    208
    209         props.put("org.quartz.scheduler.instanceName", schdConf.getInstanceName());
    210
    211         ThreadPoolConfig threadPool = schdConf.getThreadPoolConfig();
    212         props.put("org.quartz.threadPool.class", threadPool.getClassName());
    213         props.put("org.quartz.threadPool.threadCount", threadPool.getThreadCount());
    214         props.put("org.quartz.threadPool.threadPriority", threadPool.getThreadPriority());
    215
    216         JobStoreConfig jobStore = schdConf.getJobStoreConfig();
    217         props.put("org.quartz.jobStore.class", jobStore.getClassName());
    218
    219         return props;
    220     }
    221
    222     /***
    223      * Loads the SchedulerConfig object from the xml file
    224      *
    225      * @param xmlLocation of the xml file
    226      *
    227      * @return TODO: DOCUMENT ME!
    228      *
    229      * @throws Exception
    230      */
    231     private SchedulerConfig getSchedulerConfig(String xmlLocation)
    232             throws Exception
    233     {
    234         FileInputStream in = new FileInputStream(xmlLocation);
    235
    236         // create a new BeanReader
    237         BeanReader reader = createBeanReader();
    238
    239         SchedulerConfig schedConf = (SchedulerConfig) reader.parse(in);
    240
    241         return schedConf;
    242     }
    243
    244     /***
    245      * Creates a beanreader
    246      *
    247      * @return beanreader
    248      *
    249      * @throws Exception
    250      */
    251     private BeanReader createBeanReader()
    252             throws Exception
    253     {
    254         BeanReader reader = new BeanReader();
    255         reader.setXMLIntrospector(createXMLIntrospector());
    256         reader.registerBeanClass(SchedulerConfig.class);
    257
    258         return reader;
    259     }
    260
    261     /***
    262      * Loads Xml Introspector with needed values
    263      *
    264      * @return introspector
    265      */
    266     private XMLIntrospector createXMLIntrospector()
    267     {
    268         XMLIntrospector introspector = new XMLIntrospector();
    269
    270         // set elements for attributes to true
    271         introspector.getConfiguration().setAttributesForPrimitives(false);
    272
    273         // wrap collections in an XML element
    274         //introspector.setWrapCollectionsInElement(true);
    275         // turn bean elements first letter into lower case
    276         introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
    277         introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
    278
    279         introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer());
    280
    281         return introspector;
    282     }
    283
    284     private TriggerConfig getTriggerConfig(SchedulerConfig schedConf, String jobName)
    285     {
    286         TriggerConfig trig = null;
    287         List triggers = schedConf.getTriggerConfigs();
    288
    289         for (int i = 0; i < triggers.size(); i++)
    290         {
    291             TriggerConfig tmpTrig = (TriggerConfig) triggers.get(i);
    292
    293             if (tmpTrig.getJobName().equals(jobName))
    294             {
    295                 trig = tmpTrig;
    296
    297                 return trig;
    298             }
    299         }
    300
    301         return trig;
    302     }
    303 }