KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > caadmin > CACacheManager


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.core.model.ca.caadmin;
15
16 import java.util.Date JavaDoc;
17 import java.util.Hashtable JavaDoc;
18
19 import org.apache.log4j.Logger;
20 import org.ejbca.core.ejb.ca.caadmin.CADataBean;
21
22
23 /**
24  * Class managing a cache of CAs. It is not really a cache, just an object registry.
25  *
26  * @version $Id: CACacheManager.java,v 1.2 2007/01/16 11:43:26 anatom Exp $
27  *
28  */

29 public class CACacheManager {
30     
31     /** Log4j instance for Base */
32     private static transient Logger log = Logger.getLogger(CACacheManager.class);
33
34     /** Registry of CAs, kept so CA */
35     private Hashtable JavaDoc caRegistry = new Hashtable JavaDoc();
36
37     /** Implementing the Singleton pattern */
38     private static CACacheManager instance = null;
39
40     /** Don't allow external creation of this class, implementing the Singleton pattern.
41      */

42     private CACacheManager() {}
43     
44     /** Get the instance of this singleton
45      *
46      */

47     public synchronized static CACacheManager instance() {
48         if (instance == null) {
49             instance = new CACacheManager();
50         }
51         return instance;
52     }
53     
54     /** Returns a previously registered (using addCAToken) CA, or null.
55      *
56      * @param caid the id of the CA whose CA object you want to fetch.
57      * @return The previously added CA or null if the CA does not exist in the registry.
58      */

59     public CA getCA(int caid, CADataBean caData) {
60         CA ret = (CA)caRegistry.get(new Integer JavaDoc(caid));
61         if (ret != null) {
62             // We mainly cache the xml data, some of the other values may change slightly at will...
63
ret.setStatus(caData.getStatus());
64             ret.setExpireTime(new Date JavaDoc(caData.getExpireTime()));
65             ret.setName(caData.getName());
66             ret.setSubjectDN(caData.getSubjectDN());
67             ret.setCAId(caid);
68         }
69         return ret;
70     }
71     
72     /** Adds a CA to the registry. If a CA already exists for the given CAid,
73      * the old one is removed and replaced with the new. If the CA passed is null, an existing CA is removed.
74      *
75      * @param caid the id of the CA you want to fetch.
76      * @param ca the CA to be added
77      */

78     public synchronized void addCA(int caid, CA ca) {
79         removeCA(caid);
80         if (ca != null) {
81             caRegistry.put(new Integer JavaDoc(caid), ca);
82             log.debug("Added CA to registry: "+caid);
83         }
84     }
85     
86     /** Removes a CA from the cache to force an update the next time the CA is read
87      *
88      */

89     public synchronized void removeCA(int caid) {
90         if (caRegistry.containsKey(new Integer JavaDoc(caid))) {
91             caRegistry.remove(new Integer JavaDoc(caid));
92             log.debug("Removed old CA from registry: "+caid);
93         }
94     }
95 }
96
Popular Tags