jdk/src/share/classes/com/sun/tracing/ProviderFactory.java
author never
Mon, 12 Jul 2010 22:27:18 -0700
changeset 5926 a36f90d986b6
parent 4323 da93d0c0f2f2
child 23333 b0af2c7c8c91
permissions -rw-r--r--
6968385: malformed xml in sweeper logging Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     1
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     2
package com.sun.tracing;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     3
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     4
import java.util.HashSet;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     5
import java.io.PrintStream;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
     6
import java.lang.reflect.Field;
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
     7
import java.security.AccessController;
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
     8
import java.security.PrivilegedActionException;
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
     9
import java.security.PrivilegedExceptionAction;
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    10
import sun.security.action.GetPropertyAction;
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    11
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    12
import sun.tracing.NullProviderFactory;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    13
import sun.tracing.PrintStreamProviderFactory;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    14
import sun.tracing.MultiplexProviderFactory;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    15
import sun.tracing.dtrace.DTraceProviderFactory;
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    16
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    17
/**
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    18
 * {@code ProviderFactory} is a factory class used to create instances of
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    19
 * providers.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    20
 *
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    21
 * To enable tracing in an application, this class must be used to create
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    22
 * instances of the provider interfaces defined by users.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    23
 * The system-defined factory is obtained by using the
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    24
 * {@code getDefaultFactory()} static method.  The resulting instance can be
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    25
 * used to create any number of providers.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    26
 *
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    27
 * @since 1.7
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    28
 */
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    29
public abstract class ProviderFactory {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    30
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    31
    protected ProviderFactory() {}
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    32
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    33
    /**
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    34
     * Creates an implementation of a Provider interface.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    35
     *
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    36
     * @param cls the provider interface to be defined.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    37
     * @return an implementation of {@code cls}, whose methods, when called,
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    38
     * will trigger tracepoints in the application.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    39
     * @throws NullPointerException if cls is null
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    40
     * @throws IllegalArgumentException if the class definition contains
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    41
     * non-void methods
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    42
     */
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    43
    public abstract <T extends Provider> T createProvider(Class<T> cls);
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    44
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    45
    /**
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    46
     * Returns an implementation of a {@code ProviderFactory} which
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    47
     * creates instances of Providers.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    48
     *
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    49
     * The created Provider instances will be linked to all appropriate
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    50
     * and enabled system-defined tracing mechanisms in the JDK.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    51
     *
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    52
     * @return a {@code ProviderFactory} that is used to create Providers.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    53
     */
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    54
    public static ProviderFactory getDefaultFactory() {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    55
        HashSet<ProviderFactory> factories = new HashSet<ProviderFactory>();
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    56
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    57
        // Try to instantiate a DTraceProviderFactory
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    58
        String prop = AccessController.doPrivileged(
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    59
            new GetPropertyAction("com.sun.tracing.dtrace"));
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    60
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    61
        if ( (prop == null || !prop.equals("disable")) &&
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    62
             DTraceProviderFactory.isSupported() ) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    63
            factories.add(new DTraceProviderFactory());
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    64
        }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    65
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    66
        // Try to instantiate an output stream factory
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    67
        prop = AccessController.doPrivileged(
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    68
            new GetPropertyAction("sun.tracing.stream"));
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    69
        if (prop != null) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    70
            for (String spec : prop.split(",")) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    71
                PrintStream ps = getPrintStreamFromSpec(spec);
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    72
                if (ps != null) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    73
                    factories.add(new PrintStreamProviderFactory(ps));
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    74
                }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    75
            }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    76
        }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    77
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    78
        // See how many factories we instantiated, and return an appropriate
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    79
        // factory that encapsulates that.
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    80
        if (factories.size() == 0) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    81
            return new NullProviderFactory();
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    82
        } else if (factories.size() == 1) {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    83
            return factories.toArray(new ProviderFactory[1])[0];
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    84
        } else {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    85
            return new MultiplexProviderFactory(factories);
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    86
        }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    87
    }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    88
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    89
    private static PrintStream getPrintStreamFromSpec(final String spec) {
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    90
        try {
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    91
            // spec is in the form of <class>.<field>, where <class> is
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    92
            // a fully specified class name, and <field> is a static member
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    93
            // in that class.  The <field> must be a 'PrintStream' or subtype
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
    94
            // in order to be used.
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    95
            final int fieldpos = spec.lastIndexOf('.');
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    96
            final Class<?> cls = Class.forName(spec.substring(0, fieldpos));
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    97
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    98
            Field f = AccessController.doPrivileged(new PrivilegedExceptionAction<Field>() {
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
    99
                public Field run() throws NoSuchFieldException {
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   100
                    return cls.getField(spec.substring(fieldpos + 1));
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   101
                }
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   102
            });
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   103
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
   104
            return (PrintStream)f.get(null);
4323
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   105
        } catch (ClassNotFoundException e) {
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   106
            throw new AssertionError(e);
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   107
        } catch (IllegalAccessException e) {
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   108
            throw new AssertionError(e);
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   109
        } catch (PrivilegedActionException e) {
da93d0c0f2f2 6902678: com.sun.tracing.ProviderFactory.createProvider doesn't throw IllegalArgumentException
mchung
parents: 406
diff changeset
   110
            throw new AssertionError(e);
406
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
   111
        }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
   112
    }
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
   113
}
bde3a21bcab0 6690122: Provide a mechanism for specifying Java-level USDT-like dtrace probes
kamg
parents:
diff changeset
   114