001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2005 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * Initial developer: JOnAS Team
022: * --------------------------------------------------------------------------
023: * $Id: AlarmRecordBean.java 7792 2005-12-13 21:20:59Z moghrabi $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.alarm.beans;
026:
027: import java.rmi.RemoteException;
028: import java.sql.Connection;
029: import java.sql.Date;
030: import java.sql.SQLException;
031: import java.sql.Statement;
032:
033: import javax.ejb.CreateException;
034: import javax.ejb.DuplicateKeyException;
035: import javax.ejb.EJBException;
036: import javax.ejb.EntityBean;
037: import javax.ejb.EntityContext;
038: import javax.ejb.RemoveException;
039: import javax.naming.InitialContext;
040: import javax.sql.DataSource;
041:
042: /**
043: *
044: */
045: public class AlarmRecordBean implements EntityBean {
046:
047: EntityContext ejbContext;
048:
049: // ------------------------------------------------------------------
050: // State of the bean.
051: // They must be public for Container Managed Persistance.
052: // ------------------------------------------------------------------
053: public String pk;
054:
055: public int sev;
056:
057: public String from;
058:
059: public String reason;
060:
061: public int count;
062:
063: public int state;
064:
065: public Date date;
066:
067: // ------------------------------------------------------------------
068: // init DataBase
069: // ------------------------------------------------------------------
070: private void initDB() {
071:
072: // Get my DataSource from JNDI
073: DataSource ds = null;
074: InitialContext ictx = null;
075: try {
076: ictx = new InitialContext();
077: } catch (Exception e) {
078: throw new EJBException("Cannot get JNDI InitialContext");
079: }
080: try {
081: ds = (DataSource) ictx.lookup("java:comp/env/jdbc/myDS");
082: } catch (Exception e) {
083: throw new EJBException("cannot lookup datasource");
084: }
085:
086: // Drop table
087: Connection conn = null;
088: Statement stmt = null;
089: String myTable = "AlarmTable";
090: try {
091: conn = ds.getConnection();
092: stmt = conn.createStatement();
093: stmt.execute("drop table " + myTable);
094: stmt.close();
095: } catch (SQLException e) {
096: // The first time, table will not exist.
097: }
098:
099: // Create table.
100: try {
101: stmt = conn.createStatement();
102: stmt
103: .execute("create table "
104: + myTable
105: + "(dbpk varchar(12) not null primary key, dbsev integer, dbfrom varchar(12), dbreason varchar(30), dbcount integer, dbstate integer, dbdate date)");
106: stmt.close();
107: conn.close();
108: } catch (SQLException e) {
109: throw new EJBException("Exception in createTable");
110: }
111: }
112:
113: // ------------------------------------------------------------------
114: // EntityBean implementation
115: // ------------------------------------------------------------------
116:
117: /**
118: * Set the associated entity context. The container invokes this method on
119: * an instance after the instance has been created. This method is called in
120: * an unspecified transaction context.
121: * @param ctx - An EntityContext interface for the instance. The instance
122: * should store the reference to the context in an instance variable.
123: * @throws EJBException Thrown by the method to indicate a failure caused by
124: * a system-level error.
125: */
126: public void setEntityContext(EntityContext ctx) {
127: ejbContext = ctx;
128: }
129:
130: /**
131: * Unset the associated entity context. The container calls this method
132: * before removing the instance. This is the last method that the container
133: * invokes on the instance. The Java garbage collector will eventually
134: * invoke the finalize() method on the instance. This method is called in an
135: * unspecified transaction context.
136: * @throws EJBException Thrown by the method to indicate a failure caused by
137: * a system-level error.
138: */
139: public void unsetEntityContext() {
140: ejbContext = null;
141: }
142:
143: /**
144: * A container invokes this method before it removes the EJB object that is
145: * currently associated with the instance. This method is invoked when a
146: * client invokes a remove operation on the enterprise Bean's home interface
147: * or the EJB object's remote interface. This method transitions the
148: * instance from the ready state to the pool of available instances. This
149: * method is called in the transaction context of the remove operation.
150: * @throws RemoveException The enterprise Bean does not allow destruction of
151: * the object.
152: * @throws EJBException - Thrown by the method to indicate a failure caused
153: * by a system-level error.
154: */
155: public void ejbRemove() throws RemoveException {
156: }
157:
158: /**
159: * A container invokes this method to instruct the instance to synchronize
160: * its state by loading it state from the underlying database. This method
161: * always executes in the proper transaction context.
162: * @throws EJBException Thrown by the method to indicate a failure caused by
163: * a system-level error.
164: */
165: public void ejbLoad() {
166: }
167:
168: /**
169: * A container invokes this method to instruct the instance to synchronize
170: * its state by storing it to the underlying database. This method always
171: * executes in the proper transaction context.
172: * @throws EJBException Thrown by the method to indicate a failure caused by
173: * a system-level error.
174: */
175: public void ejbStore() {
176: }
177:
178: /**
179: * There must be an ejbPostCreate par ejbCreate method
180: * @throws CreateException Failure to create an entity EJB object.
181: */
182: public void ejbPostCreate(AlarmData ad) throws CreateException {
183: }
184:
185: public void ejbPostCreate() throws CreateException {
186: }
187:
188: /**
189: * The Entity bean can define 0 or more ejbCreate methods.
190: * @throws CreateException Failure to create an entity EJB object.
191: * @throws DuplicateKeyException An object with the same key already exists.
192: */
193: public String ejbCreate(AlarmData ad) throws CreateException,
194: DuplicateKeyException {
195:
196: // Init here the bean fields
197: Integer i = new Integer(ad.getNum());
198: pk = i.toString();
199: sev = ad.getSev();
200: from = ad.getDevice();
201: reason = ad.getMessage();
202: count = 1;
203: state = 1;
204: date = (java.sql.Date) ad.getDate();
205:
206: // In CMP, should return null.
207: return null;
208: }
209:
210: public String ejbCreate() throws CreateException,
211: DuplicateKeyException {
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 now = new java.util.Date();
257: date = new java.sql.Date(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 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 {
290: if (!pk.equals("0")) {
291: throw new RemoteException("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 {
300: if (!pk.equals("0")) {
301: throw new RemoteException("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 {
310: Integer id = new Integer(pk);
311: AlarmData ret = new AlarmData(id.intValue(), sev, from, reason,
312: date);
313: ret.setCount(count);
314: ret.setState(state);
315: return ret;
316: }
317: }
|