View Javadoc

1   package cz.cuni.amis.pogamut.base.utils.logging.jmx;
2   
3   import cz.cuni.amis.pogamut.base.agent.jmx.AgentJMXComponents;
4   import java.util.logging.Formatter;
5   import java.util.logging.LogRecord;
6   
7   import javax.management.ListenerNotFoundException;
8   import javax.management.MBeanNotificationInfo;
9   import javax.management.MalformedObjectNameException;
10  import javax.management.NotificationBroadcasterSupport;
11  import javax.management.NotificationEmitter;
12  import javax.management.NotificationFilter;
13  import javax.management.NotificationListener;
14  
15  import cz.cuni.amis.pogamut.base.utils.logging.LogPublisher;
16  import javax.management.ObjectName;
17  
18  
19  public class JMXLogPublisher extends LogPublisher implements JMXLogPublisherMBean, NotificationEmitter {	
20  	/**
21           * MBean's id.
22           */
23          ObjectName objectName = null;
24  
25  	/**
26  	 * Initialize publisher with the simplest formatter - just publishing the message.
27  	 */
28  	public JMXLogPublisher() {
29  		super(new Formatter(){
30  			@Override
31  			public String format(LogRecord record) {				
32  				return record.getMessage();
33  			}			
34  		});
35  	}
36  	
37  	/**
38  	 * Initialize the publisher with prespecified formatter.
39  	 * <p><p>
40  	 * WARNING: if formatter is null, nothing will be published via JMX!
41  	 * @param formatter
42  	 */
43  	public JMXLogPublisher(Formatter formatter) {
44  		super(formatter);
45  	}
46  	
47  	/**
48  	 * Support for the JMX notification broadcasting. Used to send notifications.
49  	 */
50  	protected NotificationBroadcasterSupport notification = 
51  		new NotificationBroadcasterSupport(
52  				new MBeanNotificationInfo(
53  					new String[]{ JMXLogRecordNotification.NOTIFICATION_TYPE },
54  					JMXLogRecordNotification.class.getName(), //Must be fully qualified name of the class "Log record notification",
55  					"Allows you to get messages from the logger"
56  				)
57  		);
58  	
59  	/**
60  	 * Category name of the publisher.
61  	 */
62  	protected String categoryName;
63  	
64  	public JMXLogPublisher(ObjectName parent, String categoryName) throws MalformedObjectNameException {
65  		this.categoryName = categoryName;
66          this.objectName = JMXLogCategories.getJMXLogCategoryName(parent, categoryName);
67  	}
68  
69          @Override
70  	public String getCategoryName() {
71  		return categoryName;
72  	}
73  	
74  	/**
75  	 * Sequence number for the published logs.
76  	 */
77  	protected long sequenceNumber = 1;
78  
79  	@Override
80  	public void close() throws SecurityException {
81  	}
82  
83  	@Override
84  	public void flush() {
85  	}
86  	
87  	@Override
88  	public void publish(LogRecord record) {
89  		Formatter actualFormatter = formatter;
90  		if (actualFormatter != null) {
91  			String message = actualFormatter.format(record); 
92  			notification.sendNotification(
93  				new JMXLogRecordNotification(
94  					objectName,
95  					sequenceNumber++, 
96  					record.getMillis(),
97  					message,
98  					record
99  				)
100 			);
101 		}
102 	}
103 
104 	/**
105 	 * Not used, things are published directly via publish(LogRecord)
106 	 */
107 	@Override
108 	public void publish(LogRecord record, String formattedMsg) {
109 		// not used, things published in publish(LogRecord) method
110 	}
111 	
112 	//
113 	// JMX Notification Interface follows
114 	//
115 	
116 	@Override
117 	public void removeNotificationListener(NotificationListener listener,
118 			NotificationFilter filter, Object handback)
119 			throws ListenerNotFoundException {
120 		notification.removeNotificationListener(listener, filter, handback);		
121 	}
122 
123 	@Override
124 	public void addNotificationListener(NotificationListener listener,
125 			NotificationFilter filter, Object handback)
126 			throws IllegalArgumentException {
127 		notification.addNotificationListener(listener, filter, handback);		
128 	}
129 
130 	@Override
131 	public MBeanNotificationInfo[] getNotificationInfo() {		
132 		return notification.getNotificationInfo();
133 	}
134 
135 	@Override
136 	public void removeNotificationListener(NotificationListener listener)
137 			throws ListenerNotFoundException {
138 		notification.removeNotificationListener(listener);		
139 	}
140 
141 }