KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > alarm > beans > Profil


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2005 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer: JOnAS Team
22  * --------------------------------------------------------------------------
23  * $Id: Profil.java,v 1.7 2005/06/10 09:43:41 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.alarm.beans;
28
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.Collection JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.LinkedList JavaDoc;
33
34 import javax.ejb.FinderException JavaDoc;
35 import javax.rmi.PortableRemoteObject JavaDoc;
36
37
38 /**
39  * Helper class Profil Profils are managed by AlarmManager and used by View
40  * session beans We do not use an Entity because we need neither persistence nor
41  * remote accesses. We cannot use a Session bean because it's accessed from
42  * several clients, and from the AlarmManager.
43  */

44 public class Profil {
45
46     /**
47      * Device name or "all"
48      */

49     private String JavaDoc device;
50
51     /**
52      * max severity level
53      */

54     private int maxsev; //
55

56     /**
57      * Name
58      */

59     private String JavaDoc name;
60
61     /**
62      * Is the device is all ?
63      */

64     private boolean fromAll;
65
66     /**
67      * Home of the bean AlarmRecord
68      */

69     private AlarmRecordHome arh;
70
71     /**
72      * A list of AlarmRecord objects
73      */

74     private LinkedList JavaDoc alarmList = new LinkedList JavaDoc();
75
76     /**
77      * constructor
78      * @param device the device name
79      * @param sev the severity level
80      * @param arh the home of the eban
81      */

82
83     public Profil(String JavaDoc device, String JavaDoc sev, AlarmRecordHome arh) {
84
85         // init object
86
this.device = device;
87         this.arh = arh;
88         fromAll = device.equals("all");
89         if (sev.startsWith("S")) {
90             maxsev = 1;
91             sev = "S";
92         } else if (sev.startsWith("W")) {
93             maxsev = 2;
94             sev = "W";
95         } else {
96             maxsev = 3;
97             sev = "I";
98         }
99         name = device + "-" + sev;
100
101         // find alarms already arrived and matching this Profil
102
Collection JavaDoc knal = null;
103         try {
104             if (fromAll) {
105                 knal = arh.findBySeverity(maxsev);
106             } else {
107                 knal = arh.findByInterest(device, maxsev);
108             }
109             alarmList.addAll(knal);
110         } catch (FinderException JavaDoc e) {
111             System.out.println("Profil constructor: No Alarm found");
112         } catch (RemoteException JavaDoc e) {
113             System.out.println("Error getting AlarmRecords:" + e);
114         }
115     }
116
117     /**
118      * @return the device
119      */

120     public String JavaDoc getDevice() {
121         return device;
122     }
123
124     /**
125      * @return the severity watched
126      */

127     public String JavaDoc getSeverity() {
128         switch (maxsev) {
129             case 1:
130                 return "S";
131             case 2:
132                 return "W";
133             case 3:
134                 return "I";
135             default:
136                 return "A";
137         }
138     }
139
140     /**
141      * @return the profil name
142      */

143     public String JavaDoc getName() {
144         return name;
145     }
146
147     /**
148      * get Alarms for this Profil.
149      * @param all true if get all alarms, false if get only new alarms
150      * @return Alarms for this Profil.
151      */

152      public synchronized Collection JavaDoc getAlarms(boolean all) {
153         LinkedList JavaDoc ret = new LinkedList JavaDoc();
154         Iterator JavaDoc it = alarmList.iterator();
155         while (it.hasNext()) {
156             AlarmRecord arec = (AlarmRecord) PortableRemoteObject.narrow(it.next(), AlarmRecord.class);
157             AlarmData ad = null;
158             try {
159                 ad = arec.getAlarmData();
160                 if (all || (ad.getState() == 1)) {
161                     ret.add(ad);
162                 }
163             } catch (RemoteException JavaDoc e) {
164                 System.out.println("Error getting AlarmRecord:" + e);
165             }
166         }
167         return ret;
168     }
169
170     /**
171      * Gets current Alarm level
172      * @return current Alarm level
173      */

174      public synchronized int getCurrentLevel() {
175         int ret = 1000;
176         Iterator JavaDoc it = alarmList.iterator();
177         while (it.hasNext()) {
178             AlarmRecord arec = (AlarmRecord) PortableRemoteObject.narrow(it.next(), AlarmRecord.class);
179             try {
180                 AlarmData ad = arec.getAlarmData();
181                 if (ad.getState() == 1 && ad.getSev() <= ret) {
182                     ret = ad.getSev();
183                 }
184             } catch (RemoteException JavaDoc e) {
185                 System.out.println("Error getting AlarmRecord:" + e);
186             }
187         }
188         return ret;
189     }
190
191     /**
192      * @param ad alarm data object
193      * @return true if this Profil is interested by this AlarmRecord.
194      */

195     public boolean interestedBy(AlarmData ad) {
196         return ((fromAll || ad.getDevice().equals(device)) && ad.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 }
Popular Tags