jdk/src/share/classes/com/sun/jmx/remote/util/EnvHelp.java
changeset 1004 5ba8217eb504
parent 2 90ce3da70b43
child 1247 b4c26443dee5
--- a/jdk/src/share/classes/com/sun/jmx/remote/util/EnvHelp.java	Thu Jul 31 14:20:11 2008 +0200
+++ b/jdk/src/share/classes/com/sun/jmx/remote/util/EnvHelp.java	Thu Jul 31 15:31:13 2008 +0200
@@ -40,9 +40,6 @@
 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;
@@ -50,6 +47,9 @@
 import javax.management.remote.JMXConnectorFactory;
 import javax.management.remote.JMXConnectorServerFactory;
 import com.sun.jmx.mbeanserver.GetPropertyAction;
+import com.sun.jmx.remote.security.NotificationAccessController;
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorServer;
 
 public class EnvHelp {
 
@@ -346,7 +346,24 @@
      */
     public static long getFetchTimeout(Map env) {
         return getIntegerAttribute(env, FETCH_TIMEOUT, 60000L, 0,
-                                   Long.MAX_VALUE);
+                Long.MAX_VALUE);
+    }
+
+    /**
+     * <p>Name of the attribute that specifies an object that will check
+     * accesses to add/removeNotificationListener and also attempts to
+     * receive notifications.  The value associated with this attribute
+     * should be a <code>NotificationAccessController</code> object.
+     * The default value is null.</p>
+     * This field is not public because of its com.sun dependency.
+     */
+    public static final String NOTIF_ACCESS_CONTROLLER =
+            "com.sun.jmx.remote.notification.access.controller";
+
+    public static NotificationAccessController getNotificationAccessController(
+            Map env) {
+        return (env == null) ? null :
+            (NotificationAccessController) env.get(NOTIF_ACCESS_CONTROLLER);
     }
 
     /**
@@ -470,24 +487,24 @@
     }
 
     /**
-       The value of this attribute, if present, is a string specifying
-       what other attributes should not appear in
-       JMXConnectorServer.getAttributes().  It is a space-separated
-       list of attribute patterns, where each pattern is either an
-       attribute name, or an attribute prefix followed by a "*"
-       character.  The "*" has no special significance anywhere except
-       at the end of a pattern.  By default, this list is added to the
-       list defined by {@link #DEFAULT_HIDDEN_ATTRIBUTES} (which
-       uses the same format).  If the value of this attribute begins
-       with an "=", then the remainder of the string defines the
-       complete list of attribute patterns.
+     * The value of this attribute, if present, is a string specifying
+     * what other attributes should not appear in
+     * JMXConnectorServer.getAttributes().  It is a space-separated
+     * list of attribute patterns, where each pattern is either an
+     * attribute name, or an attribute prefix followed by a "*"
+     * character.  The "*" has no special significance anywhere except
+     * at the end of a pattern.  By default, this list is added to the
+     * list defined by {@link #DEFAULT_HIDDEN_ATTRIBUTES} (which
+     * uses the same format).  If the value of this attribute begins
+     * with an "=", then the remainder of the string defines the
+     * complete list of attribute patterns.
      */
     public static final String HIDDEN_ATTRIBUTES =
         "jmx.remote.x.hidden.attributes";
 
     /**
-       Default list of attributes not to show.
-       @see #HIDDEN_ATTRIBUTES
+     * Default list of attributes not to show.
+     * @see #HIDDEN_ATTRIBUTES
      */
     /* This list is copied directly from the spec, plus
        java.naming.security.*.  Most of the attributes here would have
@@ -651,6 +668,8 @@
      * @param env the environment map.
      * @param prop the name of the property in the environment map whose
      * returned string value must be converted into a boolean value.
+     * @param systemProperty if true, consult a system property of the
+     * same name if there is no entry in the environment map.
      *
      * @return
      *   <ul>
@@ -671,16 +690,73 @@
      * @throws ClassCastException if {@code env.get(prop)} cannot be cast
      * to {@code String}.
      */
