KickJava   Java API By Example, From Geeks To Geeks.

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


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: ViewBean.java,v 1.6 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.Collection JavaDoc;
31
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34
35 /**
36  * Session bean View. Does not implement SessionSynchronization. We assume that
37  * this bean and AlarmManager run in the same JVM. Each Session bean is
38  * dedicated to 1 client: Here, the ViewProxy class. Each Session may be
39  * connected to 1 Profil or not.
40  */

41 public class ViewBean implements SessionBean JavaDoc {
42
43     /**
44      * Reference to the alarm manager
45      */

46     private static transient AlarmManager alarmManager = null;
47
48     /**
49      * Profil reference
50      */

51     private Profil prof = null;
52
53     /**
54      * Set the context for this bean
55      * @param ctx the context
56      * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
57      */

58     public void setSessionContext(SessionContext JavaDoc ctx) {
59
60     }
61
62     /**
63      * Remove
64      * @see javax.ejb.SessionBean#ejbRemove()
65      */

66     public void ejbRemove() {
67     }
68
69     /**
70      * The Session bean must define 1 or more ejbCreate methods.
71      */

72     public void ejbCreate() {
73         // Get a reference on the AlarmManager.
74
alarmManager = AlarmManager.getInstance();
75     }
76
77     /**
78      * Passivate
79      * @see javax.ejb.SessionBean#ejbPassivate()
80      */

81     public void ejbPassivate() {
82     }
83
84     /**
85      * Activate
86      * @see javax.ejb.SessionBean#ejbActivate()
87      */

88     public void ejbActivate() {
89     }
90
91     /**
92      * @return all messages for the current profil
93      * @throws RemoteException if remote call failed
94      */

95     public AlarmData[] getAllAlarms() throws RemoteException JavaDoc {
96         if (prof == null) {
97             throw new RemoteException JavaDoc("No Profil defined for this session");
98         }
99         Collection JavaDoc alist = prof.getAlarms(true);
100         return (AlarmData[]) alist.toArray(new AlarmData[0]);
101     }
102
103     /**
104      * @return new messages for the current profil
105      * @throws RemoteException if remote call failed
106      */

107     public AlarmData[] getNewAlarms() throws RemoteException JavaDoc {
108         if (prof == null) {
109             throw new RemoteException JavaDoc("No Profil defined for this session");
110         }
111         Collection JavaDoc alist = prof.getAlarms(false);
112         return (AlarmData[]) alist.toArray(new AlarmData[0]);
113     }
114
115     /**
116      * @return an enumeration of all available profils
117      */

118     public String JavaDoc[] getProfils() {
119         return alarmManager.getProfilNames();
120     }
121
122     /**
123      * Chooses to work on a particular profil.
124      * @param name of the profil
125      * @throws RemoteException if remote call failed
126      */

127     public void setProfil(String JavaDoc name) throws RemoteException JavaDoc {
128         if (name == null) {
129             prof = null;
130             return;
131         }
132         prof = alarmManager.getProfil(name);
133         if (prof == null) {
134             throw new RemoteException JavaDoc("This Profil does not exist yet: " + name);
135         }
136     }
137
138     /**
139      * @return the Alarm Level for Profil specified
140      * @param name of the profil
141      * @throws RemoteException if remote call failed
142      */

143     public int alarmLevel(String JavaDoc name) throws RemoteException JavaDoc {
144         Profil prof = alarmManager.getProfil(name);
145         if (prof == null) {
146             throw new RemoteException JavaDoc("This Profil does not exist yet: " + name);
147         }
148         return prof.getCurrentLevel();
149     }
150
151     /**
152      * Forget this message because problem has been taken into account.
153      * @param pk primary key
154      * @throws RemoteException if remote call failed
155      */

156     public void forgetAlarm(String JavaDoc pk) throws RemoteException JavaDoc {
157         alarmManager.forgetAlarm(pk);
158     }
159
160     /**
161      * creates a new Profil
162      * @param device the name of the device
163      * @param level the severity level
164      * @return the newly created profil
165      * @throws RemoteException if remote call failed
166      */

167     public String JavaDoc newProfil(String JavaDoc device, String JavaDoc level) throws RemoteException JavaDoc {
168
169         // Check arg validity
170
if (device.length() == 0) {
171             throw new RemoteException JavaDoc("null device string");
172         }
173         if (!level.startsWith("S") && !level.startsWith("W") && !level.startsWith("I")) {
174             throw new RemoteException JavaDoc("severity must be one of S|W|I");
175         }
176
177         prof = alarmManager.newProfil(device, level);
178         if (prof == null) {
179             return null;
180         }
181         return prof.getName();
182     }
183
184     /**
185      * remove a Profil
186      * @param name the name of the profil
187      * @throws RemoteException if remote call failed
188      */

189     public void removeProfil(String JavaDoc name) throws RemoteException JavaDoc {
190         if (name == null) {
191             return;
192         }
193         boolean ok = alarmManager.delProfil(name);
194         if (!ok) {
195             throw new RemoteException JavaDoc("This Profil does not exist yet: " + name);
196         }
197     }
198 }
Popular Tags