KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejtools > management > browser > model > service > NotificationServiceProvider


1 /*
2  * EJTools, the Enterprise Java Tools
3  *
4  * Distributable under LGPL license.
5  * See terms of license at www.gnu.org.
6  */

7 package org.ejtools.management.browser.model.service;
8
9 import java.awt.Component JavaDoc;
10 import java.beans.beancontext.BeanContextChildComponentProxy JavaDoc;
11 import java.beans.beancontext.BeanContextServices JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.ResourceBundle JavaDoc;
15 import java.util.Vector JavaDoc;
16
17 import javax.management.Notification JavaDoc;
18 import javax.management.NotificationListener JavaDoc;
19 import javax.management.ObjectName JavaDoc;
20 import javax.management.j2ee.ListenerRegistration JavaDoc;
21 import javax.swing.JTable JavaDoc;
22 import javax.swing.table.AbstractTableModel JavaDoc;
23
24 import org.apache.log4j.Logger;
25 import org.ejtools.beans.beancontext.CustomBeanContextServiceProvider;
26 import org.ejtools.swing.table.TableModelSorter;
27 import org.ejtools.util.LimitedStack;
28
29 /**
30  * Description of the Class
31  *
32  * @author Laurent Etiemble
33  * @version $Revision: 1.6 $
34  * @todo Javadoc to complete
35  * @todo Service release to complete
36  */

