KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > hardtoken > HardTokenProfileDataBean


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.ejb.hardtoken;
15
16 import java.io.UnsupportedEncodingException JavaDoc;
17 import java.util.HashMap JavaDoc;
18
19 import javax.ejb.CreateException JavaDoc;
20 import javax.ejb.EJBException JavaDoc;
21
22 import org.apache.log4j.Logger;
23 import org.ejbca.core.ejb.BaseEntityBean;
24 import org.ejbca.core.model.hardtoken.profiles.EnhancedEIDProfile;
25 import org.ejbca.core.model.hardtoken.profiles.HardTokenProfile;
26 import org.ejbca.core.model.hardtoken.profiles.SwedishEIDProfile;
27 import org.ejbca.core.model.hardtoken.profiles.TurkishEIDProfile;
28 import org.ejbca.util.Base64GetHashMap;
29 import org.ejbca.util.Base64PutHashMap;
30
31
32 /** Entity bean should not be used directly, use though Session beans.
33  *
34  * Entity Bean representing a hard token issuer in the ra.
35  * Information stored:
36  * <pre>
37  * id (Primary key)
38  * name (of the hard token profile)
39  * updatecount, help counter incremented each profile update used to check if a profile proxy class should update its data
40  * hardtokenprofile (Data saved concerning the hard token profile)
41  * </pre>
42  *
43  *
44  * @ejb.bean
45  * description="This enterprise bean entity represents a hard token profile with accompanying data"
46  * display-name="HardTokenProfileDataEB"
47  * name="HardTokenProfileData"
48  * jndi-name="HardTokenProfileData"
49  * local-jndi-name="HardTokenProfileDataLocal"
50  * view-type="local"
51  * type="CMP"
52  * reentrant="False"
53  * cmp-version="2.x"
54  * transaction-type="Container"
55  * schema="HardTokenProfileDataBean"
56  * primkey-field="id"
57  *
58  * @ejb.pk generate="false"
59  * class="java.lang.Integer"
60  *
61  * @ejb.persistence table-name = "HardTokenProfileData"
62  *
63  * @ejb.home
64  * generate="local"
65  * local-extends="javax.ejb.EJBLocalHome"
66  * local-class="org.ejbca.core.ejb.hardtoken.HardTokenProfileDataLocalHome"
67  *
68  * @ejb.interface
69  * generate="local"
70  * local-extends="javax.ejb.EJBLocalObject"
71  * local-class="org.ejbca.core.ejb.hardtoken.HardTokenProfileDataLocal"
72  *
73  * @ejb.finder
74  * description="findByName"
75  * signature="org.ejbca.core.ejb.hardtoken.HardTokenProfileDataLocal findByName(java.lang.String name)"
76  * query="SELECT OBJECT(a) from HardTokenProfileDataBean a WHERE a.name=?1"
77  *
78  * @ejb.finder
79  * description="findAll"
80  * signature="java.util.Collection findAll()"
81  * query="SELECT OBJECT(a) from HardTokenProfileDataBean a"
82  *
83  * @ejb.transaction type="Required"
84  *
85  * @jonas.jdbc-mapping
86  * jndi-name="${datasource.jndi-name}"
87  *
88  */

89 public abstract class HardTokenProfileDataBean extends BaseEntityBean {
90
91     private static final Logger log = Logger.getLogger(HardTokenProfileDataBean.class);
92
93     /**
94      * @ejb.pk-field
95      * @ejb.persistence column-name="id"
96      * @ejb.interface-method view-type="local"
97      */

98     public abstract Integer JavaDoc getId();
99
100     /**
101      */

102     public abstract void setId(Integer JavaDoc id);
103
104     /**
105      * @ejb.persistence column-name="name"
106      * @ejb.interface-method view-type="local"
107      */

108     public abstract String JavaDoc getName();
109
110     /**
111      * @ejb.interface-method view-type="local"
112      */

113     public abstract void setName(String JavaDoc name);
114
115     /**
116      * @ejb.persistence column-name="updateCounter"
117      * @ejb.interface-method view-type="local"
118      */

119     public abstract int getUpdateCounter();
120
121     /**
122      */

123     public abstract void setUpdateCounter(int updatecounter);
124
125     /**
126      * @ejb.persistence jdbc-type="LONGVARCHAR" column-name="data"
127      */

128     public abstract String JavaDoc getData();
129
130     /**
131      */

132     public abstract void setData(String JavaDoc data);
133
134
135
136     /**
137      * Method that returns the hard token profile data and updates it if nessesary.
138      * @ejb.interface-method view-type="local"
139      */

140     public HardTokenProfile getHardTokenProfile() {
141         
142         HardTokenProfile profile = null;
143         java.beans.XMLDecoder JavaDoc decoder;
144         try {
145             decoder = new java.beans.XMLDecoder JavaDoc(
146                         new java.io.ByteArrayInputStream JavaDoc(getData().getBytes("UTF8")));
147         } catch (UnsupportedEncodingException JavaDoc e) {
148             throw new EJBException JavaDoc(e);
149         }
150         HashMap JavaDoc h = (HashMap JavaDoc) decoder.readObject();
151         decoder.close();
152         // Handle Base64 encoded string values
153
HashMap JavaDoc data = new Base64GetHashMap(h);
154         
155         switch (((Integer JavaDoc) (data.get(HardTokenProfile.TYPE))).intValue()) {
156         case SwedishEIDProfile.TYPE_SWEDISHEID :
157             profile = new SwedishEIDProfile();
158             break;
159         case EnhancedEIDProfile.TYPE_ENHANCEDEID:
160             profile = new EnhancedEIDProfile();
161             break;
162         case TurkishEIDProfile.TYPE_TURKISHEID :
163             profile = new TurkishEIDProfile();
164             break;
165         }
166         
167         profile.loadData(data);
168         
169         return profile;
170     }
171
172     /**
173      * Method that saves the hard token profile data to database.
174      * @ejb.interface-method view-type="local"
175      */

176     public void setHardTokenProfile(HardTokenProfile hardtokenprofile){
177         // We must base64 encode string for UTF safety
178
HashMap JavaDoc a = new Base64PutHashMap();
179         a.putAll((HashMap JavaDoc)hardtokenprofile.saveData());
180         
181         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
182         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
183         encoder.writeObject(a);
184         encoder.close();
185
186         try {
187             if (log.isDebugEnabled()) {
188                 log.debug("Profiledata: \n" + baos.toString("UTF8"));
189             }
190             setData(baos.toString("UTF8"));
191         } catch (UnsupportedEncodingException JavaDoc e) {
192           throw new EJBException JavaDoc(e);
193         }
194
195         setUpdateCounter(getUpdateCounter() +1);
196     }
197
198
199     //
200
// Fields required by Container
201
//
202

203
204     /**
205      * Entity Bean holding data of a ahrd token issuer.
206      *
207      * @return null
208      * @ejb.create-method view-type="local"
209      */

210     public Integer JavaDoc ejbCreate(Integer JavaDoc id, String JavaDoc name, HardTokenProfile profile) throws CreateException JavaDoc {
211         setId(id);
212         setName(name);
213         this.setUpdateCounter(0);
214         if(profile != null)
215           setHardTokenProfile(profile);
216
217         log.debug("Created Hard Token Profile "+ name );
218         return id;
219     }
220
221     public void ejbPostCreate(Integer JavaDoc id, String JavaDoc name, HardTokenProfile profile) {
222         // Do nothing. Required.
223
}
224 }
225
Popular Tags