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: * @(#)MBeanHelper.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.StringTranslator;
032: import com.sun.jbi.management.LocalStringKeys;
033: import com.sun.jbi.management.MBeanNames;
034: import com.sun.jbi.management.system.ManagementContext;
035: import com.sun.jbi.util.EnvironmentAccess;
036: import javax.management.ObjectName;
037: import javax.management.MBeanServer;
038: import javax.management.StandardMBean;
039: import java.util.logging.Logger;
040: import java.util.logging.Level;
041:
042: /**
043: * Implementation of MBeanHelper interface.
044: *
045: * @author Sun Microsystems, Inc.
046: */
047: public class MBeanHelper implements com.sun.jbi.management.MBeanHelper,
048: com.sun.jbi.util.Constants {
049:
050: /**
051: * Handle to the main MBean server
052: */
053: private final MBeanServer mMBeanServer;
054:
055: /**
056: * Handle to the MBeanNames interface
057: */
058: private final MBeanNames mMBeanNames;
059:
060: /**
061: * Handle to the logger where this class will log errors.
062: */
063: private Logger mLogger;
064:
065: /**
066: * Handle to StringTranslator for message translation
067: */
068: private StringTranslator mTranslator;
069:
070: /**
071: * Environment Context
072: */
073: private com.sun.jbi.EnvironmentContext mCtx;
074:
075: /**
076: * Constructs a <CODE>MBeanHelper</CODE>.
077: * This class is meant to be instantiated only once by the framework.
078: * @param aMBeanServer is the MBean server provided by the framework.
079: * @param aMBeanNames is the MBeanNames instance created by the framework.
080: * @param aLogger is where this class should log messages.
081: */
082: public MBeanHelper(MBeanServer aMBeanServer,
083: com.sun.jbi.management.MBeanNames aMBeanNames,
084: Logger aLogger) {
085: mMBeanServer = aMBeanServer;
086: mMBeanNames = aMBeanNames;
087: mLogger = aLogger;
088: mCtx = com.sun.jbi.util.EnvironmentAccess.getContext();
089: mTranslator = mCtx
090: .getStringTranslator("com.sun.jbi.management");
091: }
092:
093: /**
094: * Create a LoggerMBean and associate it with <CODE>aLogger</CODE>.
095: * @param aService is the name of the system service that owns the logger.
096: * @param aLogger is the Logger to associate with the LoggerMBean.
097: * @return true if successful, otherwise false.
098: */
099: public boolean createSystemServiceLoggerMBean(String aService,
100: Logger aLogger) {
101: mLogger.fine("createSystemServiceLoggerMBean for Service="
102: + aService + " logger=" + aLogger.getName());
103:
104: // Create the object name for this service:
105: ObjectName mbname = mMBeanNames
106: .getSystemServiceLoggerMBeanName(aService, aLogger);
107: if (null == mbname) {
108: String statusMsg = mTranslator
109: .getString(
110: LocalStringKeys.MBEAN_HELPER_FAILED_OBJECT_CREATION,
111: aService, mMBeanNames.CONTROL_TYPE_LOGGER);
112: mLogger.severe(statusMsg);
113: return false;
114: }
115:
116: try {
117: //create a logger mbean implementation:
118: SystemServiceLoggerMBeanImpl loggerImpl = new SystemServiceLoggerMBeanImpl(
119: aLogger, aService);
120:
121: //register a standard mbean:
122: StandardMBean mbean = loggerImpl;
123: mMBeanServer.registerMBean(mbean, mbname);
124: } catch (Exception ex) {
125: String[] params = new String[] { aService,
126: aLogger.getName(), ex.getMessage() };
127: String locMsg = mTranslator
128: .getString(
129: com.sun.jbi.management.LocalStringKeys.JBI_ADMIN_LOGGER_MBN_CREATE_FAILED,
130: params);
131:
132: mLogger.warning(locMsg);
133: return false;
134: }
135:
136: return true;
137: }
138:
139: /**
140: * Destroy the LoggerMBean registered for <CODE>aService</CODE>.
141: * @param aService is the name of the system service that owns the logger.
142: * @return true if successful, otherwise false.
143: */
144: public boolean destroySystemServiceLoggerMBean(String aService) {
145: mLogger.fine("destroySystemServiceLoggerMBean for Service="
146: + aService);
147: //create the object name for this service:
148: ObjectName mbname = mMBeanNames.getSystemServiceMBeanName(
149: aService, mMBeanNames.CONTROL_TYPE_LOGGER);
150: if (null == mbname) {
151: String statusMsg = mTranslator
152: .getString(
153: LocalStringKeys.MBEAN_HELPER_FAILED_OBJECT_CREATION,
154: aService, mMBeanNames.CONTROL_TYPE_LOGGER);
155: mLogger.severe(statusMsg);
156: return false;
157: }
158: try {
159: mMBeanServer.unregisterMBean(mbname);
160: } catch (Exception e) {
161: /* throws:
162: * InstanceNotFoundException,
163: * MBeanRegistrationException,
164: * RuntimeOperationsException
165: */
166: return false;
167: }
168: return true;
169: }
170:
171: /**
172: * Destroy a LoggerMBean registered for <CODE>aService</CODE>.
173: * @param aService is the name of the system service that owns the logger.
174: * @param aLogger is the Logger associated with the LoggerMBean.
175: * @return true if successful, otherwise false.
176: */
177: public boolean destroySystemServiceLoggerMBean(String aService,
178: Logger aLogger) {
179: mLogger.fine("destroySystemServiceLoggerMBean for Service="
180: + aService);
181: // Create the object name for this service:
182: ObjectName mbname = mMBeanNames
183: .getSystemServiceLoggerMBeanName(aService, aLogger);
184: if (null == mbname) {
185: String statusMsg = mTranslator
186: .getString(
187: LocalStringKeys.MBEAN_HELPER_FAILED_OBJECT_CREATION,
188: aService, mMBeanNames.CONTROL_TYPE_LOGGER);
189: mLogger.severe(statusMsg);
190: return false;
191: }
192: try {
193: mMBeanServer.unregisterMBean(mbname);
194: } catch (Exception e) {
195: /* throws:
196: * InstanceNotFoundException,
197: * MBeanRegistrationException,
198: * RuntimeOperationsException
199: */
200: e.printStackTrace();
201: return false;
202: }
203: return true;
204: }
205:
206: /**
207: * Get the value of the management log level
208: *
209: * @param envCtx - management context
210: * @param attrName - name of the logger
211: * @return the log level of the requested logger or null if logger not found
212: */
213: public static Level getLogLevel(String attrName,
214: com.sun.jbi.EnvironmentContext envCtx) {
215: MBeanNames.ServiceType svcType = MBeanNames.ServiceType
216: .valueOf(com.sun.jbi.management.ConfigurationCategory.Logger
217: .toString());
218:
219: ObjectName configMBeanName = envCtx
220: .getMBeanNames()
221: .getSystemServiceMBeanName(
222: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
223: com.sun.jbi.management.config.ConfigurationBuilder
224: .getControlType(svcType));
225:
226: Level level = null;
227: try {
228: if (envCtx.getMBeanServer().isRegistered(configMBeanName)) {
229: String attrValue = (String) envCtx.getMBeanServer()
230: .getAttribute(configMBeanName, attrName);
231:
232: if (attrValue == null
233: || "null".equalsIgnoreCase(attrValue)
234: || LOG_LEVEL_DEFAULT
235: .equalsIgnoreCase(attrValue)) {
236: level = null;
237: } else {
238:
239: level = Level.parse(attrValue);
240: }
241: } else {
242: Logger.getLogger("com.sun.jbi").finest(
243: "MBean " + configMBeanName
244: + "is not registered");
245: }
246: } catch (javax.management.JMException jmex) {
247: Logger.getLogger("com.sun.jbi").warning(jmex.toString());
248: }
249:
250: return level;
251: }
252:
253: /**
254: * Set the value of the management log level
255: *
256: * @param envCtx - environment context
257: * @param logName - name of the logger
258: * @param logLevel - the log level string
259: * @return the log level of the requested logger or null if logger not found
260: */
261: public static void setLogLevel(String logName, String logLevel,
262: com.sun.jbi.EnvironmentContext envCtx, Logger aLogger) {
263: MBeanNames.ServiceType svcType = MBeanNames.ServiceType
264: .valueOf(com.sun.jbi.management.ConfigurationCategory.Logger
265: .toString());
266:
267: ObjectName configMBeanName = envCtx
268: .getMBeanNames()
269: .getSystemServiceMBeanName(
270: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
271: com.sun.jbi.management.config.ConfigurationBuilder
272: .getControlType(svcType));
273: try {
274: if (envCtx.getMBeanServer().isRegistered(configMBeanName)) {
275: javax.management.Attribute attrib = new javax.management.Attribute(
276: logName, logLevel);
277: envCtx.getMBeanServer().setAttribute(configMBeanName,
278: attrib);
279: } else {
280: aLogger.finest("MBean " + configMBeanName
281: + "is not registered");
282: }
283: } catch (javax.management.JMException jmex) {
284: aLogger.warning(jmex.toString());
285: }
286: }
287:
288: /**
289: * Revert to the global log level. This causes the persisted attribute to
290: * be deleted from the target for the instance. The global log level ( which
291: * is the same as the default / com.sun.jbi log level.
292: *
293: * @param loggerName - name of the logger whose override is to be deleted
294: * @param envCtx - environment context
295: * @param aLogger - logger to log any warnings to
296: */
297: public static void revertToGlobalLogLevel(String logName,
298: com.sun.jbi.EnvironmentContext envCtx, Logger aLogger) {
299: MBeanNames.ServiceType svcType = MBeanNames.ServiceType
300: .valueOf(com.sun.jbi.management.ConfigurationCategory.Logger
301: .toString());
302:
303: ObjectName configMBeanName = envCtx
304: .getMBeanNames()
305: .getSystemServiceMBeanName(
306: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
307: com.sun.jbi.management.config.ConfigurationBuilder
308: .getControlType(svcType));
309: try {
310: if (envCtx.getMBeanServer().isRegistered(configMBeanName)) {
311: envCtx.getMBeanServer().invoke(configMBeanName,
312: "deleteOverride", new Object[] { logName },
313: new String[] { "java.lang.String" });
314: } else {
315: aLogger.finest("MBean " + configMBeanName
316: + "is not registered");
317: }
318: } catch (javax.management.JMException jmex) {
319: aLogger.warning(jmex.toString());
320: }
321: }
322:
323: /**
324: * @return the object name of the Logger Configuration MBean on the instance
325: */
326: public static ObjectName getLoggerConfigMBeanName() {
327: MBeanNames.ServiceType svcType = MBeanNames.ServiceType
328: .valueOf(com.sun.jbi.management.ConfigurationCategory.Logger
329: .toString());
330:
331: return com.sun.jbi.util.EnvironmentAccess
332: .getContext()
333: .getMBeanNames()
334: .getSystemServiceMBeanName(
335: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
336: com.sun.jbi.management.config.ConfigurationBuilder
337: .getControlType(svcType));
338: }
339:
340: /**
341: * @return the object name of the System Configuration MBean on the instance
342: */
343: public static ObjectName getSystemConfigMBeanName() {
344: MBeanNames.ServiceType svcType = MBeanNames.ServiceType
345: .valueOf(com.sun.jbi.management.ConfigurationCategory.System
346: .toString());
347:
348: return com.sun.jbi.util.EnvironmentAccess
349: .getContext()
350: .getMBeanNames()
351: .getSystemServiceMBeanName(
352: MBeanNames.SERVICE_NAME_CONFIG_SERVICE,
353: com.sun.jbi.management.config.ConfigurationBuilder
354: .getControlType(svcType));
355: }
356: }
|