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: Profil.java 6920 2005-06-10 09:43:41Z benoitf $
024: * --------------------------------------------------------------------------
025: */package org.objectweb.alarm.beans;
026:
027: import java.rmi.RemoteException;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import java.util.LinkedList;
031:
032: import javax.ejb.FinderException;
033: import javax.rmi.PortableRemoteObject;
034:
035: /**
036: * Helper class Profil Profils are managed by AlarmManager and used by View
037: * session beans We do not use an Entity because we need neither persistence nor
038: * remote accesses. We cannot use a Session bean because it's accessed from
039: * several clients, and from the AlarmManager.
040: */
041: public class Profil {
042:
043: /**
044: * Device name or "all"
045: */
046: private String device;
047:
048: /**
049: * max severity level
050: */
051: private int maxsev; //
052:
053: /**
054: * Name
055: */
056: private String name;
057:
058: /**
059: * Is the device is all ?
060: */
061: private boolean fromAll;
062:
063: /**
064: * Home of the bean AlarmRecord
065: */
066: private AlarmRecordHome arh;
067:
068: /**
069: * A list of AlarmRecord objects
070: */
071: private LinkedList alarmList = new LinkedList();
072:
073: /**
074: * constructor
075: * @param device the device name
076: * @param sev the severity level
077: * @param arh the home of the eban
078: */
079:
080: public Profil(String device, String sev, AlarmRecordHome arh) {
081:
082: // init object
083: this .device = device;
084: this .arh = arh;
085: fromAll = device.equals("all");
086: if (sev.startsWith("S")) {
087: maxsev = 1;
088: sev = "S";
089: } else if (sev.startsWith("W")) {
090: maxsev = 2;
091: sev = "W";
092: } else {
093: maxsev = 3;
094: sev = "I";
095: }
096: name = device + "-" + sev;
097:
098: // find alarms already arrived and matching this Profil
099: Collection knal = null;
100: try {
101: if (fromAll) {
102: knal = arh.findBySeverity(maxsev);
103: } else {
104: knal = arh.findByInterest(device, maxsev);
105: }
106: alarmList.addAll(knal);
107: } catch (FinderException e) {
108: System.out.println("Profil constructor: No Alarm found");
109: } catch (RemoteException e) {
110: System.out.println("Error getting AlarmRecords:" + e);
111: }
112: }
113:
114: /**
115: * @return the device
116: */
117: public String getDevice() {
118: return device;
119: }
120:
121: /**
122: * @return the severity watched
123: */
124: public String getSeverity() {
125: switch (maxsev) {
126: case 1:
127: return "S";
128: case 2:
129: return "W";
130: case 3:
131: return "I";
132: default:
133: return "A";
134: }
135: }
136:
137: /**
138: * @return the profil name
139: */
140: public String getName() {
141: return name;
142: }
143:
144: /**
145: * get Alarms for this Profil.
146: * @param all true if get all alarms, false if get only new alarms
147: * @return Alarms for this Profil.
148: */
149: public synchronized Collection getAlarms(boolean all) {
150: LinkedList ret = new LinkedList();
151: Iterator it = alarmList.iterator();
152: while (it.hasNext()) {
153: AlarmRecord arec = (AlarmRecord) PortableRemoteObject
154: .narrow(it.next(), AlarmRecord.class);
155: AlarmData ad = null;
156: try {
157: ad = arec.getAlarmData();
158: if (all || (ad.getState() == 1)) {
159: ret.add(ad);
160: }
161: } catch (RemoteException e) {
162: System.out.println("Error getting AlarmRecord:" + e);
163: }
164: }
165: return ret;
166: }
167:
168: /**
169: * Gets current Alarm level
170: * @return current Alarm level
171: */
172: public synchronized int getCurrentLevel() {
173: int ret = 1000;
174: Iterator it = alarmList.iterator();
175: while (it.hasNext()) {
176: AlarmRecord arec = (AlarmRecord) PortableRemoteObject
177: .narrow(it.next(), AlarmRecord.class);
178: try {
179: AlarmData ad = arec.getAlarmData();
180: if (ad.getState() == 1 && ad.getSev() <= ret) {
181: ret = ad.getSev();
182: }
183: } catch (RemoteException e) {
184: System.out.println("Error getting AlarmRecord:" + e);
185: }
186: }
187: return ret;
188: }
189:
190: /**
191: * @param ad alarm data object
192: * @return true if this Profil is interested by this AlarmRecord.
193: */
194: public boolean interestedBy(AlarmData ad) {
195: return ((fromAll || ad.getDevice().equals(device)) && ad
196: .getSev() <= maxsev);
197: }
198:
199: /**
200: * add an Alarm Record to this Profil.
201: * @param arec the record
202: */
203: public synchronized void noticeAlarm(AlarmRecord arec) {
204: alarmList.add(arec);
205: }
206: }
|