-    public static boolean computeBooleanFromString(Map env, String prop)
-        throws IllegalArgumentException, ClassCastException {
+    public static boolean computeBooleanFromString(
+            Map env, String prop, boolean systemProperty) {
+
+        if (env == null)
+            throw new IllegalArgumentException("env map cannot be null");
+
+        // returns a default value of 'false' if no property is found...
+        return computeBooleanFromString(env,prop,systemProperty,false);
+    }
+
+    /**
+     * Computes a boolean value from a string value retrieved from a
+     * property in the given map.
+     *
+     * @param env the environment map.
+     * @param prop the name of the property in the environment map whose
+     * returned string value must be converted into a boolean value.
+     * @param systemProperty if true, consult a system property of the
+     * same name if there is no entry in the environment map.
+     * @param defaultValue a default value to return in case no property
+     *        was defined.
+     *
+     * @return
+     *   <ul>
+     *   <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
+     *       and {@code systemProperty} is {@code false}</li>
+     *   <li>{@code defaultValue} if {@code env.get(prop)} is {@code null}
+     *       and {@code systemProperty} is {@code true} and
+     *       {@code System.getProperty(prop)} is {@code null}</li>
+     *   <li>{@code false} if {@code env.get(prop)} is {@code null}
+     *       and {@code systemProperty} is {@code true} and
+     *       {@code System.getProperty(prop).equalsIgnoreCase("false")}
+     *       is {@code true}</li>
+     *   <li>{@code true} if {@code env.get(prop)} is {@code null}
+     *       and {@code systemProperty} is {@code true} and
+     *       {@code System.getProperty(prop).equalsIgnoreCase("true")}
+     *       is {@code true}</li>
+     *   <li>{@code false} if
+     *       {@code ((String)env.get(prop)).equalsIgnoreCase("false")}
+     *       is {@code true}</li>
+     *   <li>{@code true} if
+     *       {@code ((String)env.get(prop)).equalsIgnoreCase("true")}
+     *       is {@code true}</li>
+     *   </ul>
+     *
+     * @throws IllegalArgumentException if {@code env} is {@code null} or
+     * {@code env.get(prop)} is not {@code null} and
+     * {@code ((String)env.get(prop)).equalsIgnoreCase("false")} and
+     * {@code ((String)env.get(prop)).equalsIgnoreCase("true")} are
+     * {@code false}.
+     * @throws ClassCastException if {@code env.get(prop)} cannot be cast
+     * to {@code String}.
+     */
+    public static boolean computeBooleanFromString(
+            Map env, String prop, boolean systemProperty, boolean defaultValue) {
 
         if (env == null)
             throw new IllegalArgumentException("env map cannot be null");
 
         String stringBoolean = (String) env.get(prop);
+        if (stringBoolean == null && systemProperty) {
+            stringBoolean =
+                    AccessController.doPrivileged(new GetPropertyAction(prop));
+        }
 
         if (stringBoolean == null)
-            return false;
+            return defaultValue;
         else if (stringBoolean.equalsIgnoreCase("true"))
             return true;
         else if (stringBoolean.equalsIgnoreCase("false"))
@@ -703,6 +779,65 @@
         return new Hashtable<K, V>(m);
     }
 
+    /**
+     * Returns true if the parameter JMXConnector.USE_EVENT_SERVICE is set to a
+     * String equals "true" by ignoring case in the map or in the System.
+     */
+    public static boolean eventServiceEnabled(Map env) {
+        return computeBooleanFromString(env, JMXConnector.USE_EVENT_SERVICE, true);
+    }
+
+    /**
+     * Returns true if the parameter JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE
+     * is set to a String equals "true" (ignores case).
+     * If the property DELEGATE_TO_EVENT_SERVICE is not set, returns
+     * a default value of "true".
+     */
+    public static boolean delegateToEventService(Map env) {
+        return computeBooleanFromString(env,
+                JMXConnectorServer.DELEGATE_TO_EVENT_SERVICE, true, true);
+    }
+
+//    /**
+//     * <p>Name of the attribute that specifies an EventRelay object to use.
+//     */
+//    public static final String EVENT_RELAY =
+//            "jmx.remote.x.event.relay";
+//
+//
+//    /**
+//     * Returns an EventRelay object. The default one is FetchingEventRelay.
+//     * If {@code EVENT_RELAY} is specified in {@code env} as a key,
+//     * its value will be returned as an EventRelay object, if the value is
+//     * not of type {@code EventRelay}, the default {@code FetchingEventRelay}
+//     * will be returned.
+//     * If {@code EVENT_RELAY} is not specified but {@code ENABLE_EVENT_RELAY}
+//     * is specified as a key and its value is <code true>, the default {@code FetchingEventRelay}
+//     * will be returned.
+//     */
+//    public static EventRelay getEventRelay(Map env) {
+//        Map info = env == null ?
+//            Collections.EMPTY_MAP : env;
+//
+//        Object o = env.get(EVENT_RELAY);
+//        if (o instanceof EventRelay) {
+//            return (EventRelay)o;
+//        } else if (o != null) {
+//            logger.warning("getEventRelay",
+//                    "The user specified object is not an EventRelay object, " +
+//                    "using the default class FetchingEventRelay.");
+//
+//            return new FetchingEventRelay();
+//        }
+//
+//        if (enableEventRelay(env)) {
+//            return new FetchingEventRelay();
+//        }
+//
+//        return null;
+//    }
+
+
     private static final class SinkOutputStream extends OutputStream {
         public void write(byte[] b, int off, int len) {}
         public void write(int b) {}