KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > appserver > jboss > CRLCreateThread


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  
14 package org.ejbca.appserver.jboss;
15
16 import java.util.Calendar JavaDoc;
17 import java.util.Date JavaDoc;
18
19 import javax.ejb.EJBException JavaDoc;
20 import javax.naming.Context JavaDoc;
21 import javax.naming.InitialContext JavaDoc;
22
23 import org.apache.log4j.Logger;
24 import org.ejbca.core.model.log.Admin;
25
26 import org.ejbca.core.ejb.ca.crl.ICreateCRLSessionHome;
27 import org.ejbca.core.ejb.ca.crl.ICreateCRLSessionRemote;
28
29
30
31 /**
32  * A thread managing the automatic creation of CRLs.
33 */

34 public class CRLCreateThread extends Thread JavaDoc
35 {
36     private static Logger log = Logger.getLogger(CRLCreateThread.class);
37
38     /** Constants used internally. */
39   private static final int CHECK_DAILY = 1;
40   private static final int CHECK_HOURLY = 2;
41   private static final int CHECK_30MIN = 3;
42   private static final int CHECK_15MIN = 4;
43   private static final int CHECK_1MIN = 5;
44
45   /** We may create new CRLs if the current one expires within this the confiured overlap time + the pollTime */
46   private long m_pollTime = 1*60*1000; // Default polltime is 1 minute
47

48   private boolean run = false;
49   private int check = 0;
50   private ICreateCRLSessionRemote createcrlsession = null;
51   private Admin administrator = new Admin(Admin.TYPE_CACOMMANDLINE_USER);
52   
53    public CRLCreateThread(String JavaDoc polltime){
54         super();
55         
56         if(polltime.equalsIgnoreCase(CRLCreateService.POLLTIME_DAILY)){
57             check = CHECK_DAILY;
58             m_pollTime = 24*60*60*1000;
59         }
60         if(polltime.equalsIgnoreCase(CRLCreateService.POLLTIME_HOURLY)){
61           check = CHECK_HOURLY;
62           m_pollTime = 60*60*1000;
63         }
64         if(polltime.equalsIgnoreCase(CRLCreateService.POLLTIME_30MIN)){
65           check = CHECK_30MIN;
66           m_pollTime = 30*60*1000;
67         }
68         if(polltime.equalsIgnoreCase(CRLCreateService.POLLTIME_15MIN)){
69           check = CHECK_15MIN;
70           m_pollTime = 15*60*1000;
71         }
72         if(polltime.equalsIgnoreCase(CRLCreateService.POLLTIME_1MIN)){
73           check = CHECK_1MIN;
74           m_pollTime = 60*1000;
75         }
76         
77         try{
78             Context JavaDoc context = new InitialContext JavaDoc();
79             ICreateCRLSessionHome home = (ICreateCRLSessionHome) javax.rmi.PortableRemoteObject.narrow(context.lookup(
80             "CreateCRLSession"), ICreateCRLSessionHome.class);
81             this.createcrlsession = home.create();
82         }catch(Exception JavaDoc e){
83             throw new EJBException JavaDoc(e);
84         }
85         
86    }
87
88
89    public void run()
90    {
91        run=true;
92        while(run){
93            try{
94                sleep(getTimeToNextPoll());
95                try{
96                    if(run)
97                        // Create a new CRL if the old one expires within our polltime + an overlap time, so a CRL is always created
98
// at least the configured (in CA configuration) overlap time minutes before the old one expires.
99
// In this way we will in the worst case get the overlap time as the time
100
// when applications can get the new CRL before the old one expires.
101
this.createcrlsession.createCRLs(administrator, m_pollTime);
102                    log.debug("CRLCreateThread: createCRLs");
103                }catch(Exception JavaDoc e){
104                    log.error("Error generating CRLs: ", e);
105                }
106            }catch( InterruptedException JavaDoc e){}
107        }
108    }
109
110   public void stopThread()
111   {
112     this.run = false;
113     this.check = 0;
114   }
115   
116   
117   /**
118    * Method calculating the time in milliseconds to the next Poll
119    */

120   private long getTimeToNextPoll(){
121       long nexttime = 0;
122       Calendar JavaDoc nextcalendar = Calendar.getInstance();
123
124       switch(check){
125            case CHECK_DAILY :
126                nextcalendar.add(Calendar.DATE,1);
127                nextcalendar.set(Calendar.HOUR_OF_DAY,0);
128                nextcalendar.set(Calendar.MINUTE, 0);
129                nextcalendar.set(Calendar.SECOND, 0);
130                nexttime = nextcalendar.getTimeInMillis();
131                break;
132            case CHECK_HOURLY :
133                nextcalendar.add(Calendar.HOUR_OF_DAY,1);
134                nextcalendar.set(Calendar.MINUTE, 0);
135                nextcalendar.set(Calendar.SECOND, 0);
136                nexttime = nextcalendar.getTimeInMillis();
137                break;
138            case CHECK_30MIN :
139                nextcalendar.add(Calendar.MINUTE,(30 - (nextcalendar.get(Calendar.MINUTE) % 30)));
140                nextcalendar.set(Calendar.SECOND, 0);
141                nexttime = nextcalendar.getTimeInMillis();
142                break;
143            case CHECK_15MIN :
144                nextcalendar.add(Calendar.MINUTE,(15 - (nextcalendar.get(Calendar.MINUTE) % 15)));
145                nextcalendar.set(Calendar.SECOND, 0);
146                nexttime = nextcalendar.getTimeInMillis();
147                break;
148            case CHECK_1MIN :
149                nextcalendar.add(Calendar.MINUTE,1);
150                nextcalendar.set(Calendar.SECOND, 0);
151                nexttime = nextcalendar.getTimeInMillis();
152                break;
153            default :
154                log.error("Invalid Polltime set for CRLCreateService! Using 1 minute.");
155                nextcalendar.add(Calendar.MINUTE,1);
156                nextcalendar.set(Calendar.SECOND, 0);
157                nexttime = nextcalendar.getTimeInMillis();
158                break;
159                
160       }
161
162       return nexttime - (new Date JavaDoc()).getTime();
163   }
164
165  
166     
167 }
Popular Tags