📄 envhelp.java
字号:
/* * @(#)EnvHelp.java 1.33 04/02/13 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */package com.sun.jmx.remote.util;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.util.Collection;import java.util.HashMap;import java.util.Hashtable;import java.util.Iterator;import java.util.Map;import java.util.SortedMap;import java.util.SortedSet;import java.util.StringTokenizer;import java.util.TreeMap;import java.util.TreeSet;import java.security.AccessController;import java.security.PrivilegedAction;import java.security.PrivilegedActionException;import java.security.PrivilegedExceptionAction;import javax.management.ObjectName;import javax.management.MBeanServer;import javax.management.InstanceNotFoundException;import javax.management.remote.JMXConnectorFactory;import javax.management.remote.JMXConnectorServerFactory;import com.sun.jmx.mbeanserver.GetPropertyAction;public class EnvHelp { /** * <p>Name of the attribute that specifies a default class loader * object. * The value associated with this attribute is a ClassLoader object</p> */ private static final String DEFAULT_CLASS_LOADER = JMXConnectorFactory.DEFAULT_CLASS_LOADER; /** * <p>Name of the attribute that specifies a default class loader * ObjectName. * The value associated with this attribute is an ObjectName object</p> */ private static final String DEFAULT_CLASS_LOADER_NAME = JMXConnectorServerFactory.DEFAULT_CLASS_LOADER_NAME; /** * Get the Connector Server default class loader. * <p> * Returns: * <p> * <ul> * <li> * The ClassLoader object found in <var>env</var> for * <tt>jmx.remote.default.class.loader</tt>, if any. * </li> * <li> * The ClassLoader pointed to by the ObjectName found in * <var>env</var> for <tt>jmx.remote.default.class.loader.name</tt>, * and registered in <var>mbs</var> if any. * </li> * <li> * The current thread's context classloader otherwise. * </li> * </ul> * * @param env Environment attributes. * @param mbs The MBeanServer for which the connector server provides * remote access. * * @return the connector server's default class loader. * * @exception IllegalArgumentException if one of the following is true: * <ul> * <li>both * <tt>jmx.remote.default.class.loader</tt> and * <tt>jmx.remote.default.class.loader.name</tt> are specified, * </li> * <li>or * <tt>jmx.remote.default.class.loader</tt> is not * an instance of {@link ClassLoader}, * </li> * <li>or * <tt>jmx.remote.default.class.loader.name</tt> is not * an instance of {@link ObjectName}, * </li> * <li>or * <tt>jmx.remote.default.class.loader.name</tt> is specified * but <var>mbs</var> is null. * </li> * @exception InstanceNotFoundException if * <tt>jmx.remote.default.class.loader.name</tt> is specified * and the ClassLoader MBean is not found in <var>mbs</var>. */ public static ClassLoader resolveServerClassLoader(Map env, MBeanServer mbs) throws InstanceNotFoundException { if (env == null) return Thread.currentThread().getContextClassLoader(); Object loader = env.get(DEFAULT_CLASS_LOADER); Object name = env.get(DEFAULT_CLASS_LOADER_NAME); if (loader != null && name != null) { final String msg = "Only one of " + DEFAULT_CLASS_LOADER + " or " + DEFAULT_CLASS_LOADER_NAME + " should be specified."; throw new IllegalArgumentException(msg); } if (loader == null && name == null) return Thread.currentThread().getContextClassLoader(); if (loader != null) { if (loader instanceof ClassLoader) { return (ClassLoader) loader; } else { final String msg = "ClassLoader object is not an instance of " + ClassLoader.class.getName() + " : " + loader.getClass().getName(); throw new IllegalArgumentException(msg); } } ObjectName on; if (name instanceof ObjectName) { on = (ObjectName) name; } else { final String msg = "ClassLoader name is not an instance of " + ObjectName.class.getName() + " : " + name.getClass().getName(); throw new IllegalArgumentException(msg); } if (mbs == null) throw new IllegalArgumentException("Null MBeanServer object"); return mbs.getClassLoader(on); } /** * Get the Connector Client default class loader. * <p> * Returns: * <p> * <ul> * <li> * The ClassLoader object found in <var>env</var> for * <tt>jmx.remote.default.class.loader</tt>, if any. * </li> * <li>The <tt>Thread.currentThread().getContextClassLoader()</tt> * otherwise. * </li> * </ul> * <p> * Usually a Connector Client will call * <pre> * ClassLoader dcl = EnvHelp.resolveClientClassLoader(env); * </pre> * in its <tt>connect(Map env)</tt> method. * * @return The connector client default class loader. * * @exception IllegalArgumentException if * <tt>jmx.remote.default.class.loader</tt> is specified * and is not an instance of {@link ClassLoader}. */ public static ClassLoader resolveClientClassLoader(Map env) { if (env == null) return Thread.currentThread().getContextClassLoader(); Object loader = env.get(DEFAULT_CLASS_LOADER); if (loader == null) return Thread.currentThread().getContextClassLoader(); if (loader instanceof ClassLoader) { return (ClassLoader) loader; } else { final String msg = "ClassLoader object is not an instance of " + ClassLoader.class.getName() + " : " + loader.getClass().getName(); throw new IllegalArgumentException(msg); } } /** * Initialize the cause field of a {@code Throwable} object. * * @param throwable The {@code Throwable} on which the cause is set. * @param cause The cause to set on the supplied {@code Throwable}. * @return the {@code Throwable} with the cause field initialized. */ public static <T extends Throwable> T initCause(T throwable, Throwable cause) { throwable.initCause(cause); return throwable; } /** * Returns the cause field of a Throwable object. * The cause field can be got only if <var>t</var> has an * {@link Throwable#getCause()} method (JDK Version >= 1.4) * @param t Throwable on which the cause must be set. * @return the cause if getCause() succeeded and the got value is not * null, otherwise return the <var>t</var>. */ public static Throwable getCause(Throwable t) { Throwable ret = t; try { java.lang.reflect.Method getCause = t.getClass().getMethod("getCause", (Class[]) null); ret = (Throwable)getCause.invoke(t, (Object[]) null); } catch (Exception e) { // OK. // it must be older than 1.4. } return (ret != null) ? ret: t; } /** * <p>Name of the attribute that specifies the size of a notification * buffer for a connector server. The default value is 1000. */ public static final String BUFFER_SIZE_PROPERTY = "jmx.remote.x.notification.buffer.size"; /** * Returns the size of a notification buffer for a connector server. * The default value is 1000. */ public static int getNotifBufferSize(Map env) { int defaultQueueSize = 1000; // default value // keep it for the compability for the fix: // 6174229: Environment parameter should be notification.buffer.size // instead of buffer.size final String oldP = "jmx.remote.x.buffer.size"; // the default value re-specified in the system try { GetPropertyAction act = new GetPropertyAction(BUFFER_SIZE_PROPERTY); String s = (String)AccessController.doPrivileged(act); if (s != null) { defaultQueueSize = Integer.parseInt(s); } else { // try the old one act = new GetPropertyAction(oldP); s = (String)AccessController.doPrivileged(act); if (s != null) { defaultQueueSize = Integer.parseInt(s); } } } catch (RuntimeException e) { logger.warning("getNotifBufferSize", "Can't use System property "+ BUFFER_SIZE_PROPERTY+ ": " + e); logger.debug("getNotifBufferSize", e); } int queueSize = defaultQueueSize; try { if (env.containsKey(BUFFER_SIZE_PROPERTY)) { queueSize = (int)EnvHelp.getIntegerAttribute(env,BUFFER_SIZE_PROPERTY, defaultQueueSize,0, Integer.MAX_VALUE); } else { // try the old one queueSize = (int)EnvHelp.getIntegerAttribute(env,oldP, defaultQueueSize,0, Integer.MAX_VALUE); } } catch (RuntimeException e) { logger.warning("getNotifBufferSize", "Can't determine queuesize (using default): "+ e); logger.debug("getNotifBufferSize", e); } return queueSize; } /** * <p>Name of the attribute that specifies the maximum number of * notifications that a client will fetch from its server.. The * value associated with this attribute should be an * <code>Integer</code> object. The default value is 1000.</p> */ public static final String MAX_FETCH_NOTIFS = "jmx.remote.x.notification.fetch.max"; /** * Returns the maximum notification number which a client will * fetch every time. */ public static int getMaxFetchNotifNumber(Map env) { return (int) getIntegerAttribute(env, MAX_FETCH_NOTIFS, 1000, 1, Integer.MAX_VALUE); } /** * <p>Name of the attribute that specifies the timeout for a * client to fetch notifications from its server. The value * associated with this attribute should be a <code>Long</code> * object. The default value is 60000 milleseconds.</p> */ public static final String FETCH_TIMEOUT = "jmx.remote.x.notification.fetch.timeout"; /** * Returns the timeout for a client to fetch notifications. */ public static long getFetchTimeout(Map env) { return getIntegerAttribute(env, FETCH_TIMEOUT, 60000L, 0, Long.MAX_VALUE); } /** * Get an integer-valued attribute with name <code>name</code> * from <code>env</code>. If <code>env</code> is null, or does * not contain an entry for <code>name</code>, return * <code>defaultValue</code>. The value may be a Number, or it * may be a String that is parsable as a long. It must be at * least <code>minValue</code> and at most<code>maxValue</code>. * * @throws IllegalArgumentException if <code>env</code> contains * an entry for <code>name</code> but it does not meet the * constraints above. */ public static long getIntegerAttribute(Map env, String name, long defaultValue, long minValue,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -