KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > ca > publisher > PublisherDataBean


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

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

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

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

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

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

120     public abstract int getUpdateCounter();
121
122     /**
123      * @ejb.interface-method view-type="local"
124      */

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

130     public abstract String JavaDoc getData();
131
132     /**
133      */

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

141     public BasePublisher getPublisher() {
142
143         if (publisher == null) {
144             java.beans.XMLDecoder JavaDoc decoder;
145             try {
146                 decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(getData().getBytes("UTF8")));
147             } catch (UnsupportedEncodingException JavaDoc e) {
148                 throw new EJBException JavaDoc(e);
149             }
150             HashMap h = (HashMap) decoder.readObject();
151             decoder.close();
152             // Handle Base64 encoded string values
153
HashMap data = new Base64GetHashMap(h);
154
155             switch (((Integer JavaDoc) (data.get(BasePublisher.TYPE))).intValue()) {
156                 case LdapPublisher.TYPE_LDAPPUBLISHER:
157                     publisher = new LdapPublisher();
158                     break;
159                 case LdapSearchPublisher.TYPE_LDAPSEARCHPUBLISHER:
160                       publisher = new LdapSearchPublisher();
161                       break;
162                 case ActiveDirectoryPublisher.TYPE_ADPUBLISHER:
163                     publisher = new ActiveDirectoryPublisher();
164                     break;
165                 case CustomPublisherContainer.TYPE_CUSTOMPUBLISHERCONTAINER:
166                     publisher = new CustomPublisherContainer();
167                     break;
168             }
169
170             publisher.loadData(data);
171         }
172
173         return publisher;
174     }
175
176     /**
177      * Method that saves the publisher data to database.
178      *
179      * @ejb.interface-method view-type="local"
180      */

181     public void setPublisher(BasePublisher publisher) {
182         // We must base64 encode string for UTF safety
183
HashMap a = new Base64PutHashMap();
184         a.putAll((HashMap)publisher.saveData());
185         
186         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
187         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
188         encoder.writeObject(a);
189         encoder.close();
190
191         try {
192             if (log.isDebugEnabled()) {
193                 log.debug("Profiledata: \n" + baos.toString("UTF8"));
194             }
195             setData(baos.toString("UTF8"));
196         } catch (UnsupportedEncodingException JavaDoc e) {
197             throw new EJBException JavaDoc(e);
198         }
199
200         this.publisher = publisher;
201         setUpdateCounter(getUpdateCounter() + 1);
202     }
203
204
205     //
206
// Fields required by Container
207
//
208
/**
209      * Passivates bean, resets CA data.
210      */

211     public void ejbPassivate() {
212         this.publisher = null;
213     }
214
215
216     /**
217      * Entity Bean holding data of a publisher.
218      *
219      * @return null
220      * @ejb.create-method view-type="local"
221      */

222     public Integer JavaDoc ejbCreate(Integer JavaDoc id, String JavaDoc name, BasePublisher publisher) throws CreateException JavaDoc {
223         setId(id);
224         setName(name);
225         this.setUpdateCounter(0);
226         if (publisher != null)
227             setPublisher(publisher);
228
229         log.debug("Created Hard Token Profile " + name);
230         return id;
231     }
232
233     public void ejbPostCreate(Integer JavaDoc id, String JavaDoc name, BasePublisher publisher) {
234         // Do nothing. Required.
235
}
236 }
237
Popular Tags