1 7 package org.ejtools.management.browser.model.service; 8 9 import java.awt.Component ; 10 import java.beans.beancontext.BeanContextChildComponentProxy ; 11 import java.beans.beancontext.BeanContextServices ; 12 import java.util.Collection ; 13 import java.util.Iterator ; 14 import java.util.ResourceBundle ; 15 import java.util.Vector ; 16 17 import javax.management.Notification ; 18 import javax.management.NotificationListener ; 19 import javax.management.ObjectName ; 20 import javax.management.j2ee.ListenerRegistration ; 21 import javax.swing.JTable ; 22 import javax.swing.table.AbstractTableModel ; 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 37 public class NotificationServiceProvider extends CustomBeanContextServiceProvider implements BeanContextChildComponentProxy , NotificationService, NotificationListener 38 { 39 40 protected NotificationTableModel model = null; 41 42 protected LimitedStack notifications = new LimitedStack(500); 43 44 protected Vector objectNames = new Vector (); 45 46 protected NotificationService service = null; 47 48 protected JTable table = null; 49 50 private static Logger logger = Logger.getLogger(NotificationServiceProvider.class); 51 52 private static ResourceBundle resources = ResourceBundle.getBundle("org.ejtools.management.browser.Resources"); 53 54 55 56 public NotificationServiceProvider() 57 { 58 this.service = this; 59 } 60 61 62 67 public void addObjectName(ObjectName object) 68 { 69 BeanContextServices context = (BeanContextServices ) 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 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 e) 89 { 90 logger.error("Error during utilisation of service ConnectionService", e); 91 } 92 } 93 } 94 95 96 97 public void clearAll() 98 { 99 this.notifications.clear(); 100 } 101 102 103 106 public Component 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 (sorter); 113 sorter.addMouseListenerToHeaderInTable(this.table); 114 } 115 return this.table; 116 } 117 118 119 126 public Iterator getCurrentServiceSelectors(BeanContextServices bcs, java.lang.Class serviceClass) 127 { 128 return (new Vector ()).iterator(); 129 } 130 131 132 141 public Object getService(BeanContextServices bcs, java.lang.Object requestor, java.lang.Class serviceClass, java.lang.Object serviceSelector) 142 { 143 return this.service; 144 } 145 146 147 153 public void handleNotification(Notification notification, java.lang.Object 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 170 public boolean hasObjectName(ObjectName object) 171 { 172 return this.objectNames.contains(object.getCanonicalName()); 173 } 174 175 176 181 public Collection listAll() 182 { 183 return this.reverse(notifications); 184 } 185 186 187 194 public void releaseService(BeanContextServices bcs, java.lang.Object requestor, java.lang.Object service) { } 195 196 197 202 public void removeObjectName(ObjectName object) 203 { 204 BeanContextServices context = (BeanContextServices ) 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 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 e) 224 { 225 logger.error("Error during utilisation of service ConnectionService", e); 226 } 227 } 228 } 229 230 231 234 protected Class [] getServiceClass() 235 { 236 return new Class []{NotificationService.class}; 237 } 238 239 240 247 protected Collection reverse(Collection collection) 248 { 249 Vector result = new Vector (); 250 251 Iterator it = collection.iterator(); 252 while (it.hasNext()) 253 { 254 result.insertElementAt(it.next(), 0); 255 } 256 257 return result; 258 } 259 260 261 269 private class NotificationTableModel extends AbstractTableModel 270 { 271 272 private String COLUMN_MESSAGE = resources.getString("notification.table.column.message"); 273 274 private String COLUMN_SEQUENCE = resources.getString("notification.table.column.sequence"); 275 276 private String COLUMN_SOURCE = resources.getString("notification.table.column.source"); 277 278 private String COLUMN_TIMESTAMP = resources.getString("notification.table.column.timestamp"); 279 280 private String COLUMN_TYPE = resources.getString("notification.table.column.type"); 281 282 283 287 public Class getColumnClass(int columnIndex) 288 { 289 switch (columnIndex) 290 { 291 case 0: 292 return Object .class; 293 case 1: 294 return String .class; 295 case 2: 296 return String .class; 297 case 3: 298 return String .class; 299 case 4: 300 return String .class; 301 } 302 return Object .class; 303 } 304 305 306 309 public int getColumnCount() 310 { 311 return 5; 312 } 313 314 315 319 public String 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 341 public int getRowCount() 342 { 343 return NotificationServiceProvider.this.notifications.size(); 344 } 345 346 347 352 public Object getValueAt(int rowIndex, int columnIndex) 353 { 354 Object o = null; 355 if (rowIndex < NotificationServiceProvider.this.notifications.size()) 356 { 357 Notification notif = (Notification ) 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 ("" + notif.getTimeStamp()); 365 break; 366 case 2: 367 o = new String ("" + 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 (); 380 } 381 return o; 382 } 383 } 384 } 385 | Popular Tags |