Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/tracing/ProviderFactory.java
38831 views
/*1* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.tracing;2627import java.util.HashSet;28import java.io.PrintStream;29import java.lang.reflect.Field;30import java.security.AccessController;31import java.security.PrivilegedActionException;32import java.security.PrivilegedExceptionAction;33import sun.security.action.GetPropertyAction;3435import sun.tracing.NullProviderFactory;36import sun.tracing.PrintStreamProviderFactory;37import sun.tracing.MultiplexProviderFactory;38import sun.tracing.dtrace.DTraceProviderFactory;3940/**41* {@code ProviderFactory} is a factory class used to create instances of42* providers.43*44* To enable tracing in an application, this class must be used to create45* instances of the provider interfaces defined by users.46* The system-defined factory is obtained by using the47* {@code getDefaultFactory()} static method. The resulting instance can be48* used to create any number of providers.49*50* @since 1.751*/52public abstract class ProviderFactory {5354protected ProviderFactory() {}5556/**57* Creates an implementation of a Provider interface.58*59* @param cls the provider interface to be defined.60* @return an implementation of {@code cls}, whose methods, when called,61* will trigger tracepoints in the application.62* @throws NullPointerException if cls is null63* @throws IllegalArgumentException if the class definition contains64* non-void methods65*/66public abstract <T extends Provider> T createProvider(Class<T> cls);6768/**69* Returns an implementation of a {@code ProviderFactory} which70* creates instances of Providers.71*72* The created Provider instances will be linked to all appropriate73* and enabled system-defined tracing mechanisms in the JDK.74*75* @return a {@code ProviderFactory} that is used to create Providers.76*/77public static ProviderFactory getDefaultFactory() {78HashSet<ProviderFactory> factories = new HashSet<ProviderFactory>();7980// Try to instantiate a DTraceProviderFactory81String prop = AccessController.doPrivileged(82new GetPropertyAction("com.sun.tracing.dtrace"));8384if ( (prop == null || !prop.equals("disable")) &&85DTraceProviderFactory.isSupported() ) {86factories.add(new DTraceProviderFactory());87}8889// Try to instantiate an output stream factory90prop = AccessController.doPrivileged(91new GetPropertyAction("sun.tracing.stream"));92if (prop != null) {93for (String spec : prop.split(",")) {94PrintStream ps = getPrintStreamFromSpec(spec);95if (ps != null) {96factories.add(new PrintStreamProviderFactory(ps));97}98}99}100101// See how many factories we instantiated, and return an appropriate102// factory that encapsulates that.103if (factories.size() == 0) {104return new NullProviderFactory();105} else if (factories.size() == 1) {106return factories.toArray(new ProviderFactory[1])[0];107} else {108return new MultiplexProviderFactory(factories);109}110}111112private static PrintStream getPrintStreamFromSpec(final String spec) {113try {114// spec is in the form of <class>.<field>, where <class> is115// a fully specified class name, and <field> is a static member116// in that class. The <field> must be a 'PrintStream' or subtype117// in order to be used.118final int fieldpos = spec.lastIndexOf('.');119final Class<?> cls = Class.forName(spec.substring(0, fieldpos));120121Field f = AccessController.doPrivileged(new PrivilegedExceptionAction<Field>() {122public Field run() throws NoSuchFieldException {123return cls.getField(spec.substring(fieldpos + 1));124}125});126127return (PrintStream)f.get(null);128} catch (ClassNotFoundException e) {129throw new AssertionError(e);130} catch (IllegalAccessException e) {131throw new AssertionError(e);132} catch (PrivilegedActionException e) {133throw new AssertionError(e);134}135}136}137138139140