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
22
23 ObjectName objectName = null;
24
25
26
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
39
40
41
42
43 public JMXLogPublisher(Formatter formatter) {
44 super(formatter);
45 }
46
47
48
49
50 protected NotificationBroadcasterSupport notification =
51 new NotificationBroadcasterSupport(
52 new MBeanNotificationInfo(
53 new String[]{ JMXLogRecordNotification.NOTIFICATION_TYPE },
54 JMXLogRecordNotification.class.getName(),
55 "Allows you to get messages from the logger"
56 )
57 );
58
59
60
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
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
106
107 @Override
108 public void publish(LogRecord record, String formattedMsg) {
109
110 }
111
112
113
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 }