KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > alarm > beans > AlarmRecordBean


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: JOnAS Team
22  * --------------------------------------------------------------------------
23  * $Id: AlarmRecordBean.java,v 1.4 2004/04/09 12:56:41 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.alarm.beans;
28
29 import java.rmi.RemoteException JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.Date JavaDoc;
32 import java.sql.SQLException JavaDoc;
33 import java.sql.Statement JavaDoc;
34
35 import javax.ejb.CreateException JavaDoc;
36 import javax.ejb.DuplicateKeyException JavaDoc;
37 import javax.ejb.EJBException JavaDoc;
38 import javax.ejb.EntityBean JavaDoc;
39 import javax.ejb.EntityContext JavaDoc;
40 import javax.ejb.RemoveException JavaDoc;
41 import javax.naming.InitialContext JavaDoc;
42 import javax.sql.DataSource JavaDoc;
43
44 /**
45  *
46  */

47 public class AlarmRecordBean implements EntityBean JavaDoc {
48
49     EntityContext JavaDoc ejbContext;
50
51     // ------------------------------------------------------------------
52
// State of the bean.
53
// They must be public for Container Managed Persistance.
54
// ------------------------------------------------------------------
55
public String JavaDoc pk;
56
57     public int sev;
58
59     public String JavaDoc from;
60
61     public String JavaDoc reason;
62
63     public int count;
64
65     public int state;
66
67     public Date JavaDoc date;
68
69     // ------------------------------------------------------------------
70
// init DataBase
71
// ------------------------------------------------------------------
72
private void initDB() {
73
74         // Get my DataSource from JNDI
75
DataSource JavaDoc ds = null;
76         InitialContext JavaDoc ictx = null;
77         try {
78             ictx = new InitialContext JavaDoc();
79         } catch (Exception JavaDoc e) {
80             throw new EJBException JavaDoc("Cannot get JNDI InitialContext");
81         }
82         try {
83             ds = (DataSource JavaDoc) ictx.lookup("java:comp/env/jdbc/myDS");
84         } catch (Exception JavaDoc e) {
85             throw new EJBException JavaDoc("cannot lookup datasource");
86         }
87
88         // Drop table
89
Connection JavaDoc conn = null;
90         Statement JavaDoc stmt = null;
91         String JavaDoc myTable = "AlarmTable";
92         try {
93             conn = ds.getConnection();
94             stmt = conn.createStatement();
95             stmt.execute("drop table " + myTable);
96             stmt.close();
97         } catch (SQLException JavaDoc e) {
98             // The first time, table will not exist.
99
}
100
101         // Create table.
102
try {
103             stmt = conn.createStatement();
104             stmt
105                     .execute("create table "
106                             + myTable
107                             + "(dbpk varchar(12) not null primary key, dbsev integer, dbfrom varchar(12), dbreason varchar(30), dbcount integer, dbstate integer, dbdate date)");
108             stmt.close();
109             conn.close();
110         } catch (SQLException JavaDoc e) {
111             throw new EJBException JavaDoc("Exception in createTable");
112         }
113     }
114
115     // ------------------------------------------------------------------
116
// EntityBean implementation
117
// ------------------------------------------------------------------
118

119     /**
120      * Set the associated entity context. The container invokes this method on
121      * an instance after the instance has been created. This method is called in
122      * an unspecified transaction context.
123      * @param ctx - An EntityContext interface for the instance. The instance
124      * should store the reference to the context in an instance variable.
125      * @throws EJBException Thrown by the method to indicate a failure caused by
126      * a system-level error.
127      */

128     public void setEntityContext(EntityContext JavaDoc ctx) {
129         ejbContext = ctx;
130     }
131
132     /**
133      * Unset the associated entity context. The container calls this method
134      * before removing the instance. This is the last method that the container
135      * invokes on the instance. The Java garbage collector will eventually
136      * invoke the finalize() method on the instance. This method is called in an
137      * unspecified transaction context.
138      * @throws EJBException Thrown by the method to indicate a failure caused by
139      * a system-level error.
140      */

141     public void unsetEntityContext() {
142         ejbContext = null;
143     }
144
145     /**
146      * A container invokes this method before it removes the EJB object that is
147      * currently associated with the instance. This method is invoked when a
148      * client invokes a remove operation on the enterprise Bean's home interface
149      * or the EJB object's remote interface. This method transitions the
150      * instance from the ready state to the pool of available instances. This
151      * method is called in the transaction context of the remove operation.
152      * @throws RemoveException The enterprise Bean does not allow destruction of
153      * the object.
154      * @throws EJBException - Thrown by the method to indicate a failure caused
155      * by a system-level error.
156      */

157     public void ejbRemove() throws RemoveException JavaDoc {
158     }
159
160     /**
161      * A container invokes this method to instruct the instance to synchronize
162      * its state by loading it state from the underlying database. This method
163      * always executes in the proper transaction context.
164      * @throws EJBException Thrown by the method to indicate a failure caused by
165      * a system-level error.
166      */

167     public void ejbLoad() {
168     }
169
170     /**
171      * A container invokes this method to instruct the instance to synchronize
172      * its state by storing it to the underlying database. This method always
173      * executes in the proper transaction context.
174      * @throws EJBException Thrown by the method to indicate a failure caused by
175      * a system-level error.
176      */

177     public void ejbStore() {
178     }
179
180     /**
181      * There must be an ejbPostCreate par ejbCreate method
182      * @throws CreateException Failure to create an entity EJB object.
183      */

184     public void ejbPostCreate(AlarmData ad) throws CreateException JavaDoc {
185     }
186
187     public void ejbPostCreate() throws CreateException JavaDoc {
188     }
189
190     /**
191      * The Entity bean can define 0 or more ejbCreate methods.
192      * @throws CreateException Failure to create an entity EJB object.
193      * @throws DuplicateKeyException An object with the same key already exists.
194      */

195     public String JavaDoc ejbCreate(AlarmData ad) throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
196
197         // Init here the bean fields
198
Integer JavaDoc i = new Integer JavaDoc(ad.getNum());
199         pk = i.toString();
200         sev = ad.getSev();
201         from = ad.getDevice();
202         reason = ad.getMessage();
203         count = 1;
204         state = 1;
205         date = (java.sql.Date JavaDoc) ad.getDate();
206
207         // In CMP, should return null.
208
return null;
209     }
210
211     public String JavaDoc ejbCreate() throws CreateException JavaDoc, DuplicateKeyException JavaDoc {
212
213         // init database
214
initDB();
215
216         // init bean fields
217
pk = "0";
218         sev = 99; // avoids taking this as a true AlarmRecord.
219
from = "init";
220         reason = "init";
221         count = 0;
222         state = 0;
223         date = null;
224
225         // In CMP, should return null.
226
return null;
227     }
228
229     /**
230      * A container invokes this method on an instance before the instance
231      * becomes disassociated with a specific EJB object.
232      */

