001: /**
002: * Copyright (C) 2001-2003 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.log4j;
018:
019: import org.objectweb.util.monolog.api.Handler;
020: import org.objectweb.util.monolog.api.MonologFactory;
021: import org.objectweb.util.monolog.wrapper.common.RelatifEnvironmentPathGetter;
022: import org.apache.log4j.AppenderSkeleton;
023: import org.apache.log4j.PatternLayout;
024: import org.apache.log4j.spi.LoggingEvent;
025:
026: import java.util.HashMap;
027: import java.util.Map; //import java.io.OutputStreamWriter;
028:
029: import javax.management.ListenerNotFoundException;
030: import javax.management.MBeanNotificationInfo;
031: import javax.management.Notification;
032: import javax.management.NotificationBroadcasterSupport;
033: import javax.management.NotificationEmitter;
034: import javax.management.NotificationFilter;
035: import javax.management.NotificationListener;
036:
037: /**
038: *
039: * @author Sebastien Chassande-Barrioz
040: */
041: public class JMXHandler extends AppenderSkeleton implements
042: NotificationEmitter, Handler {
043:
044: /**
045: * This fields contains the properties of the Handler
046: */
047: protected HashMap prop = null;
048:
049: public JMXHandler() {
050: super ();
051: }
052:
053: /**
054: * It Builds a new JMXHandler.
055: * @param name is the handler name.
056: */
057: public JMXHandler(String name) {
058: super ();
059: setName(name);
060: prop = new HashMap();
061: }
062:
063: CustomNotificationBroadcasterSupport emitter = new CustomNotificationBroadcasterSupport();
064:
065: public void addNotificationListener(NotificationListener listener,
066: NotificationFilter filter, Object handback)
067: throws IllegalArgumentException {
068: emitter.addNotificationListener(listener, filter, handback);
069: }
070:
071: public void removeNotificationListener(NotificationListener listener)
072: throws ListenerNotFoundException {
073: emitter.removeNotificationListener(listener);
074: }
075:
076: public void removeNotificationListener(
077: NotificationListener listener, NotificationFilter filter,
078: Object handback) throws ListenerNotFoundException {
079: emitter.removeNotificationListener(listener, filter, handback);
080: }
081:
082: public MBeanNotificationInfo[] getNotificationInfo() {
083: return emitter.getNotificationInfo();
084: }
085:
086: public Map getAttributes() {
087: return prop;
088: }
089:
090: public void setAttributes(Map attributes) {
091: prop.clear();
092: prop.putAll(attributes);
093: Object mf = prop.get("activation");
094: if (mf != null) {
095: prop.remove("activation");
096: setAttribute("activation", mf);
097: }
098: }
099:
100: // IMPLEMENTATION OF THE Handler INTERFACE //
101: //---------------------------------------------//
102: public String getType() {
103: return "jmx";
104: }
105:
106: public String[] getAttributeNames() {
107: return (String[]) prop.keySet().toArray(new String[0]);
108: }
109:
110: public Object getAttribute(String key) {
111: return prop.get(key);
112: }
113:
114: public Object setAttribute(String key, Object value) {
115: if (prop == null)
116: prop = new HashMap();
117: if (!key.equalsIgnoreCase("activation")) {
118: return prop.put(key, value);
119: } else if (prop.containsKey(key)) {
120: return null; //already activated
121: }
122: MonologFactory mf = (MonologFactory) value;
123: String pattern = (String) prop.get(Handler.PATTERN_ATTRIBUTE);
124: if (pattern != null) {
125: setLayout(new PatternLayout(PatternConverter
126: .monolog2log4j(pattern)));
127: }
128:
129: String level = (String) prop.get(Handler.LEVEL_ATTRIBUTE);
130: if (level != null && level.length() > 0) {
131: int levelVal = org.objectweb.util.monolog.wrapper.common.LevelImpl
132: .evaluate(level, mf);
133: setThreshold(org.apache.log4j.Level.toLevel(levelVal));
134: }
135:
136: String output = (String) prop.get(Handler.OUTPUT_ATTRIBUTE);
137: output = RelatifEnvironmentPathGetter.getRealPath(output);
138:
139: super .activateOptions();
140: return null;
141: }
142:
143: private long notificationSequence = 0;
144:
145: /* (non-Javadoc)
146: * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
147: */
148: public void doAppend(LoggingEvent event) {
149: Notification notification = new Notification(
150: "Monolog.JMXHandler.Log", "JMXHandler:Type=Log4j",
151: ++notificationSequence, System.currentTimeMillis(),
152: event.getMessage().toString());
153: notification.setUserData(event);
154: emitter.sendNotification(notification);
155: }
156:
157: class CustomNotificationBroadcasterSupport extends
158: NotificationBroadcasterSupport {
159:
160: public void sendNotification(Notification notification) {
161: super .sendNotification(notification);
162: }
163: }
164:
165: /* (non-Javadoc)
166: * @see org.apache.log4j.AppenderSkeleton#append(org.apache.log4j.spi.LoggingEvent)
167: */
168: protected void append(LoggingEvent event) {
169: this .append(event);
170: }
171:
172: /* (non-Javadoc)
173: * @see org.apache.log4j.Appender#close()
174: */
175: public void close() {
176: if (this != null) {
177: this .close();
178: }
179:
180: }
181:
182: /* (non-Javadoc)
183: * @see org.apache.log4j.Appender#requiresLayout()
184: */
185: public boolean requiresLayout() {
186: // TODO Auto-generated method stub
187: return false;
188: }
189:
190: }
|