1 26 27 package org.objectweb.alarm.beans; 28 29 import java.rmi.RemoteException ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 33 import javax.ejb.CreateException ; 34 import javax.ejb.FinderException ; 35 import javax.naming.Context ; 36 import javax.naming.InitialContext ; 37 import javax.naming.NamingException ; 38 import javax.rmi.PortableRemoteObject ; 39 40 43 public class AlarmManager { 44 45 48 private static AlarmManager unique = null; 49 50 53 private Context ictx = null; 54 55 58 private AlarmRecordHome arh = null; 59 60 63 private AlarmRecord arcount = null; 64 65 68 private LinkedList profilList = new LinkedList (); 69 70 73 private AlarmManager() { 74 Debug.log("AlarmManager - creating"); 75 76 try { 78 ictx = new InitialContext (); 79 arh = (AlarmRecordHome) PortableRemoteObject.narrow(ictx.lookup("alarmrecord"), AlarmRecordHome.class); 80 } catch (NamingException e) { 81 Debug.logError("AlarmManager : Cannot get AlarmRecordHome:" + e); 82 } 83 84 int trycount = 0; 87 while (arcount == null) { 88 try { 89 arcount = arh.findByPrimaryKey("0"); 90 int alarmid = arcount.getAlarmCount(); 91 Debug.log("AlarmManager: " + alarmid + " alarm records"); 92 } catch (Exception e) { 93 if (trycount > 0) { 94 Debug.logError("AlarmManager - bad start"); 95 return; 96 } 97 Debug.log("AlarmTable does not exist: create it"); 98 try { 99 arh.create(); 100 } catch (Exception f) { 101 Debug.logError("AlarmManager: Cannot init database:" + e); 102 } 103 trycount++; 104 } 105 } 106 107 newProfil("all", "A"); 109 110 Debug.log("AlarmManager - created"); 111 } 112 113 116 public static AlarmManager getInstance() { 117 if (unique == null) { 118 unique = new AlarmManager(); 119 } 120 return unique; 121 } 122 123 131 public synchronized void alarm(int severity, String from, String reason) { 132 Debug.log("AlarmManager new Alarm from " + from + ": " + reason); 133 134 AlarmRecord arec = null; 137 try { 138 arec = arh.findAlarm(from, reason); 139 } catch (FinderException e) { 140 Debug.logError("AlarmManager: " + e); 141 } catch (RemoteException e) { 142 Debug.logError("AlarmManager: " + e); 143 } 144 145 if (arec == null) { 146 Debug.log("new AlarmRecord"); 148 AlarmData ad = null; 149 try { 150 int alarmid = arcount.getNewIdent(); 152 java.util.Date now = new java.util.Date (); 153 ad = new AlarmData(alarmid, severity, from, reason, new java.sql.Date (now.getTime())); 154 arec = arh.create(ad); 155 } catch (CreateException e) { 156 Debug.logError("AlarmManager: " + e); 157 } catch (RemoteException e) { 158 Debug.logError("AlarmManager: " + e); 159 } 160 Iterator i = profilList.iterator(); 162 while (i.hasNext()) { 163 Profil prof = (Profil) i.next(); 164 if (prof.interestedBy(ad)) { 165 prof.noticeAlarm(arec); 166 } 167 } 168 } else { 169 Debug.log("AlarmRecord already known"); 171 try { 172 arec.update(severity); 173 } catch (RemoteException e) { 174 Debug.logError("AlarmManager: " + e); 175 } 176 } 177 178 } 179 180 186 public void forgetAlarm(String pk) throws RemoteException { 187 Debug.log("entering for " + pk); 188 189 AlarmRecord arec = null; 191 try { 192 arec = arh.findByPrimaryKey(pk); 193 } catch (FinderException e) { 194 Debug.logError("AlarmManager Alarm not found"); 195 throw new RemoteException ("Alarm not found"); 196 } catch (RemoteException e) { 197 Debug.logError("AlarmManager: " + e); 198 throw e; 199 } 200 201 try { 203 arec.setProcessed(); 204 } catch (RemoteException e) { 205 Debug.logError("AlarmManager: " + e); 206 throw e; 207 } 208 } 209 210 216 public Profil newProfil(String from, String maxsev) { 217 Debug.log("entering for " + from + "/" + maxsev); 218 219 Iterator i = profilList.iterator(); 221 while (i.hasNext()) { 222 Profil prof = (Profil) i.next(); 223 if (prof.getDevice().equals(from) && prof.getSeverity().equals(maxsev)) { 224 return null; 225 } 226 } 227 228 Profil p = new Profil(from, maxsev, arh); 229 profilList.add(p); 230 return p; 231 } 232 233 236 public String [] getProfilNames() { 237 Debug.log("entering"); 238 LinkedList nlist = new LinkedList (); 239 Iterator i = profilList.iterator(); 240 while (i.hasNext()) { 241 Profil prof = (Profil) i.next(); 242 nlist.add(prof.getName()); 243 } 244 return (String []) nlist.toArray(new String [0]); 245 } 246 247 251 public Profil getProfil(String name) { 252 Debug.log("entering for " + name); 253 Profil ret = null; 254 Iterator i = profilList.iterator(); 255 while (i.hasNext()) { 256 Profil prof = (Profil) i.next(); 257 if (prof.getName().equals(name)) { 258 ret = prof; 259 } 260 } 261 return ret; 262 } 263 264 269 public boolean delProfil(String name) { 270 Debug.log("entering for " + name); 271 Iterator i = profilList.iterator(); 272 while (i.hasNext()) { 273 Profil prof = (Profil) i.next(); 274 if (prof.getName().equals(name)) { 275 i.remove(); 276 return true; 277 } 278 } 279 return false; 280 } 281 }
| Popular Tags
|