001: /**
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999-2004 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: AlarmManager.java 4583 2004-04-09 12:56:41Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.alarm.beans;
026:
027: import java.rmi.RemoteException;
028: import java.util.Iterator;
029: import java.util.LinkedList;
030:
031: import javax.ejb.CreateException;
032: import javax.ejb.FinderException;
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import javax.rmi.PortableRemoteObject;
037:
038: /**
039: * AlarmManager implementation.
040: */
041: public class AlarmManager {
042:
043: /**
044: * Unique instance of this class
045: */
046: private static AlarmManager unique = null;
047:
048: /**
049: * Initial context
050: */
051: private Context ictx = null;
052:
053: /**
054: * Home the bean AlarmRecord
055: */
056: private AlarmRecordHome arh = null;
057:
058: /**
059: * Bean alarm Record
060: */
061: private AlarmRecord arcount = null;
062:
063: /**
064: * List of profiles
065: */
066: private LinkedList profilList = new LinkedList();
067:
068: /**
069: * Default constructor
070: */
071: private AlarmManager() {
072: Debug.log("AlarmManager - creating");
073:
074: // Get a ref on AlarmRecordHome
075: try {
076: ictx = new InitialContext();
077: arh = (AlarmRecordHome) PortableRemoteObject.narrow(ictx
078: .lookup("alarmrecord"), AlarmRecordHome.class);
079: } catch (NamingException e) {
080: Debug.logError("AlarmManager : Cannot get AlarmRecordHome:"
081: + e);
082: }
083:
084: // Get number of alarms still in database
085: // Create AlarmTable if does not exist
086: int trycount = 0;
087: while (arcount == null) {
088: try {
089: arcount = arh.findByPrimaryKey("0");
090: int alarmid = arcount.getAlarmCount();
091: Debug
092: .log("AlarmManager: " + alarmid
093: + " alarm records");
094: } catch (Exception e) {
095: if (trycount > 0) {
096: Debug.logError("AlarmManager - bad start");
097: return;
098: }
099: Debug.log("AlarmTable does not exist: create it");
100: try {
101: arh.create();
102: } catch (Exception f) {
103: Debug
104: .logError("AlarmManager: Cannot init database:"
105: + e);
106: }
107: trycount++;
108: }
109: }
110:
111: // Create a set of default profils
112: newProfil("all", "A");
113:
114: Debug.log("AlarmManager - created");
115: }
116:
117: /**
118: * @return unique instance of this class
119: */
120: public static AlarmManager getInstance() {
121: if (unique == null) {
122: unique = new AlarmManager();
123: }
124: return unique;
125: }
126:
127: /**
128: * * a new Alarm is arrived must be synchronized to make sure we do not
129: * create several AlarmRecord for the same Alarm (in case of many identical
130: * Alarm arriving at the same time)
131: * @param severity the level of severity
132: * @param from the device name
133: * @param reason the reason of the alarm
134: */
135: public synchronized void alarm(int severity, String from,
136: String reason) {
137: Debug
138: .log("AlarmManager new Alarm from " + from + ": "
139: + reason);
140:
141: // Search if alarm already known
142: // Key information is made of "from"+"reason"
143: AlarmRecord arec = null;
144: try {
145: arec = arh.findAlarm(from, reason);
146: } catch (FinderException e) {
147: Debug.logError("AlarmManager: " + e);
148: } catch (RemoteException e) {
149: Debug.logError("AlarmManager: " + e);
150: }
151:
152: if (arec == null) {
153: // If New Alarm: Create the AlarmRecord
154: Debug.log("new AlarmRecord");
155: AlarmData ad = null;
156: try {
157: // Allocate a unique ident
158: int alarmid = arcount.getNewIdent();
159: java.util.Date now = new java.util.Date();
160: ad = new AlarmData(alarmid, severity, from, reason,
161: new java.sql.Date(now.getTime()));
162: arec = arh.create(ad);
163: } catch (CreateException e) {
164: Debug.logError("AlarmManager: " + e);
165: } catch (RemoteException e) {
166: Debug.logError("AlarmManager: " + e);
167: }
168: // Notice profiles interested
169: Iterator i = profilList.iterator();
170: while (i.hasNext()) {
171: Profil prof = (Profil) i.next();
172: if (prof.interestedBy(ad)) {
173: prof.noticeAlarm(arec);
174: }
175: }
176: } else {
177: // Old Alarm -> just increment count.
178: Debug.log("AlarmRecord already known");
179: try {
180: arec.update(severity);
181: } catch (RemoteException e) {
182: Debug.logError("AlarmManager: " + e);
183: }
184: }
185:
186: }
187:
188: /**
189: * Mark an AlarmRecord as processed. We don't remove it now to keep it in
190: * history.
191: * @param pk the primary key
192: * @throws RemoteException as it is remote it can fails
193: */
194: public void forgetAlarm(String pk) throws RemoteException {
195: Debug.log("entering for " + pk);
196:
197: // Find this Alarm by its PK
198: AlarmRecord arec = null;
199: try {
200: arec = arh.findByPrimaryKey(pk);
201: } catch (FinderException e) {
202: Debug.logError("AlarmManager Alarm not found");
203: throw new RemoteException("Alarm not found");
204: } catch (RemoteException e) {
205: Debug.logError("AlarmManager: " + e);
206: throw e;
207: }
208:
209: // Change Alarm state
210: try {
211: arec.setProcessed();
212: } catch (RemoteException e) {
213: Debug.logError("AlarmManager: " + e);
214: throw e;
215: }
216: }
217:
218: /**
219: * Makes a new Profil
220: * @param from the device name
221: * @param maxsev the maximum level of severity
222: * @return the new profile created
223: */
224: public Profil newProfil(String from, String maxsev) {
225: Debug.log("entering for " + from + "/" + maxsev);
226:
227: // Check if already exist
228: Iterator i = profilList.iterator();
229: while (i.hasNext()) {
230: Profil prof = (Profil) i.next();
231: if (prof.getDevice().equals(from)
232: && prof.getSeverity().equals(maxsev)) {
233: return null;
234: }
235: }
236:
237: Profil p = new Profil(from, maxsev, arh);
238: profilList.add(p);
239: return p;
240: }
241:
242: /**
243: * @return the list of available profils
244: */
245: public String[] getProfilNames() {
246: Debug.log("entering");
247: LinkedList nlist = new LinkedList();
248: Iterator i = profilList.iterator();
249: while (i.hasNext()) {
250: Profil prof = (Profil) i.next();
251: nlist.add(prof.getName());
252: }
253: return (String[]) nlist.toArray(new String[0]);
254: }
255:
256: /**
257: * @param name of the profile to remove
258: * @return a reference on Profil object, given its name.
259: */
260: public Profil getProfil(String name) {
261: Debug.log("entering for " + name);
262: Profil ret = null;
263: Iterator i = profilList.iterator();
264: while (i.hasNext()) {
265: Profil prof = (Profil) i.next();
266: if (prof.getName().equals(name)) {
267: ret = prof;
268: }
269: }
270: return ret;
271: }
272:
273: /**
274: * remove a Profil
275: * @param name the name of the profile to remove
276: * @return true if the profile was removed
277: */
278: public boolean delProfil(String name) {
279: Debug.log("entering for " + name);
280: Iterator i = profilList.iterator();
281: while (i.hasNext()) {
282: Profil prof = (Profil) i.next();
283: if (prof.getName().equals(name)) {
284: i.remove();
285: return true;
286: }
287: }
288: return false;
289: }
290: }
|