1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
|
/*
* Copyright (c) 2004, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.monitoring.share;
import java.lang.management.*;
import java.util.logging.*;
import javax.management.*;
import java.io.IOException;
import java.util.List;
import nsk.share.*;
/**
* <code>LoggingMonitor</code> class is a wrapper of <code>LoggingMXBean</code>.
* Depending on command line arguments, an instance of this class redirects
* invocations to the <code>LoggingMXBean</code> interface. If
* <code>-testMode="directly"</code> option is set, this instance directly
* invokes corresponding method of the <code>LoggingMXBean</code> interface. If
* <code>-testMode="server"</code> option is set it will make invocations via
* MBeanServer. If <code>-testMode="proxy"</code> option is set it will make
* invocations via MBeanServer proxy.
*
* @see ArgumentHandler
*/
public class LoggingMonitor extends Monitor {
public static final String LOGGING_MXBEAN_NAME =
LogManager.LOGGING_MXBEAN_NAME;
private static LoggingMXBean mbean = LogManager.getLoggingMXBean();
private LoggingMXBean proxyInstance = null;
// Internal trace level
private static final int TRACE_LEVEL = 10;
// Names of the attributes of ClassLoadingMXBean
private static final String GET_LOGGER_LEVEL = "getLoggerLevel";
private static final String SET_LOGGER_LEVEL = "setLoggerLevel";
private static final String GET_PARENT_LOGGER_NAME = "getParentLoggerName";
private static final String LOGGER_NAMES = "LoggerNames";
static {
Monitor.logPrefix = "LoggingMonitor> ";
}
/**
* Creates a new <code>LoggingMonitor</code> object.
*
* @param log <code>Log</code> object to print info to.
* @param argumentHandler <code>ArgumentHandler</code> object that saves
* all info about test's arguments.
*
*/
public LoggingMonitor(Log log, ArgumentHandler argumentHandler) {
super(log, argumentHandler);
}
public ObjectName getMBeanObjectName() {
return mbeanObjectName;
}
/**
*
* Return a proxy instance for a platform
* {@link java.lang.management.LoggingMXBean
* <code>LoggingMXBean</code>} interface.
*
*/
LoggingMXBean getProxy() {
if (proxyInstance == null) {
// create proxy instance
try {
proxyInstance = (LoggingMXBean)
ManagementFactory.newPlatformMXBeanProxy(
getMBeanServer(),
LOGGING_MXBEAN_NAME,
LoggingMXBean.class
);
} catch (Exception e) {
throw new Failure(e);
}
}
return proxyInstance;
}
/**
* Redirects the invocation to
* {@link java.util.logging.LoggingMXBean#getLoggerLevel(String)
* <code>LoggingMXBean.getLoggerLevel(String loggerName)</code>}.
*
* @param loggerName The name of the <tt>Logger</tt> to be retrieved.
*
* @return The name of the log level of the specified logger; or
* an empty string if the log level of the specified logger
* is <tt>null</tt>. If the specified logger does not
* exist, <tt>null</tt> is returned.
*
* @see java.util.logging.LoggingMXBean#getLoggerLevel(String)
*/
public String getLoggerLevel(String loggerName) {
int mode = getTestMode();
switch (mode) {
case DIRECTLY_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerLevel] "
+ "getLoggerLevel() directly invoked");
return mbean.getLoggerLevel(loggerName);
case SERVER_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerLevel] "
+ "getLoggerLevel() invoked through MBeanServer");
try {
Object[] params = { loggerName };
String[] signature = { String.class.getName() };
String res = (String) getMBeanServer().invoke(
mbeanObjectName,
GET_LOGGER_LEVEL,
params,
signature );
return res;
} catch (Exception e) {
throw new Failure(e);
}
case PROXY_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerLevel] "
+ "getLoggerLevel() invoked through MBeanServer proxy");
return getProxy().getLoggerLevel(loggerName);
}
throw new TestBug("Unknown testMode " + mode);
}
/**
* Redirects the invocation to
* {@link java.util.logging.LoggingMXBean#getLoggerNames()
* <code>LoggingMXBean.getLoggerNames()</code>}.
*
* @return A list of <tt>String</tt> each of which is a
* currently registered <tt>Logger</tt> name.
*
* @see java.util.logging.LoggingMXBean#getLoggerNames()
*/
public List<String> getLoggerNames() {
int mode = getTestMode();
switch (mode) {
case DIRECTLY_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerNames] "
+ "getLoggerNames() directly invoked");
return mbean.getLoggerNames();
case SERVER_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerNames] "
+ "getLoggerNames() invoked through MBeanServer");
try {
Object value = getMBeanServer().getAttribute(
mbeanObjectName, LOGGER_NAMES);
if (value instanceof List)
return (List<String>) value;
else if (value instanceof Object[]) {
Object[] names = (Object[]) value;
List<String> res = new java.util.ArrayList<String>(names.length);
for (int i=0; i < names.length; i++)
res.add(names[i].toString());
return res;
} else {
String[] names = (String[]) getMBeanServer().getAttribute(
mbeanObjectName, LOGGER_NAMES);
List<String> res = new java.util.ArrayList<String>(names.length);
for (int i=0; i<names.length; i++)
res.add(names[i]);
return res;
}
} catch (Exception e) {
throw new Failure(e);
}
case PROXY_MODE:
logger.trace(TRACE_LEVEL,"[getLoggerNames] "
+ "getLoggerNames() invoked through MBeanServer proxy");
return getProxy().getLoggerNames();
}
throw new TestBug("Unknown testMode " + mode);
}
/**
* Redirects the invocation to
* {@link java.util.logging.LoggingMXBean#getParentLoggerName(String)
* <code>LoggingMXBean.getParentLoggerName(String loggerName)</code>}.
*
* @param loggerName The name of a <tt>Logger</tt>.
*
* @return the name of the nearest existing parent logger;
* an empty string if the specified logger is the root logger.
* If the specified logger does not exist, <tt>null</tt>
* is returned.
*
* @see java.util.logging.LoggingMXBean#getParentLoggerName(String)
*/
public String getParentLoggerName(String loggerName) {
int mode = getTestMode();
switch (mode) {
case DIRECTLY_MODE:
logger.trace(TRACE_LEVEL,"[getParentLoggerName] "
+ "getParentLoggerName() directly invoked");
return mbean.getParentLoggerName(loggerName);
case SERVER_MODE:
logger.trace(TRACE_LEVEL,"[getParentLoggerName] "
+ "getParentLoggerName() invoked through MBeanServer");
try {
Object[] params = { loggerName };
String[] signature = { String.class.getName() };
String res = (String) getMBeanServer().invoke(
mbeanObjectName,
GET_PARENT_LOGGER_NAME,
params,
signature );
return res;
} catch (Exception e) {
throw new Failure(e);
}
case PROXY_MODE:
logger.trace(TRACE_LEVEL,"[getParentLoggerName] "
+ "getParentLoggerName() invoked through MBeanServer proxy");
return getProxy().getParentLoggerName(loggerName);
}
throw new TestBug("Unknown testMode " + mode);
}
/**
* Redirects the invocation to
* {@link java.util.logging.LoggingMXBean#setLoggerLevel(String,String)
* <code>LoggingMXBean.setLoggerLevel(String loggerName, String levelName)</code>}.
*
* @param loggerName The name of the <tt>Logger</tt> to be set.
* Must be non-null.
* @param levelName The name of the level to set the specified logger to,
* or <tt>null</tt> if to set the level to inherit
* from its nearest ancestor.
*
* @see java.util.logging.LoggingMXBean#setLoggerLevel(String,String)
*/
public void setLoggerLevel(String loggerName, String levelName) {
int mode = getTestMode();
switch (mode) {
case DIRECTLY_MODE:
logger.trace(TRACE_LEVEL,"[setLoggerLevel] "
+ "setLoggerLevel() directly invoked");
mbean.setLoggerLevel(loggerName, levelName);
break;
case SERVER_MODE:
logger.trace(TRACE_LEVEL,"[setLoggerLevel] "
+ "setLoggerLevel() invoked through MBeanServer");
try {
Object[] params = { loggerName, levelName };
String[] signature = { String.class.getName(), String.class.getName() };
getMBeanServer().invoke(
mbeanObjectName,
SET_LOGGER_LEVEL,
params,
signature );
} catch (Exception e) {
throw new Failure(e);
}
break;
case PROXY_MODE:
logger.trace(TRACE_LEVEL,"[setLoggerLevel] "
+ "setLoggerLevel() invoked through MBeanServer proxy");
getProxy().setLoggerLevel(loggerName, levelName);
break;
}
throw new TestBug("Unknown testMode " + mode);
}
} // LoggingMonitor
|