37 public class NotificationServiceProvider extends CustomBeanContextServiceProvider implements BeanContextChildComponentProxy JavaDoc, NotificationService, NotificationListener JavaDoc
38 {
39    /** Description of the Field */
40    protected NotificationTableModel model = null;
41    /** Description of the Field */
42    protected LimitedStack notifications = new LimitedStack(500);
43    /** Description of the Field */
44    protected Vector JavaDoc objectNames = new Vector JavaDoc();
45    /** Description of the Field */
46    protected NotificationService service = null;
47    /** Description of the Field */
48    protected JTable JavaDoc table = null;
49    /** Description of the Field */
50    private static Logger logger = Logger.getLogger(NotificationServiceProvider.class);
51    /** Description of the Field */
52    private static ResourceBundle JavaDoc resources = ResourceBundle.getBundle("org.ejtools.management.browser.Resources");
53
54
55    /** Constructor for the ConnectionServiceProvider object */
56    public NotificationServiceProvider()
57    {
58       this.service = this;
59    }
60
61
62    /**
63     * Adds a feature to the ObjectName attribute of the NotificationService object
64     *
65     * @param object The feature to be added to the ObjectName attribute
66     */

67    public void addObjectName(ObjectName JavaDoc object)
68    {
69       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
70
71       if (this.hasObjectName(object))
72       {
73          return;
74       }
75
76       if (context.hasService(ConnectionService.class))
77       {
78          logger.debug("Using service ConnectionService...");
79          try
80          {
81             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
82             ListenerRegistration JavaDoc registration = service.getMEJB().getListenerRegistry();
83             logger.debug("ListenerRegistration " + registration);
84             registration.addNotificationListener(object, this, null, "EJTOOLS Management BROWSER");
85             this.objectNames.add(object.getCanonicalName());
86             context.releaseService(this, this, ConnectionService.class);
87          }
88          catch (Exception JavaDoc e)
89          {
90             logger.error("Error during utilisation of service ConnectionService", e);
91          }
92       }
93    }
94
95
96    /** Description of the Method */
97    public void clearAll()
98    {
99       this.notifications.clear();
100    }
101
102
103    /**
104     * @return The component value
105     */

106    public Component JavaDoc getComponent()
107    {
108       if (this.table == null)
109       {
110          this.model = new NotificationServiceProvider.NotificationTableModel();
111          TableModelSorter sorter = new TableModelSorter(this.model);
112          this.table = new JTable JavaDoc(sorter);
113          sorter.addMouseListenerToHeaderInTable(this.table);
114       }
115       return this.table;
116    }
117
118
119    /**
120     * Getter for the currentServiceSelectors attribute
121     *
122     * @param bcs Description of the Parameter
123     * @param serviceClass Description of the Parameter
124     * @return The currentServiceSelectors value
125     */

126    public Iterator JavaDoc getCurrentServiceSelectors(BeanContextServices JavaDoc bcs, java.lang.Class JavaDoc serviceClass)
127    {
128       return (new Vector JavaDoc()).iterator();
129    }
130
131
132    /**
133     * Getter for the service attribute
134     *
135     * @param bcs Description of the Parameter
136     * @param requestor Description of the Parameter
137     * @param serviceClass Description of the Parameter
138     * @param serviceSelector Description of the Parameter
139     * @return The service value
140     */

141    public Object JavaDoc getService(BeanContextServices JavaDoc bcs, java.lang.Object JavaDoc requestor, java.lang.Class JavaDoc serviceClass, java.lang.Object JavaDoc serviceSelector)
142    {
143       return this.service;
144    }
145
146
147    /**
148     * Description of the Method
149     *
150     * @param notification Description of the Parameter
151     * @param handback Description of the Parameter
152     */

153    public void handleNotification(Notification JavaDoc notification, java.lang.Object JavaDoc handback)
154    {
155       logger.debug("Notification received (" + handback + ")");
156       this.notifications.push(notification);
157       if (this.model != null)
158       {
159          this.model.fireTableRowsInserted(this.notifications.size(), this.notifications.size());
160       }
161    }
162
163
164    /**
165     * Description of the Method
166     *
167     * @param object Description of the Parameter
168     * @return Description of the Return Value
169     */

170    public boolean hasObjectName(ObjectName JavaDoc object)
171    {
172       return this.objectNames.contains(object.getCanonicalName());
173    }
174
175
176    /**
177     * Description of the Method
178     *
179     * @return Description of the Return Value
180     */

181    public Collection JavaDoc listAll()
182    {
183       return this.reverse(notifications);
184    }
185
186
187    /**
188     * Description of the Method
189     *
190     * @param bcs Description of the Parameter
191     * @param requestor Description of the Parameter
192     * @param service Description of the Parameter
193     */

194    public void releaseService(BeanContextServices JavaDoc bcs, java.lang.Object JavaDoc requestor, java.lang.Object JavaDoc service) { }
195
196
197    /**
198     * Description of the Method
199     *
200     * @param object Description of the Parameter
201     */

202    public void removeObjectName(ObjectName JavaDoc object)
203    {
204       BeanContextServices JavaDoc context = (BeanContextServices JavaDoc) getBeanContext();
205
206       if (!this.hasObjectName(object))
207       {
208          return;
209       }
210
211       if (context.hasService(ConnectionService.class))
212       {
213          logger.debug("Using service ConnectionService...");
214          try
215          {
216             ConnectionService service = (ConnectionService) context.getService(this, this, ConnectionService.class, this, this);
217             ListenerRegistration JavaDoc registration = service.getMEJB().getListenerRegistry();
218             logger.debug("ListenerRegistration " + registration);
219             registration.removeNotificationListener(object, this);
220             this.objectNames.remove(object.getCanonicalName());
221             context.releaseService(this, this, ConnectionService.class);
222          }
223          catch (Exception JavaDoc e)
224          {
225             logger.error("Error during utilisation of service ConnectionService", e);
226          }
227       }
228    }
229
230
231    /**
232     * @return The serviceClass value
233     */

234    protected Class JavaDoc[] getServiceClass()
235    {
236       return new Class JavaDoc[]{NotificationService.class};
237    }
238
239
240    /**
241     * Description of the Method
242     *
243     * @param collection Description of the Parameter
244     * @return Description of the Return Value
245     * @todo Move elsewhere
246     */

247    protected Collection JavaDoc reverse(Collection JavaDoc collection)
248    {
249       Vector JavaDoc result = new Vector JavaDoc();
250
251       Iterator JavaDoc it = collection.iterator();
252       while (it.hasNext())
253       {
254          result.insertElementAt(it.next(), 0);
255       }
256
257       return result;
258    }
259
260
261    /**
262     * Description of the Class
263     *
264     * @author letiembl
265     * @version $Revision: 1.6 $
266     * @created 18 septembre 2002
267     * @todo I18N
268     */

269    private class NotificationTableModel extends AbstractTableModel JavaDoc
270    {
271       /** Description of the Field */
272       private String JavaDoc COLUMN_MESSAGE = resources.getString("notification.table.column.message");
273       /** Description of the Field */
274       private String JavaDoc COLUMN_SEQUENCE = resources.getString("notification.table.column.sequence");
275       /** Description of the Field */
276       private String JavaDoc COLUMN_SOURCE = resources.getString("notification.table.column.source");
277       /** Description of the Field */
278       private String JavaDoc COLUMN_TIMESTAMP = resources.getString("notification.table.column.timestamp");
279       /** Description of the Field */
280       private String JavaDoc COLUMN_TYPE = resources.getString("notification.table.column.type");
281
282
283       /**
284        * @param columnIndex Description of the Parameter
285        * @return The columnClass value
286        */

287       public Class JavaDoc getColumnClass(int columnIndex)
288       {
289          switch (columnIndex)
290          {
291             case 0:
292                return Object JavaDoc.class;
293             case 1:
294                return String JavaDoc.class;
295             case 2:
296                return String JavaDoc.class;
297             case 3:
298                return String JavaDoc.class;
299             case 4:
300                return String JavaDoc.class;
301          }
302          return Object JavaDoc.class;
303       }
304
305
306       /**
307        * @return The columnCount value
308        */

309       public int getColumnCount()
310       {
311          return 5;
312       }
313
314
315       /**
316        * @param columnIndex Description of the Parameter
317        * @return The columnName value
318        */

319       public String JavaDoc getColumnName(int columnIndex)
320       {
321          switch (columnIndex)
322          {
323             case 0:
324                return COLUMN_SOURCE;
325             case 1:
326                return COLUMN_TIMESTAMP;
327             case 2:
328                return COLUMN_SEQUENCE;
329             case 3:
330                return COLUMN_TYPE;
331             case 4:
332                return COLUMN_MESSAGE;
333          }
334          return super.getColumnName(columnIndex);
335       }
336
337
338       /**
339        * @return The rowCount value
340        */

341       public int getRowCount()
342       {
343          return NotificationServiceProvider.this.notifications.size();
344       }
345
346
347       /**
348        * @param rowIndex Description of the Parameter
349        * @param columnIndex Description of the Parameter
350        * @return The valueAt value
351        */

352       public Object JavaDoc getValueAt(int rowIndex, int columnIndex)
353       {
354          Object JavaDoc o = null;
355          if (rowIndex < NotificationServiceProvider.this.notifications.size())
356          {
357             Notification JavaDoc notif = (Notification JavaDoc) NotificationServiceProvider.this.notifications.elementAt(rowIndex);
358             switch (columnIndex)
359             {
360                case 0:
361                   o = notif.getSource();
362                   break;
363                case 1:
364                   o = new String JavaDoc("" + notif.getTimeStamp());
365                   break;
366                case 2:
367                   o = new String JavaDoc("" + notif.getSequenceNumber());
368                   break;
369                case 3:
370                   o = notif.getType();
371                   break;
372                case 4:
373                   o = notif.getMessage();
374                   break;
375             }
376          }
377          if (o == null)
378          {
379             o = new String JavaDoc();
380          }
381          return o;
382       }
383    }
384 }
385
Popular Tags