jdk/src/share/classes/com/sun/tracing/ProviderFactory.java
changeset 4323 da93d0c0f2f2
parent 406 bde3a21bcab0
child 23333 b0af2c7c8c91
equal deleted inserted replaced
4322:6508d71adfd1 4323:da93d0c0f2f2
     2 package com.sun.tracing;
     2 package com.sun.tracing;
     3 
     3 
     4 import java.util.HashSet;
     4 import java.util.HashSet;
     5 import java.io.PrintStream;
     5 import java.io.PrintStream;
     6 import java.lang.reflect.Field;
     6 import java.lang.reflect.Field;
     7 import java.util.logging.Logger;
     7 import java.security.AccessController;
       
     8 import java.security.PrivilegedActionException;
       
     9 import java.security.PrivilegedExceptionAction;
       
    10 import sun.security.action.GetPropertyAction;
     8 
    11 
     9 import sun.tracing.NullProviderFactory;
    12 import sun.tracing.NullProviderFactory;
    10 import sun.tracing.PrintStreamProviderFactory;
    13 import sun.tracing.PrintStreamProviderFactory;
    11 import sun.tracing.MultiplexProviderFactory;
    14 import sun.tracing.MultiplexProviderFactory;
    12 import sun.tracing.dtrace.DTraceProviderFactory;
    15 import sun.tracing.dtrace.DTraceProviderFactory;
    50      */
    53      */
    51     public static ProviderFactory getDefaultFactory() {
    54     public static ProviderFactory getDefaultFactory() {
    52         HashSet<ProviderFactory> factories = new HashSet<ProviderFactory>();
    55         HashSet<ProviderFactory> factories = new HashSet<ProviderFactory>();
    53 
    56 
    54         // Try to instantiate a DTraceProviderFactory
    57         // Try to instantiate a DTraceProviderFactory
    55         String prop = null;
    58         String prop = AccessController.doPrivileged(
    56         try { prop = System.getProperty("com.sun.tracing.dtrace"); }
    59             new GetPropertyAction("com.sun.tracing.dtrace"));
    57         catch (java.security.AccessControlException e) {
    60 
    58             Logger.getAnonymousLogger().fine(
       
    59                 "Cannot access property com.sun.tracing.dtrace");
       
    60         }
       
    61         if ( (prop == null || !prop.equals("disable")) &&
    61         if ( (prop == null || !prop.equals("disable")) &&
    62              DTraceProviderFactory.isSupported() ) {
    62              DTraceProviderFactory.isSupported() ) {
    63             factories.add(new DTraceProviderFactory());
    63             factories.add(new DTraceProviderFactory());
    64         }
    64         }
    65 
    65 
    66         // Try to instantiate an output stream factory
    66         // Try to instantiate an output stream factory
    67         try { prop = System.getProperty("sun.tracing.stream"); }
    67         prop = AccessController.doPrivileged(
    68         catch (java.security.AccessControlException e) {
    68             new GetPropertyAction("sun.tracing.stream"));
    69             Logger.getAnonymousLogger().fine(
       
    70                 "Cannot access property sun.tracing.stream");
       
    71         }
       
    72         if (prop != null) {
    69         if (prop != null) {
    73             for (String spec : prop.split(",")) {
    70             for (String spec : prop.split(",")) {
    74                 PrintStream ps = getPrintStreamFromSpec(spec);
    71                 PrintStream ps = getPrintStreamFromSpec(spec);
    75                 if (ps != null) {
    72                 if (ps != null) {
    76                     factories.add(new PrintStreamProviderFactory(ps));
    73                     factories.add(new PrintStreamProviderFactory(ps));
    87         } else {
    84         } else {
    88             return new MultiplexProviderFactory(factories);
    85             return new MultiplexProviderFactory(factories);
    89         }
    86         }
    90     }
    87     }
    91 
    88 
    92     private static PrintStream getPrintStreamFromSpec(String spec) {
    89     private static PrintStream getPrintStreamFromSpec(final String spec) {
    93         try {
    90         try {
    94             // spec is in the form of <class>.<field>, where <class> is
    91             // spec is in the form of <class>.<field>, where <class> is
    95             // a fully specified class name, and <field> is a static member
    92             // a fully specified class name, and <field> is a static member
    96             // in that class.  The <field> must be a 'PrintStream' or subtype
    93             // in that class.  The <field> must be a 'PrintStream' or subtype
    97             // in order to be used.
    94             // in order to be used.
    98             int fieldpos = spec.lastIndexOf('.');
    95             final int fieldpos = spec.lastIndexOf('.');
    99             Class<?> cls = Class.forName(spec.substring(0, fieldpos));
    96             final Class<?> cls = Class.forName(spec.substring(0, fieldpos));
   100             Field f = cls.getField(spec.substring(fieldpos + 1));
    97 
   101             Class<?> fieldType = f.getType();
    98             Field f = AccessController.doPrivileged(new PrivilegedExceptionAction<Field>() {
       
    99                 public Field run() throws NoSuchFieldException {
       
   100                     return cls.getField(spec.substring(fieldpos + 1));
       
   101                 }
       
   102             });
       
   103 
   102             return (PrintStream)f.get(null);
   104             return (PrintStream)f.get(null);
   103         } catch (Exception e) {
   105         } catch (ClassNotFoundException e) {
   104             Logger.getAnonymousLogger().warning(
   106             throw new AssertionError(e);
   105                 "Could not parse sun.tracing.stream property: " + e);
   107         } catch (IllegalAccessException e) {
       
   108             throw new AssertionError(e);
       
   109         } catch (PrivilegedActionException e) {
       
   110             throw new AssertionError(e);
   106         }
   111         }
   107         return null;
       
   108     }
   112     }
   109 }
   113 }
   110 
   114