001: /**
002: * Copyright (C) 2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.util.monolog.wrapper.javaLog;
018:
019: import org.objectweb.util.monolog.wrapper.common.OutputStreamSwitcher; //import org.objectweb.util.monolog.api.BasicLevel;
020:
021: //import java.io.OutputStream;
022: import java.io.Writer; //import java.io.OutputStreamWriter;
023: //import java.io.UnsupportedEncodingException;
024: import java.util.logging.LogRecord;
025: import java.util.logging.ErrorManager;
026: import javax.management.ListenerNotFoundException;
027: import javax.management.MBeanNotificationInfo;
028: import javax.management.Notification;
029: import javax.management.NotificationBroadcasterSupport;
030: import javax.management.NotificationEmitter;
031: import javax.management.NotificationFilter;
032: import javax.management.NotificationListener;
033:
034: /**
035: * The aim of this class is to permit to specify the output for the console
036: * handler of the java.util.logging system.
037: *
038: * This console handler is also able to choose the right ouput (System.err
039: * or System.out) depending on the message level.
040: *
041: * @author S.Chassande-Barrioz
042: */
043: public class JMXHandler extends java.util.logging.Handler implements
044: NotificationEmitter {
045:
046: protected OutputStreamSwitcher oss;
047: private Writer writer;
048:
049: public JMXHandler() {
050: super ();
051: }
052:
053: CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport();
054:
055: public void addNotificationListener(NotificationListener listener,
056: NotificationFilter filter, Object handback)
057: throws IllegalArgumentException {
058: emitter.addNotificationListener(listener, filter, handback);
059: }
060:
061: public void removeNotificationListener(NotificationListener listener)
062: throws ListenerNotFoundException {
063: emitter.removeNotificationListener(listener);
064: }
065:
066: public void removeNotificationListener(
067: NotificationListener listener, NotificationFilter filter,
068: Object handback) throws ListenerNotFoundException {
069: emitter.removeNotificationListener(listener, filter, handback);
070: }
071:
072: public MBeanNotificationInfo[] getNotificationInfo() {
073: return emitter.getNotificationInfo();
074: }
075:
076: private long notificationSequence = 0;
077:
078: public void publish(LogRecord record) {
079: if (!isLoggable(record)) {
080: return;
081: }
082: String msg = record.getMessage();
083: Notification notification = new Notification(
084: "Monolog.JMXHandler.Log", "JMXHandler:Type=javaLog",
085: ++notificationSequence, System.currentTimeMillis(), msg);
086: notification.setUserData(record);
087: emitter.sendNotification(notification);
088: }
089:
090: class CustomNotificationBroadcasterSupport extends
091: NotificationBroadcasterSupport {
092: public void sendNotification(Notification notification) {
093: super .sendNotification(notification);
094: }
095:
096: public void addNotificationListener(
097: NotificationListener listener,
098: NotificationFilter filter, Object handback)
099: throws IllegalArgumentException {
100: super .addNotificationListener(listener, filter, handback);
101:
102: }
103: }
104:
105: public void flush() {
106: if (writer != null) {
107: try {
108: writer.flush();
109: } catch (Exception ex) {
110: // We don't want to throw an exception here, but we
111: // report the exception to any registered ErrorManager.
112: reportError(null, ex, ErrorManager.FLUSH_FAILURE);
113: }
114: }
115: }
116:
117: public void close() throws SecurityException {
118: flush();
119: }
120: }
|