1 26 27 package org.objectweb.alarm.beans; 28 29 import java.rmi.RemoteException ; 30 import java.sql.Connection ; 31 import java.sql.Date ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 35 import javax.ejb.CreateException ; 36 import javax.ejb.DuplicateKeyException ; 37 import javax.ejb.EJBException ; 38 import javax.ejb.EntityBean ; 39 import javax.ejb.EntityContext ; 40 import javax.ejb.RemoveException ; 41 import javax.naming.InitialContext ; 42 import javax.sql.DataSource ; 43 44 47 public class AlarmRecordBean implements EntityBean { 48 49 EntityContext ejbContext; 50 51 public String pk; 56 57 public int sev; 58 59 public String from; 60 61 public String reason; 62 63 public int count; 64 65 public int state; 66 67 public Date date; 68 69 private void initDB() { 73 74 DataSource ds = null; 76 InitialContext ictx = null; 77 try { 78 ictx = new InitialContext (); 79 } catch (Exception e) { 80 throw new EJBException ("Cannot get JNDI InitialContext"); 81 } 82 try { 83 ds = (DataSource ) ictx.lookup("java:comp/env/jdbc/myDS"); 84 } catch (Exception e) { 85 throw new EJBException ("cannot lookup datasource"); 86 } 87 88 Connection conn = null; 90 Statement stmt = null; 91 String myTable = "AlarmTable"; 92 try { 93 conn = ds.getConnection(); 94 stmt = conn.createStatement(); 95 stmt.execute("drop table " + myTable); 96 stmt.close(); 97 } catch (SQLException e) { 98 } 100 101 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 e) { 111 throw new EJBException ("Exception in createTable"); 112 } 113 } 114 115 119 128 public void setEntityContext(EntityContext ctx) { 129 ejbContext = ctx; 130 } 131 132 141 public void unsetEntityContext() { 142 ejbContext = null; 143 } 144 145 157 public void ejbRemove() throws RemoveException { 158 } 159 160 167 public void ejbLoad() { 168 } 169 170 177 public void ejbStore() { 178 } 179 180 184 public void ejbPostCreate(AlarmData ad) throws CreateException { 185 } 186 187 public void ejbPostCreate() throws CreateException { 188 } 189 190 195 public String ejbCreate(AlarmData ad) throws CreateException , DuplicateKeyException { 196 197 Integer i = new Integer (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 ) ad.getDate(); 206 207 return null; 209 } 210 211 public String ejbCreate() throws CreateException , DuplicateKeyException { 212 213 initDB(); 215 216 pk = "0"; 218 sev = 99; from = "init"; 220 reason = "init"; 221 count = 0; 222 state = 0; 223 date = null; 224 225 return null; 227 } 228 229 233 public void ejbPassivate() { 234 } 235 236 241 public void ejbActivate() { 242 } 243 244 248 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 263 public void forget() { 264 state = 3; 265 } 266 267 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 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 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 309 public AlarmData getAlarmData() throws RemoteException { 310 Integer id = new Integer (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 |