KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999-2004 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: AlarmManager.java,v 1.2 2004/04/09 12:56:41 benoitf Exp $
24  * --------------------------------------------------------------------------
25  */

26
27 package org.objectweb.alarm.beans;
28
29 import java.rmi.RemoteException JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32
33 import javax.ejb.CreateException JavaDoc;
34 import javax.ejb.FinderException JavaDoc;
35 import javax.naming.Context JavaDoc;
36 import javax.naming.InitialContext JavaDoc;
37 import javax.naming.NamingException JavaDoc;
38 import javax.rmi.PortableRemoteObject JavaDoc;
39
40 /**
41  * AlarmManager implementation.
42  */

43 public class AlarmManager {
44
45     /**
46      * Unique instance of this class
47      */

48     private static AlarmManager unique = null;
49
50     /**
51      * Initial context
52      */

53     private Context JavaDoc ictx = null;
54
55     /**
56      * Home the bean AlarmRecord
57      */

58     private AlarmRecordHome arh = null;
59
60     /**
61      * Bean alarm Record
62      */

63     private AlarmRecord arcount = null;
64
65     /**
66      * List of profiles
67      */

68     private LinkedList JavaDoc profilList = new LinkedList JavaDoc();
69
70     /**
71      * Default constructor
72      */

73     private AlarmManager() {
74         Debug.log("AlarmManager - creating");
75
76         // Get a ref on AlarmRecordHome
77
try {
78             ictx = new InitialContext JavaDoc();
79             arh = (AlarmRecordHome) PortableRemoteObject.narrow(ictx.lookup("alarmrecord"), AlarmRecordHome.class);
80         } catch (NamingException JavaDoc e) {
81             Debug.logError("AlarmManager : Cannot get AlarmRecordHome:" + e);
82         }
83
84         // Get number of alarms still in database
85
// Create AlarmTable if does not exist
86
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 JavaDoc 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 JavaDoc f) {
101                     Debug.logError("AlarmManager: Cannot init database:" + e);
102                 }
103                 trycount++;
104             }
105         }
106
107         // Create a set of default profils
108
newProfil("all", "A");
109
110         Debug.log("AlarmManager - created");
111     }
112
113     /**
114      * @return unique instance of this class
115      */

116     public static AlarmManager getInstance() {
117         if (unique == null) {
118             unique = new AlarmManager();
119         }
120         return unique;
121     }
122
123     /**
124      * * a new Alarm is arrived must be synchronized to make sure we do not
125      * create several AlarmRecord for the same Alarm (in case of many identical
126      * Alarm arriving at the same time)
127      * @param severity the level of severity
128      * @param from the device name
129      * @param reason the reason of the alarm
130      */

131     public synchronized void alarm(int severity, String JavaDoc from, String JavaDoc reason) {
132         Debug.log("AlarmManager new Alarm from " + from + ": " + reason);
133
134         // Search if alarm already known
135
// Key information is made of "from"+"reason"
136
AlarmRecord arec = null;
137         try {
138             arec = arh.findAlarm(from, reason);
139         } catch (FinderException JavaDoc e) {
140             Debug.logError("AlarmManager: " + e);
141         } catch (RemoteException JavaDoc e) {
142             Debug.logError("AlarmManager: " + e);
143         }
144
145         if (arec == null) {
146             // If New Alarm: Create the AlarmRecord
147
Debug.log("new AlarmRecord");
148             AlarmData ad = null;
149             try {
150                 // Allocate a unique ident
151
int alarmid = arcount.getNewIdent();
152                 java.util.Date JavaDoc now = new java.util.Date JavaDoc();
153                 ad = new AlarmData(alarmid, severity, from, reason, new java.sql.Date JavaDoc(now.getTime()));
154                 arec = arh.create(ad);
155             } catch (CreateException JavaDoc e) {
156                 Debug.logError("AlarmManager: " + e);
157             } catch (RemoteException JavaDoc e) {
158                 Debug.logError("AlarmManager: " + e);
159             }
160             // Notice profiles interested
161
Iterator JavaDoc 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             // Old Alarm -> just increment count.
170
Debug.log("AlarmRecord already known");
171             try {
172                 arec.update(severity);
173             } catch (RemoteException JavaDoc e) {
174                 Debug.logError("AlarmManager: " + e);
175             }
176         }
177
178     }
179
180     /**
181      * Mark an AlarmRecord as processed. We don't remove it now to keep it in
182      * history.
183      * @param pk the primary key
184      * @throws RemoteException as it is remote it can fails
185      */

186     public void forgetAlarm(String JavaDoc pk) throws RemoteException JavaDoc {
187         Debug.log("entering for " + pk);
188
189         // Find this Alarm by its PK
190
AlarmRecord arec = null;
191         try {
192             arec = arh.findByPrimaryKey(pk);
193         } catch (FinderException JavaDoc e) {
194             Debug.logError("AlarmManager Alarm not found");
195             throw new RemoteException JavaDoc("Alarm not found");
196         } catch (RemoteException JavaDoc e) {
197             Debug.logError("AlarmManager: " + e);
198             throw e;
199         }
200
201         // Change Alarm state
202
try {
203             arec.setProcessed();
204         } catch (RemoteException JavaDoc e) {
205             Debug.logError("AlarmManager: " + e);
206             throw e;
207         }
208     }
209
210     /**
211      * Makes a new Profil
212      * @param from the device name
213      * @param maxsev the maximum level of severity
214      * @return the new profile created
215      */

216     public Profil newProfil(String JavaDoc from, String JavaDoc maxsev) {
217         Debug.log("entering for " + from + "/" + maxsev);
218
219         // Check if already exist
220
Iterator JavaDoc 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     /**
234      * @return the list of available profils
235      */

236     public String JavaDoc[] getProfilNames() {
237         Debug.log("entering");
238         LinkedList JavaDoc nlist = new LinkedList JavaDoc();
239         Iterator JavaDoc i = profilList.iterator();
240         while (i.hasNext()) {
241             Profil prof = (Profil) i.next();
242             nlist.add(prof.getName());
243         }
244         return (String JavaDoc[]) nlist.toArray(new String JavaDoc[0]);
245     }
246
247     /**
248      * @param name of the profile to remove
249      * @return a reference on Profil object, given its name.
250      */

251     public Profil getProfil(String JavaDoc name) {
252         Debug.log("entering for " + name);
253         Profil ret = null;
254         Iterator JavaDoc 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     /**
265      * remove a Profil
266      * @param name the name of the profile to remove
267      * @return true if the profile was removed
268      */

269     public boolean delProfil(String JavaDoc name) {
270         Debug.log("entering for " + name);
271         Iterator JavaDoc 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