KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ra > userdatasource > UserDataSourceDataBean


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.ra.userdatasource;
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.ra.userdatasource.BaseUserDataSource;
25 import org.ejbca.core.model.ra.userdatasource.CustomUserDataSourceContainer;
26 import org.ejbca.util.Base64GetHashMap;
27 import org.ejbca.util.Base64PutHashMap;
28
29 /**
30  * Entity bean should not be used directly, use though Session beans.
31  *
32  * Entity Bean representing a user data source to the ra
33  * Information stored:
34  * <pre>
35  * id (Primary key)
36  * name (of the user data source)
37  * updatecount, help counter incremented each update used to check if a user data source proxy class should update its data
38  * userdatasource (Data saved concerning the user data source)
39  * </pre>
40  *
41  * @ejb.bean
42  * description="This enterprise bean entity represents a user data source"
43  * display-name="UserDataSourceDataEB"
44  * name="UserDataSourceData"
45  * jndi-name="UserDataSourceData"
46  * local-jndi-name="UserDataSourceDataLocal"
47  * view-type="local"
48  * type="CMP"
49  * reentrant="False"
50  * cmp-version="2.x"
51  * transaction-type="Container"
52  * schema="UserDataSourceDataBean"
53  * primkey-field="id"
54  *
55  * @ejb.pk generate="false"
56  * class="java.lang.Integer"
57  *
58  * @ejb.persistence table-name = "UserDataSourceData"
59  *
60  * @ejb.home
61  * generate="local"
62  * local-extends="javax.ejb.EJBLocalHome"
63  * local-class="org.ejbca.core.ejb.ra.userdatasource.UserDataSourceDataLocalHome"
64  *
65  * @ejb.interface
66  * generate="local"
67  * local-extends="javax.ejb.EJBLocalObject"
68  * local-class="org.ejbca.core.ejb.ra.userdatasource.UserDataSourceDataLocal"
69  *
70  * @ejb.finder
71  * description="findByName"
72  * signature="org.ejbca.core.ejb.ra.userdatasource.UserDataSourceDataLocal findByName(java.lang.String name)"
73  * query="SELECT OBJECT(a) from UserDataSourceDataBean a WHERE a.name=?1"
74  *
75  * @ejb.finder
76  * description="findAll"
77  * signature="Collection findAll()"
78  * query="SELECT OBJECT(a) from UserDataSourceDataBean a"
79  *
80  * @ejb.transaction type="Required"
81  *
82  * @jonas.jdbc-mapping
83  * jndi-name="${datasource.jndi-name}"
84  */

85 public abstract class UserDataSourceDataBean extends BaseEntityBean {
86
87     private static final Logger log = Logger.getLogger(UserDataSourceDataBean.class);
88
89     private BaseUserDataSource userdatasource = null;
90
91     /**
92      * @ejb.pk-field
93      * @ejb.persistence column-name="id"
94      * @ejb.interface-method view-type="local"
95      */

96     public abstract Integer JavaDoc getId();
97
98     /**
99      */

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

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

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

117     public abstract int getUpdateCounter();
118
119     /**
120      * @ejb.interface-method view-type="local"
121      */

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

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

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

138     public BaseUserDataSource getUserDataSource() {
139
140         if (userdatasource == null) {
141             java.beans.XMLDecoder JavaDoc decoder;
142             try {
143                 decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(getData().getBytes("UTF8")));
144             } catch (UnsupportedEncodingException JavaDoc e) {
145                 throw new EJBException JavaDoc(e);
146             }
147             HashMap JavaDoc h = (HashMap JavaDoc) decoder.readObject();
148             decoder.close();
149             // Handle Base64 encoded string values
150
HashMap JavaDoc data = new Base64GetHashMap(h);
151
152             switch (((Integer JavaDoc) (data.get(BaseUserDataSource.TYPE))).intValue()) {
153                 case CustomUserDataSourceContainer.TYPE_CUSTOMUSERDATASOURCECONTAINER:
154                     userdatasource = new CustomUserDataSourceContainer();
155                     break;
156             }
157
158             userdatasource.loadData(data);
159         }
160
161         return userdatasource;
162     }
163
164     /**
165      * Method that saves the userdatasource data to database.
166      *
167      * @ejb.interface-method view-type="local"
168      */

169     public void setUserDataSource(BaseUserDataSource userdatasource) {
170         // We must base64 encode string for UTF safety
171
HashMap JavaDoc a = new Base64PutHashMap();
172         a.putAll((HashMap JavaDoc)userdatasource.saveData());
173         
174         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
175         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
176         encoder.writeObject(a);
177         encoder.close();
178
179         try {
180             if (log.isDebugEnabled()) {
181                 log.debug("Profiledata: \n" + baos.toString("UTF8"));
182             }
183             setData(baos.toString("UTF8"));
184         } catch (UnsupportedEncodingException JavaDoc e) {
185             throw new EJBException JavaDoc(e);
186         }
187
188         this.userdatasource = userdatasource;
189         setUpdateCounter(getUpdateCounter() + 1);
190     }
191
192
193     //
194
// Fields required by Container
195
//
196
/**
197      * Passivates bean, resets CA data.
198      */

199     public void ejbPassivate() {
200         this.userdatasource = null;
201     }
202
203
204     /**
205      * Entity Bean holding data of a userdatasource.
206      *
207      * @return null
208      * @ejb.create-method view-type="local"
209      */

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