001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SystemServiceLoggerMBeanImpl.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package com.sun.jbi.management.support;
030:
031: import com.sun.jbi.management.common.LoggerMBean;
032: import com.sun.jbi.management.support.MBeanHelper;
033: import com.sun.jbi.management.LocalStringKeys;
034:
035: import java.util.logging.Logger;
036: import java.util.logging.Level;
037:
038: import javax.management.Notification;
039: import javax.management.NotificationListener;
040: import javax.management.AttributeChangeNotification;
041: import javax.management.MBeanServer;
042: import javax.management.MBeanRegistration;
043: import javax.management.ObjectName;
044:
045: /**
046: * LoggerMBean defines standard controls for setting the properties of a
047: * single JBI Framework service or JBI Installable Component logger.
048: *
049: * @author Sun Microsystems, Inc.
050: */
051: public class SystemServiceLoggerMBeanImpl extends
052: DefaultLoggerMBeanImpl implements LoggerMBean,
053: NotificationListener, MBeanRegistration,
054: com.sun.jbi.util.Constants {
055: /**
056: * Flag to determine if this logger Mbean is listening to log level
057: * changes.
058: */
059: private boolean isListening;
060:
061: /**
062: *
063: */
064: com.sun.jbi.StringTranslator mTranslator;
065:
066: /** Constructs a <CODE>SystemServiceLoggerMBeanImpl</CODE>. */
067: public SystemServiceLoggerMBeanImpl(Logger mLogger)
068: throws Exception {
069: //allocate a logger:
070: this (mLogger, mLogger.getName());
071:
072: mTranslator = com.sun.jbi.util.EnvironmentAccess.getContext()
073: .getStringTranslator("com.sun.jbi.management");
074: isListening = false;
075: }
076:
077: /** Constructs a <CODE>SystemServiceLoggerMBeanImpl</CODE>. */
078: public SystemServiceLoggerMBeanImpl(Logger mLogger,
079: String aDisplayName) throws Exception {
080: //allocate a logger:
081: //mLogger = mLogger;
082: //mDisplayName = aDisplayName;
083: super (mLogger, aDisplayName);
084:
085: // If there is a persisted log level for this logger
086: // set the log level based on that.
087: Level level = MBeanHelper.getLogLevel(mLogger.getName(),
088: com.sun.jbi.util.EnvironmentAccess.getContext());
089:
090: if (level != null) {
091: mLogger.setLevel(level);
092: }
093:
094: mTranslator = com.sun.jbi.util.EnvironmentAccess.getContext()
095: .getStringTranslator("com.sun.jbi.management");
096:
097: isListening = false;
098: }
099:
100: /*---------------------------------------------------------------------------------*\
101: * NotificationListener Operations *
102: \*---------------------------------------------------------------------------------*/
103:
104: /**
105: * handle a notification from the Logger Configuration MBean
106: *
107: * @param notification - the notification
108: * @param the callback passed in
109: */
110: public void handleNotification(Notification notification,
111: Object handback) {
112: if (notification instanceof AttributeChangeNotification) {
113: AttributeChangeNotification notif = (AttributeChangeNotification) notification;
114:
115: if (notif.getAttributeName().equals(mLogger.getName())) {
116: mLogger
117: .fine("run time log level change notifictaion for "
118: + mLogger.getName()
119: + " from "
120: + notif.getOldValue()
121: + " to "
122: + notif.getNewValue());
123: }
124:
125: String level = (String) notif.getNewValue();
126: try {
127: if (level == null || level == "null"
128: || LOG_LEVEL_DEFAULT.equals(level)) {
129: super .setDefault();
130: } else {
131: mLogger.setLevel(Level.parse(level));
132: }
133: } catch (IllegalArgumentException iex) {
134: mLogger.fine("Failed to set log level for logger "
135: + mLogger.getName() + " to " + level + " : "
136: + iex.getMessage());
137: }
138: }
139: }
140:
141: /*---------------------------------------------------------------------------------*\
142: * MBean Registration Operations *
143: \*---------------------------------------------------------------------------------*/
144:
145: /**
146: * Preregsitration event.
147: */
148: public ObjectName preRegister(MBeanServer server, ObjectName name)
149: throws Exception {
150: return name;
151: }
152:
153: /**
154: * Post registration event. Start listening to log level changes
155: */
156: public void postRegister(Boolean registrationDone) {
157: if (registrationDone) {
158: startListeningToLogLevelChanges();
159: isListening = true;
160: mLogger
161: .fine("Logger "
162: + mLogger.getName()
163: + " is now listening for changes in the logger configuration");
164: }
165: }
166:
167: /**
168: * Prederegistration event.
169: */
170: public void preDeregister() throws Exception {
171: // nop
172: }
173:
174: /**
175: * Post deregistration event. Stop listening to log level changes
176: */
177: public void postDeregister() {
178: if (isListening) {
179: stopListeningToLogLevelChanges();
180: isListening = false;
181: mLogger
182: .fine("Logger "
183: + mLogger.getName()
184: + " has stopped listening for changes in the logger configuration");
185: }
186: }
187:
188: /*---------------------------------------------------------------------------------*\
189: * Private Helpers *
190: \*---------------------------------------------------------------------------------*/
191:
192: /**
193: * Register as a listener for attribute change events
194: */
195: private void startListeningToLogLevelChanges() {
196: javax.management.MBeanServer mbeanServer = com.sun.jbi.util.EnvironmentAccess
197: .getContext().getMBeanServer();
198:
199: /**
200: * Create a AttributeChangeNotificationFilter
201: */
202: javax.management.AttributeChangeNotificationFilter filter = new javax.management.AttributeChangeNotificationFilter();
203: filter.disableAllAttributes();
204: filter.enableAttribute(mLogger.getName());
205:
206: try {
207: mbeanServer.addNotificationListener(MBeanHelper
208: .getLoggerConfigMBeanName(), this , filter, null);
209: } catch (Exception ex) {
210: String[] params = new String[] { mDisplayName,
211: mLogger.getName(),
212: MBeanHelper.getLoggerConfigMBeanName().toString(),
213: ex.getMessage() };
214: String errMsg = mTranslator
215: .getString(
216: LocalStringKeys.JBI_ADMIN_FAILED_ADD_LOGGER_LISTENER,
217: params);
218: mLogger.info(errMsg);
219: }
220: }
221:
222: /**
223: * Stop listening to attribute change events
224: */
225: private void stopListeningToLogLevelChanges() {
226: javax.management.MBeanServer mbeanServer = com.sun.jbi.util.EnvironmentAccess
227: .getContext().getMBeanServer();
228:
229: try {
230: if (mbeanServer.isRegistered(MBeanHelper
231: .getLoggerConfigMBeanName())) {
232: mbeanServer.removeNotificationListener(MBeanHelper
233: .getLoggerConfigMBeanName(), this );
234: }
235: } catch (Exception ex) {
236: String[] params = new String[] { mDisplayName,
237: mLogger.getName(),
238: MBeanHelper.getLoggerConfigMBeanName().toString(),
239: ex.getMessage() };
240: String errMsg = mTranslator
241: .getString(
242: LocalStringKeys.JBI_ADMIN_FAILED_RM_LOGGER_LISTENER,
243: params);
244: mLogger.warning(errMsg);
245: }
246: }
247:
248: }
|