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 | 9 |
     public class Scheduler
48 |  |
             implements Configurable, Startable
49 |  |
     {
50 |  |
         /** Log4j category used for logging. */
51 | 27 |
         private static Category log = Category.getInstance(Scheduler.class);
52 | 2 |
53 |  |
         /** TODO: DOCUMENT ME! */
54 | 9 |
         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 | 9 |
             scheduler.start();
88 | 9 |
         }
89 |  |
90 |  |
         /**
91 |  |
          * stop the scheduler.
92 | 2 |
          *
93 | 2 |
          * @throws Exception
94 |  |
          */
95 |  |
         public void stop()
96 |  |
                 throws Exception
97 |  |
         {
98 | 9 |
             scheduler.shutdown();
99 | 9 |
         }
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 | 9 |
             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 | 9 |
                 SchedulerConfig schedConfig = getSchedulerConfig(xmlLocation);
118 |  |
                 Properties factoryProps;
119 | 2 |
120 |  |
                 try
121 |  |
                 {
122 | 9 |
                     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 | 9 |
                 }
142 |  |
143 | 9 |
                 schedulerFactory = new StdSchedulerFactory();
144 | 9 |
                 schedulerFactory.initialize(factoryProps);
145 | 2 |
146 | 9 |
                 scheduler = (StdScheduler) schedulerFactory.getScheduler();
147 | 2 |
148 | 11 |
                 List jobConfigs = schedConfig.getJobConfigs();
149 |  |
150 | 11 |
                 if (!jobConfigs.isEmpty())
151 |  |
                 {
152 | 29 |
                     for (int i = 0; i < jobConfigs.size(); i++)
153 |  |
                     {
154 | 20 |
                         JobConfig jobConf = (JobConfig) jobConfigs.get(i);
155 | 18 |
                         String jobName = (String) jobConf.getName();
156 | 6 |
157 | 18 |
                         String jobGroup = jobConf.getGroup();
158 | 22 |
                         String jobClassName = jobConf.getClassName();
159 | 4 |
160 | 18 |
                         JobDetail job = new JobDetail(jobName, jobGroup, Class.forName(jobClassName));
161 | 4 |
162 | 4 |
                         //get the trigger for this job if it exists
163 | 20 |
                         TriggerConfig triggerConf = getTriggerConfig(schedConfig, job.getName());
164 | 2 |
165 | 20 |
                         if (triggerConf != null)
166 |  |
                         {
167 | 18 |
                             job.getName();
168 | 2 |
169 | 20 |
                             String triggerGroup = triggerConf.getGroup();
170 | 20 |
                             String triggerName = triggerConf.getName();
171 | 20 |
                             String cronExpression =
172 | 2 |
                                 triggerConf.getSeconds() + " " + triggerConf.getMinutes() + " " + triggerConf.getHours() + " "
173 | 2 |
                                 + triggerConf.getDayOfMonth() + " " + triggerConf.getMonth() + " " + triggerConf.getDayOfWeek();
174 | 2 |
175 | 22 |
                             CronTrigger cronTrigger = new CronTrigger(triggerName, triggerGroup, jobName, jobGroup, cronExpression);
176 | 4 |
177 | 20 |
                             scheduler.scheduleJob(job, cronTrigger);
178 | 18 |
                             triggerConf = null;
179 | 18 |
                             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 | 9 |
             }
196 | 9 |
         }
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 | 9 |
             Properties props = new Properties();
208 | 1 |
209 | 11 |
             props.put("org.quartz.scheduler.instanceName", schdConf.getInstanceName());
210 | 1 |
211 | 9 |
             ThreadPoolConfig threadPool = schdConf.getThreadPoolConfig();
212 | 9 |
             props.put("org.quartz.threadPool.class", threadPool.getClassName());
213 | 9 |
             props.put("org.quartz.threadPool.threadCount", threadPool.getThreadCount());
214 | 9 |
             props.put("org.quartz.threadPool.threadPriority", threadPool.getThreadPriority());
215 |  |
216 | 10 |
             JobStoreConfig jobStore = schdConf.getJobStoreConfig();
217 | 10 |
             props.put("org.quartz.jobStore.class", jobStore.getClassName());
218 | 1 |
219 | 10 |
             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 | 9 |
             FileInputStream in = new FileInputStream(xmlLocation);
235 |  |
236 |  |
             // create a new BeanReader
237 | 9 |
             BeanReader reader = createBeanReader();
238 | 1 |
239 | 11 |
             SchedulerConfig schedConf = (SchedulerConfig) reader.parse(in);
240 | 1 |
241 | 10 |
             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 | 10 |
             BeanReader reader = new BeanReader();
255 | 9 |
             reader.setXMLIntrospector(createXMLIntrospector());
256 | 9 |
             reader.registerBeanClass(SchedulerConfig.class);
257 |  |
258 | 9 |
             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 | 10 |
             XMLIntrospector introspector = new XMLIntrospector();
269 | 1 |
270 | 1 |
             // set elements for attributes to true
271 | 11 |
             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 | 9 |
             introspector.getConfiguration().setElementNameMapper(new DecapitalizeNameMapper());
277 | 8 |
             introspector.getConfiguration().setAttributeNameMapper(new DecapitalizeNameMapper());
278 | 1 |
279 | 8 |
             introspector.getConfiguration().setPluralStemmer(new DefaultPluralStemmer());
280 | 1 |
281 | 8 |
             return introspector;
282 | 1 |
         }
283 | 2 |
284 | 2 |
         private TriggerConfig getTriggerConfig(SchedulerConfig schedConf, String jobName)
285 | 4 |
         {
286 | 19 |
             TriggerConfig trig = null;
287 | 16 |
             List triggers = schedConf.getTriggerConfigs();
288 | 3 |
289 | 24 |
             for (int i = 0; i < triggers.size(); i++)
290 | 3 |
             {
291 | 24 |
                 TriggerConfig tmpTrig = (TriggerConfig) triggers.get(i);
292 | 3 |
293 | 24 |
                 if (tmpTrig.getJobName().equals(jobName))
294 | 2 |
                 {
295 | 17 |
                     trig = tmpTrig;
296 | 3 |
297 | 16 |
                     return trig;
298 | 1 |
                 }
299 | 1 |
             }
300 |  |
301 | 0 |
             return trig;
302 |  |
         }
303 |  |
     }

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