KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > model > ca > catoken > HardCATokenContainer


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.catoken;
15
16 import java.io.ByteArrayInputStream JavaDoc;
17 import java.io.IOException JavaDoc;
18 import java.security.PrivateKey JavaDoc;
19 import java.security.PublicKey JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.ejb.EJBException JavaDoc;
25
26
27
28
29 /**
30  * HardCATokenContainer is a class managing the persistent storage of a hardcatoken publisher.
31  *
32  *
33  * @version $Id: HardCATokenContainer.java,v 1.4 2006/05/28 14:21:11 anatom Exp $
34  */

35 public class HardCATokenContainer extends CAToken{
36     private IHardCAToken hardcatoken = null;
37     
38     public static final float LATEST_VERSION = 1;
39     
40     public static final int TYPE_CUSTOMPUBLISHERCONTAINER = 1;
41     
42     // Default Values
43

44     protected static final String JavaDoc CLASSPATH = "classpath";
45     protected static final String JavaDoc PROPERTYDATA = "propertydata";
46         
47     
48     
49     public HardCATokenContainer(){
50         super();
51         data.put(CATOKENTYPE, new Integer JavaDoc(CATokenInfo.CATOKENTYPE_HSM));
52         setClassPath(null);
53         setPropertyData("");
54     }
55     
56     public HardCATokenContainer(HashMap JavaDoc data) {
57         loadData(data);
58         data.put(CATOKENTYPE, new Integer JavaDoc(CATokenInfo.CATOKENTYPE_HSM));
59      }
60     
61     // Public Methods
62

63     /**
64      * Returns the current hardcatoken configuration.
65      */

66     public CATokenInfo getCATokenInfo() {
67         HardCATokenInfo info = new HardCATokenInfo();
68         info.setClassPath(getClassPath());
69         info.setProperties(getPropertyData());
70         info.setSignatureAlgorithm(getSignatureAlgorithm());
71         if ( hardcatoken!=null ){
72             info.setCATokenStatus(hardcatoken.getCATokenStatus());
73         }else{
74             info.setCATokenStatus(IHardCAToken.STATUS_OFFLINE);
75         }
76             
77         return info;
78     }
79
80     /**
81      * Updates the hardcatoken configuration
82      */

83     public void updateCATokenInfo(CATokenInfo catokeninfo) {
84         // We must be able to upgrade class path
85
if (((HardCATokenInfo)catokeninfo).getClassPath() != null) {
86               this.setClassPath(((HardCATokenInfo)catokeninfo).getClassPath());
87               this.hardcatoken = null;
88         }
89         if(getSignatureAlgorithm() == null)
90           this.setSignatureAlgorithm(catokeninfo.getSignatureAlgorithm());
91         
92         if(!this.getPropertyData().equals(((HardCATokenInfo)catokeninfo).getProperties())){
93           this.setPropertyData(((HardCATokenInfo)catokeninfo).getProperties());
94           this.hardcatoken = null;
95         }
96     }
97
98     /**
99      * @see org.ejbca.core.model.ca.catoken.CAToken#activate(java.lang.String)
100      */

101     public void activate(String JavaDoc authorizationcode) throws CATokenAuthenticationFailedException, CATokenOfflineException {
102         getHardCAToken().activate(authorizationcode);
103     }
104
105     /**
106      * @see org.ejbca.core.model.ca.catoken.CAToken#deactivate()
107      */

108     public boolean deactivate() {
109         return getHardCAToken().deactivate();
110     }
111
112     
113     /**
114      * @see org.ejbca.core.model.ca.catoken.CAToken#getPrivateKey()
115      */

116     public PrivateKey JavaDoc getPrivateKey(int purpose) throws CATokenOfflineException{
117         return getHardCAToken().getPrivateKey(purpose);
118     }
119
120     /**
121      * @see org.ejbca.core.model.ca.catoken.CAToken#getPublicKey()
122      */

123     public PublicKey JavaDoc getPublicKey(int purpose) throws CATokenOfflineException{
124         return getHardCAToken().getPublicKey(purpose);
125     }
126
127
128     /**
129      * @see org.ejbca.core.model.ca.catoken.CAToken#getProvider()
130      */

131     public String JavaDoc getProvider() {
132         return getHardCAToken().getProvider();
133     }
134
135     
136     
137     // Private methods
138
/**
139      * Returns the class path of custom publisher used.
140      */

141     private String JavaDoc getClassPath(){
142         return (String JavaDoc) data.get(CLASSPATH);
143     }
144
145     /**
146      * Sets the class path of custom publisher used.
147      */

148     private void setClassPath(String JavaDoc classpath){
149       data.put(CLASSPATH, classpath);
150     }
151     
152     /**
153      * Returns the class path of custom publisher used.
154      */

155     private String JavaDoc getSignatureAlgorithm(){
156         return (String JavaDoc) data.get(SIGNATUREALGORITHM);
157     }
158
159     /**
160      * Sets the SignatureAlgoritm
161      */

162     private void setSignatureAlgorithm(String JavaDoc signaturealgoritm){
163       data.put(SIGNATUREALGORITHM, signaturealgoritm);
164     }
165
166
167     /**
168      * Returns the propertydata used to configure this custom publisher.
169      */

170     private String JavaDoc getPropertyData(){
171         return (String JavaDoc) data.get(PROPERTYDATA);
172     }
173
174     /**
175      * Sets the propertydata used to configure this custom publisher.
176      */

177     private void setPropertyData(String JavaDoc propertydata){
178         data.put(PROPERTYDATA, propertydata);
179     }
180     
181     private Properties JavaDoc getProperties() throws IOException JavaDoc{
182         Properties JavaDoc prop = new Properties JavaDoc();
183         prop.load(new ByteArrayInputStream JavaDoc(getPropertyData().getBytes()));
184         return prop;
185     }
186
187     
188     
189     private IHardCAToken getHardCAToken() {
190         if(hardcatoken == null){
191             try{
192                 Class JavaDoc implClass = Class.forName( getClassPath());
193                 Object JavaDoc obj = implClass.newInstance();
194                 this.hardcatoken = (IHardCAToken) obj;
195                 this.hardcatoken.init(getProperties(), getSignatureAlgorithm());
196             }catch(ClassNotFoundException JavaDoc e){
197                 throw new EJBException JavaDoc(e);
198             }
199             catch(IllegalAccessException JavaDoc iae){
200                 throw new EJBException JavaDoc(iae);
201             }
202             catch(IOException JavaDoc ioe){
203                 throw new EJBException JavaDoc(ioe);
204             }
205             catch(InstantiationException JavaDoc ie){
206                 throw new EJBException JavaDoc(ie);
207             }
208         }
209         
210         return hardcatoken;
211     }
212         
213     /**
214      * @see org.ejbca.core.model.ca.publisher.BasePublisher#clone()
215      */

216     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
217         HardCATokenContainer clone = new HardCATokenContainer();
218         HashMap JavaDoc clonedata = (HashMap JavaDoc) clone.saveData();
219
220         Iterator JavaDoc i = (data.keySet()).iterator();
221         while(i.hasNext()){
222             Object JavaDoc key = i.next();
223             clonedata.put(key, data.get(key));
224         }
225
226         clone.loadData(clonedata);
227         return clone;
228     }
229
230     /**
231      * @see org.ejbca.core.model.ca.publisher.BasePublisher#getLatestVersion()
232      */

233     public float getLatestVersion() {
234         return LATEST_VERSION;
235     }
236
237
238
239     public void upgrade() {
240         if(Float.compare(LATEST_VERSION, getVersion()) != 0) {
241             // New version of the class, upgrade
242

243             data.put(VERSION, new Float JavaDoc(LATEST_VERSION));
244           }
245     }
246     
247
248 }
249
Popular Tags