KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > services > intervals > PeriodicalInterval


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software 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 any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13 package org.ejbca.core.model.services.intervals;
14
15 import javax.ejb.EJBException JavaDoc;
16
17 import org.apache.log4j.Logger;
18 import org.ejbca.core.model.InternalResources;
19 import org.ejbca.core.model.services.BaseInterval;
20
21 /**
22  * Class implementing a peridical IInterval for monitoring services
23  *
24  * The main method is getTimeToExecution
25  *
26  * @author Philip Vendil 2006 sep 27
27  *
28  * @version $Id: PeriodicalInterval.java,v 1.4 2006/12/13 10:35:10 anatom Exp $
29  */

30 public class PeriodicalInterval extends BaseInterval {
31     
32     private static final Logger log = Logger.getLogger(PeriodicalInterval.class);
33     /** Internal localization of logs and errors */
34     private static final InternalResources intres = InternalResources.getInstance();
35
36     public static final String JavaDoc PROP_UNIT = "interval.periodical.unit";
37     public static final String JavaDoc PROP_VALUE = "interval.periodical.value";
38
39     public static final String JavaDoc UNIT_SECONDS = "SECONDS";
40     public static final String JavaDoc UNIT_MINUTES = "MINUTES";
41     public static final String JavaDoc UNIT_HOURS = "HOURS";
42     public static final String JavaDoc UNIT_DAYS = "DAYS";
43     
44     public static final int UNITVAL_SECONDS = 1;
45     public static final int UNITVAL_MINUTES = 60;
46     public static final int UNITVAL_HOURS = 3600;
47     public static final int UNITVAL_DAYS = 86400;
48
49     public static final String JavaDoc[] AVAILABLE_UNITS = {UNIT_SECONDS, UNIT_MINUTES, UNIT_HOURS, UNIT_DAYS};
50     public static final int[] AVAILABLE_UNITSVALUES = {UNITVAL_SECONDS, UNITVAL_MINUTES, UNITVAL_HOURS, UNITVAL_DAYS};
51     
52     
53     private transient int interval = 0;
54     
55     /**
56      * Methods that reads the interval from the configured properties
57      * and transforms it into seconds
58      *
59      *
60      * @see org.ejbca.core.model.services.IInterval#getTimeToExecution()
61      */

62     public long getTimeToExecution() {
63         log.debug(">PeriodicalInterval.getTimeToExecution()");
64         if(interval == 0){
65             String JavaDoc unit = properties.getProperty(PROP_UNIT);
66             if(unit == null){
67                 String JavaDoc msg = intres.getLocalizedMessage("services.interval.errorconfig", serviceName, "UNIT");
68                 throw new EJBException JavaDoc(msg);
69             }
70             int unitval = 0;
71             for(int i=0;i<AVAILABLE_UNITS.length;i++){
72                 if(AVAILABLE_UNITS[i].equalsIgnoreCase(unit)){
73                     unitval = AVAILABLE_UNITSVALUES[i];
74                     break;
75                 }
76             }
77             if(unitval == 0){
78                 String JavaDoc msg = intres.getLocalizedMessage("services.interval.errorconfig", serviceName, "UNIT");
79                 throw new EJBException JavaDoc(msg);
80             }
81                         
82             String JavaDoc value = properties.getProperty(PROP_VALUE);
83             int intvalue = 0;
84             try{
85               intvalue = Integer.parseInt(value);
86             }catch(NumberFormatException JavaDoc e){
87                 String JavaDoc msg = intres.getLocalizedMessage("services.interval.errorconfig", serviceName, "VALUE");
88                 throw new EJBException JavaDoc(msg);
89             }
90             
91             if(intvalue == 0){
92                 String JavaDoc msg = intres.getLocalizedMessage("services.interval.errorconfig", serviceName, "UNIT");
93                 throw new EJBException JavaDoc(msg);
94             }
95             interval = intvalue * unitval;
96         }
97         log.debug("PeriodicalInterval.getTimeToExecution() : " + interval);
98         return interval;
99     }
100
101 }
102
Popular Tags