KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > mybatchfwk > BatchConfiguration


1 /*
2  * MyBatchFramework - Open-source batch framework.
3  * Copyright (C) 2006 Jérôme Bertèche cyberteche@users.sourceforge.net
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * Jérôme Bertèche
16  * Email: cyberteche@users.sourceforge.net
17  */

18 package net.sf.mybatchfwk;
19
20 import java.lang.reflect.Field JavaDoc;
21 import java.util.Date JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import net.sf.mybatchfwk.utils.PropertiesLoader;
25 import net.sf.mybatchfwk.utils.Property;
26
27 /**
28  * Configuration of the batch.<br>
29  * It can be load from the configuration file 'my-batch-fwk.properties' by the method <i>getConfigurationProperties</i> or build by the user.
30  *
31  * @author Jérôme Bertèche (cyberteche@users.sourceforge.net)
32  */

33 public class BatchConfiguration {
34     
35     /**
36      * The name of the configuration file
37      */

38     public static final String JavaDoc CONFIGURATION_FILENAME = "my-batch-fwk.properties";
39     
40     /**
41      * The date format use to parse dates stored into the configuration file
42      */

43     public static final String JavaDoc DATE_FORMAT = "yyyy/MM/dd HH:mm:ss z";
44     
45     /**
46      * The properties manager
47      */

48     private PropertiesLoader propertiesLoader;
49     
50     
51     // ### batch configuration ###
52
/**
53      * The class name of the batch
54      */

55     @Property("mbf.batch.className")
56     private String JavaDoc batchClassName;
57     
58     /**
59      * The class name of the execution report
60      */

61     @Property("mbf.batch.executionReportClassName")
62     private String JavaDoc executionReportClassName;
63     
64     /**
65      * Indicate a deferred start of the batch. If true, then the batch can be started through JMX.
66      */

67     @Property("mbf.batch.deferredStart")
68     private boolean deferredStart = false;
69     
70     /**
71      * After the end of the batch, the main thread can sleep to permit JMX calls.
72      */

73     @Property("mbf.batch.stayAliveAfterShutdown")
74     private boolean stayAliveAfterShutdown = false;
75     
76     /**
77      * If true then the new tasks will be executed.
78      * (the execution history must be enable)
79      */

80     @Property("mbf.batch.executeNewTasks")
81     private boolean executeNewTasks = true;
82     
83     /**
84      * If true then the completed tasks will be executed.
85      * (the execution history must be enable)
86      */

87     @Property("mbf.batch.executeCompletedTasks")
88     private boolean executeCompletedTasks = true;
89     
90     /**
91      * If true then the failed tasks will be executed.
92      * (the execution history must be enable)
93      */

94     @Property("mbf.batch.executeFailedTasks")
95     private boolean executeFailedTasks = true;
96     
97     
98     // ### thread pool configuration ###
99
/**
100      * The minimum size of thread pool (threads that execute tasks)
101      */

102     @Property("mbf.threadPool.minSize")
103     private int threadPoolMinSize = 0;
104     
105     /**
106      * The maximum size of thread pool (threads that execute tasks)
107      */

108     @Property("mbf.threadPool.maxSize")
109     private int threadPoolMaxSize = 1;
110     
111     /**
112      * Blocking queue capacity.
113      * When the capacity of the tasks queue is reach, then the batch waits when it offer a new task to the TasksExecutor.<br>
114      * This capacity must be specified to prevents errors of type "OutOfMemoryError".<br>
115      * Default value: Integer.MAX_VALUE
116      */

117     @Property("mbf.threadPool.blockingQueueCapacity")
118     private int blockingQueueCapacity = Integer.MAX_VALUE;
119     
120     
121     // ### schedule configuration ###
122
/**
123      * Enable the schedule
124      */

125     @Property("mbf.schedule.enable")
126     private boolean scheduleEnable;
127     
128     /**
129      * The start date of the batch to delay the execution
130      * (the schedule must be enable)
131      */

132     @Property("mbf.schedule.startDate")
133     private Date JavaDoc scheduleStartDate;
134     
135     /**
136      * The period time to create periodic execution.
137      * (the schedule must be enable)
138      */

139     @Property("mbf.schedule.periodTime")
140     private long schedulePeriodTime = 0;
141     
142     /**
143      * The end date of the batch to fix a limit to the periodic execution.
144      * (the schedule must be enable; the period time must be defined)
145      */

146     @Property("mbf.schedule.endDate")
147     private Date JavaDoc scheduleEndDate;
148     
149     /**
150      * Number of loop for periodic execution
151      * (the schedule must be enable; the period time must be defined)
152      */

153     @Property("mbf.schedule.numberOfLoop")
154     private long scheduleNumberOfLoop;
155
156     
157     // ### jmx configuration ###
158
/**
159      * Enable/Disable JMX management
160      */

161     @Property("mbf.jmx.enable")
162     private boolean jmxEnable = false;
163     
164     /**
165      * Enable/Disable JMX notifications
166      */

167     @Property("mbf.jmx.enableNotifications")
168     private boolean jmxEnableNotifications = false;
169     
170     // ### execution history ###
171
/**
172      * The class name of the execution history
173      */

174     @Property("mbf.executionHistory.className")
175     private String JavaDoc executionHistoryClassName;
176     
177     
178     /**
179      * Default constructor: the default behavior is applicated.<br>
180      * Default behavior:<br>
181      * - Default execution report<br>
182      * - No history<br>
183      * - Infinite capacity of the blocking queue<br>
184      * - Infinite thread pool size<br>
185      * - Scheduler disabled<br>
186      * - JMX management disabled<br>
187      */

188     public BatchConfiguration() {
189         this.propertiesLoader = new PropertiesLoader(new Properties JavaDoc());
190     }
191     
192     /**
193      * Init the configuration from properties.<br>
194      * Init missing parameters by their default values.
195      *
196      * @param properties properties (most of the time build by the <i>getConfigurationProperties</i> method)
197      * @throws BatchException if a parameter is not of the waiting type
198      */

199     public BatchConfiguration(Properties JavaDoc properties) throws BatchException {
200         this.propertiesLoader = new PropertiesLoader(properties);
201         
202         // we load the configuration using the annotations
203
try {
204             this.propertiesLoader.assignProperties(this);
205         } catch (Exception JavaDoc e) {
206             throw new BatchException("an error occured during the loading of the configuration", e);
207         }
208     }
209     
210     /**
211      * Check the consistency of the configuration:<br>
212      * - the min size of the thread pool cannot be greater than the max size.<br>
213      * - if the schedule is enable at least one schedule parameter must be set.<br>
214      *
215      * @throws BatchException
216      */

217     protected void checkConsistency() throws BatchException {
218         
219         if (getThreadPoolMaxSize() < getThreadPoolMinSize()) {
220             throw new BatchException("thread pool: the minimum size cannot be greater than the maximum size");
221         }
222         
223         if ((isScheduleEnable()) && (getScheduleStartDate() == null) && (getScheduleEndDate() == null) && (getSchedulePeriodTime() <= 0)) {
224             throw new BatchException("schedule: at least one of this parameter must be set (startDate, endDate or periodTime)");
225         }
226     }
227     
228     @Override JavaDoc
229     public String JavaDoc toString() {
230         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc().append(getClass().getName()).append(" [");
231         
232         int i = 0;
233         for (Field JavaDoc field : PropertiesLoader.getPropertyFields(getClass())) {
234             if (i++ > 0) {
235                 buffer.append(", ");
236             }
237             try {
238                 buffer.append(field.getName()).append("='").append(field.get(this)).append("'");
239             } catch (Exception JavaDoc e) {}
240         }
241         buffer.append("]");
242         
243         return buffer.toString();
244     }
245     
246     /**
247      * @param executionHistoryClass
248      */

249     public void setExecutionHistoryClass(Class JavaDoc executionHistoryClass) {
250         this.executionHistoryClassName = executionHistoryClass.getName();
251     }
252     
253     
254     /**
255      * @param executionReportClass
256      */

257     public void setExecutionReportClass(Class JavaDoc executionReportClass) {
258         this.executionReportClassName = executionReportClass.getName();
259     }
260     
261     
262     /**
263      * @param batchClass
264      */

265     public void setBatchClass(Class JavaDoc batchClass) {
266         this.batchClassName = batchClass.getName();
267     }
268     
269     /**
270      * Return true if the tasks mut be filtered before to be execute
271      * @return
272      */

273     public boolean isTaskFilterEnable() {
274         return (!(executeNewTasks && executeCompletedTasks && executeFailedTasks));
275     }
276     
277     // ######################################################
278
// ################ GETTERS and SETTERS #################
279
// ######################################################
280

281     /**
282      * @return the batchClassName
283      */

284     public String JavaDoc getBatchClassName() {
285         return batchClassName;
286     }
287     /**
288      * @param batchClassName the batchClassName to set
289      */

290     public void setBatchClassName(String JavaDoc batchClassName) {
291         this.batchClassName = batchClassName;
292     }
293     /**
294      * @return the scheduleEnable
295      */

296     public boolean isScheduleEnable() {
297         return scheduleEnable;
298     }
299     /**
300      * @param scheduleEnable the scheduleEnable to set
301      */

302     public void setScheduleEnable(boolean scheduleEnabled) {
303         this.scheduleEnable = scheduleEnabled;
304     }
305     /**
306      * @return the scheduleEndDate
307      */

308     public Date JavaDoc getScheduleEndDate() {
309         return scheduleEndDate;
310     }
311     /**
312      * @param scheduleEndDate the scheduleEndDate to set
313      */

314     public void setScheduleEndDate(Date JavaDoc scheduleEndDate) {
315         this.scheduleEndDate = scheduleEndDate;
316     }
317     /**
318      * @return the scheduleNumberOfLoop
319      */

320     public long getScheduleNumberOfLoop() {
321         return scheduleNumberOfLoop;
322     }
323     /**
324      * @param scheduleNumberOfLoop the scheduleNumberOfLoop to set
325      */

326     public void setScheduleNumberOfLoop(long scheduleNumberOfLoop) {
327         this.scheduleNumberOfLoop = scheduleNumberOfLoop;
328     }
329     /**
330      * @return the schedulePeriodTime
331      */

332     public long getSchedulePeriodTime() {
333         return schedulePeriodTime;
334     }
335     /**
336      * @param schedulePeriodTime the schedulePeriodTime to set
337      */

338     public void setSchedulePeriodTime(long schedulePeriodTime) {
339         this.schedulePeriodTime = schedulePeriodTime;
340     }
341     /**
342      * @return the scheduleStartDate
343      */

344     public Date JavaDoc getScheduleStartDate() {
345         return scheduleStartDate;
346     }
347     /**
348      * @param scheduleStartDate the scheduleStartDate to set
349      */

350     public void setScheduleStartDate(Date JavaDoc scheduleStartDate) {
351         this.scheduleStartDate = scheduleStartDate;
352     }
353     /**
354      * @return the threadPoolMaxSize
355      */

356     public int getThreadPoolMaxSize() {
357         return threadPoolMaxSize;
358     }
359     /**
360      * @param threadPoolMaxSize the threadPoolMaxSize to set
361      */

362     public void setThreadPoolMaxSize(int threadPoolMaxSize) {
363         this.threadPoolMaxSize = threadPoolMaxSize;
364     }
365     /**
366      * @return the threadPoolMinSize
367      */

368     public int getThreadPoolMinSize() {
369         return threadPoolMinSize;
370     }
371     /**
372      * @param threadPoolMinSize the threadPoolMinSize to set
373      */

374     public void setThreadPoolMinSize(int threadPoolMinSize) {
375         this.threadPoolMinSize = threadPoolMinSize;
376     }
377
378     /**
379      * @return the deferredStart
380      */

381     public boolean isDeferredStart() {
382         return deferredStart;
383     }
384
385     /**
386      * @param deferredStart the deferredStart to set
387      */

388     public void setDeferredStart(boolean batchDeferredStart) {
389         this.deferredStart = batchDeferredStart;
390     }
391
392     /**
393      * @return the stayAliveAfterShutdown
394      */

395     public boolean isStayAliveAfterShutdown() {
396         return stayAliveAfterShutdown;
397     }
398
399     /**
400      * @param stayAliveAfterShutdown the stayAliveAfterShutdown to set
401      */

402     public void setStayAliveAfterShutdown(boolean stayAliveAfterShutdown) {
403         this.stayAliveAfterShutdown = stayAliveAfterShutdown;
404     }
405
406     /**
407      * @return Returns the jmxEnable.
408      */

409     public boolean isJmxEnable() {
410         return jmxEnable;
411     }
412
413     /**
414      * @param jmxEnable The jmxEnable to set.
415      */

416     public void setJmxEnable(boolean jmxEnable) {
417         this.jmxEnable = jmxEnable;
418     }
419
420     /**
421      * @return Returns the jmxEnableNotifications.
422      */

423     public boolean isJmxEnableNotifications() {
424         return jmxEnableNotifications;
425     }
426
427     /**
428      * @param jmxEnableNotifications The jmxEnableNotifications to set.
429      */

430     public void setJmxEnableNotifications(boolean jmxEnableNotifications) {
431         this.jmxEnableNotifications = jmxEnableNotifications;
432     }
433
434     /**
435      * @return Returns the executionReportClassName.
436      */

437     public String JavaDoc getExecutionReportClassName() {
438         return executionReportClassName;
439     }
440
441     /**
442      * @param executionReportClassName The executionReportClassName to set.
443      */

444     public void setExecutionReportClassName(String JavaDoc executionReportClassName) {
445         this.executionReportClassName = executionReportClassName;
446     }
447
448     /**
449      * @return Returns the blockingQueueCapacity.
450      */

451     public int getBlockingQueueCapacity() {
452         return blockingQueueCapacity;
453     }
454
455     /**
456      * @param blockingQueueCapacity The blockingQueueCapacity to set.
457      */

458     public void setBlockingQueueCapacity(int blockingQueueCapacity) {
459         this.blockingQueueCapacity = blockingQueueCapacity;
460     }
461
462     /**
463      * @return Returns the executionHistoryClassName.
464      */

465     public String JavaDoc getExecutionHistoryClassName() {
466         return executionHistoryClassName;
467     }
468
469     /**
470      * @param executionHistoryClassName The executionHistoryClassName to set.
471      */

472     public void setExecutionHistoryClassName(String JavaDoc historyStorageClassName) {
473         this.executionHistoryClassName = historyStorageClassName;
474     }
475
476     /**
477      * @return Returns the executeCompletedTasks.
478      */

479     public boolean isExecuteCompletedTasks() {
480         return executeCompletedTasks;
481     }
482
483     /**
484      * @param executeCompletedTasks The executeCompletedTasks to set.
485      */

486     public void setExecuteCompletedTasks(boolean executeCompletedTasks) {
487         this.executeCompletedTasks = executeCompletedTasks;
488     }
489
490     /**
491      * @return Returns the executeFailedTasks.
492      */

493     public boolean isExecuteFailedTasks() {
494         return executeFailedTasks;
495     }
496
497     /**
498      * @param executeFailedTasks The executeFailedTasks to set.
499      */

500     public void setExecuteFailedTasks(boolean executeFailedTasks) {
501         this.executeFailedTasks = executeFailedTasks;
502     }
503
504     /**
505      * @return Returns the executeNewTasks.
506      */

507     public boolean isExecuteNewTasks() {
508         return executeNewTasks;
509     }
510
511     /**
512      * @param executeNewTasks The executeNewTasks to set.
513      */

514     public void setExecuteNewTasks(boolean executeNewTasks) {
515         this.executeNewTasks = executeNewTasks;
516     }
517
518     /**
519      * @return the propertiesLoader
520      */

521     public PropertiesLoader getPropertiesLoader() {
522         return propertiesLoader;
523     }
524
525     /**
526      * @param propertiesLoader the propertiesLoader to set
527      */

528     public void setPropertiesLoader(PropertiesLoader propertiesManager) {
529         this.propertiesLoader = propertiesManager;
530     }
531 }
532
Popular Tags