View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging.jmx;
2   
3   import cz.cuni.amis.pogamut.base.utils.logging.LogCategory;
4   import java.io.IOException;
5   import java.util.logging.LogRecord;
6   import javax.management.InstanceNotFoundException;
7   import javax.management.MBeanServerConnection;
8   import javax.management.Notification;
9   import javax.management.NotificationListener;
10  import javax.management.ObjectName;
11  
12  /**
13   * Proxies a single log category, intended to be used only for registering handlers.
14   * TODO what to do when user logs a message on this instance? throw exception?
15   * @author ik
16   */
17  public class LogCategoryJMXProxy extends LogCategory {
18  
19      ObjectName categoryObjectName = null;
20  	private MBeanServerConnection mbsc;
21  
22      public LogCategoryJMXProxy(MBeanServerConnection mbsc, ObjectName parent, String categoryName) throws InstanceNotFoundException, IOException {
23          super(categoryName);
24          categoryObjectName = JMXLogCategories.getJMXLogCategoryName(parent, categoryName);
25          this.mbsc = mbsc;
26      }
27      
28      public void enableLogReading() throws InstanceNotFoundException, IOException {
29      	// add notification listener
30          mbsc.addNotificationListener(categoryObjectName, new NotificationListener() {
31  
32              @Override
33              public void handleNotification(Notification notification, Object handback) {
34                  // user data is by convention in JMXLogRecordContainer 
35                  // because of trouble with serialializing parameters of a LogRecord
36                  JMXLogRecordContainer container = (JMXLogRecordContainer)notification.getUserData();
37                  //container.printInfo();
38  
39                  log(container.getRecordWithParameters());
40              }
41          }, null, null);
42      }
43      
44  }