KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > services > ServiceDataBean


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

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

94     public abstract Integer JavaDoc getId();
95
96     /**
97      */

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

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

109     public abstract void setName(String JavaDoc name);
110
111
112     /**
113      * @ejb.persistence jdbc-type="LONGVARCHAR" column-name="data"
114      */

115     public abstract String JavaDoc getData();
116
117     /**
118      */

119     public abstract void setData(String JavaDoc data);
120
121     /**
122      * Method that returns the service configuration data and updates it if nessesary.
123      *
124      * @ejb.interface-method view-type="local"
125      * @jboss.persistence row-locking="true"
126      */

127     public ServiceConfiguration getServiceConfiguration() {
128
129         if (serviceConfiguration == null) {
130             java.beans.XMLDecoder JavaDoc decoder;
131             try {
132                 decoder = new java.beans.XMLDecoder JavaDoc(new java.io.ByteArrayInputStream JavaDoc(getData().getBytes("UTF8")));
133             } catch (UnsupportedEncodingException JavaDoc e) {
134                 throw new EJBException JavaDoc(e);
135             }
136             HashMap JavaDoc h = (HashMap JavaDoc) decoder.readObject();
137             decoder.close();
138             // Handle Base64 encoded string values
139
HashMap JavaDoc data = new Base64GetHashMap(h);
140
141             serviceConfiguration = new ServiceConfiguration();
142             serviceConfiguration.loadData(data);
143         }
144
145         return serviceConfiguration;
146     }
147
148     /**
149      * Method that saves the service configuration data to database.
150      *
151      * @ejb.interface-method view-type="local"
152      */

153     public void setServiceConfiguration(ServiceConfiguration serviceConfiguration) {
154         // We must base64 encode string for UTF safety
155
HashMap JavaDoc a = new Base64PutHashMap();
156         a.putAll((HashMap JavaDoc)serviceConfiguration.saveData());
157         
158         java.io.ByteArrayOutputStream JavaDoc baos = new java.io.ByteArrayOutputStream JavaDoc();
159         java.beans.XMLEncoder JavaDoc encoder = new java.beans.XMLEncoder JavaDoc(baos);
160         encoder.writeObject(a);
161         encoder.close();
162
163         try {
164             if (log.isDebugEnabled()) {
165                 log.debug("Service data: \n" + baos.toString("UTF8"));
166             }
167             setData(baos.toString("UTF8"));
168         } catch (UnsupportedEncodingException JavaDoc e) {
169             throw new EJBException JavaDoc(e);
170         }
171
172         this.serviceConfiguration = serviceConfiguration;
173     }
174
175
176     //
177
// Fields required by Container
178
//
179
/**
180      * Passivates bean, resets CA data.
181      */

182     public void ejbPassivate() {
183         this.serviceConfiguration = null;
184     }
185
186
187     /**
188      * Entity Bean holding data of a service configuration.
189      *
190      * @return null
191      * @ejb.create-method view-type="local"
192      */

193     public Integer JavaDoc ejbCreate(Integer JavaDoc id, String JavaDoc name, ServiceConfiguration serviceConfiguration) throws CreateException JavaDoc {
194         setId(id);
195         setName(name);
196
197         if (serviceConfiguration != null)
198             setServiceConfiguration(serviceConfiguration);
199
200         log.debug("Created Service Configuration " + name);
201         return id;
202     }
203
204     public void ejbPostCreate(Integer JavaDoc id, String JavaDoc name, ServiceConfiguration serviceConfiguration) {
205         // Do nothing. Required.
206
}
207 }
208
Popular Tags