233     public void ejbPassivate() {
234     }
235
236     /**
237      * A container invokes this method when the instance is taken out of the
238      * pool of available instances to become associated with a specific EJB
239      * object.
240      */

241     public void ejbActivate() {
242     }
243
244     // ------------------------------------------------------------------
245
// AlarmRecord implementation
246
// ------------------------------------------------------------------
247

248     /**
249      *
250      */

251     public void update(int s) {
252         count++;
253         if (s < sev) {
254             sev = s;
255         }
256         java.util.Date JavaDoc now = new java.util.Date JavaDoc();
257         date = new java.sql.Date JavaDoc(now.getTime());
258     }
259
260     /**
261      * forget
262      */

263     public void forget() {
264         state = 3;
265     }
266
267     /**
268      * setProcessed
269      */

270     public void setProcessed() {
271         state = 2;
272     }
273
274     public String JavaDoc getFrom() {
275         return from;
276     }
277
278     public int getSeverity() {
279         return sev;
280     }
281
282     public int getCount() {
283         return count;
284     }
285
286     /**
287      * Get the number of alarms in database only valid for special "0" element.
288      */

289     public int getAlarmCount() throws RemoteException JavaDoc {
290         if (!pk.equals("0")) {
291             throw new RemoteException JavaDoc("pk should be 0");
292         }
293         return count;
294     }
295
296     /**
297      * Get a new Ident only valid for special "0" element.
298      */

299     public int getNewIdent() throws RemoteException JavaDoc {
300         if (!pk.equals("0")) {
301             throw new RemoteException JavaDoc("pk should be 0");
302         }
303         return ++count;
304     }
305
306     /**
307      * Returns the AlarmData for that instance
308      */

309     public AlarmData getAlarmData() throws RemoteException JavaDoc {
310         Integer JavaDoc id = new Integer JavaDoc(pk);
311         AlarmData ret = new AlarmData(id.intValue(), sev, from, reason, date);
312         ret.setCount(count);
313         ret.setState(state);
314         return ret;
315     }
316 }
Popular Tags