Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/tracing/ProviderFactory.java
38831 views
1
/*
2
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
10
*
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
16
*
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20
*
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
23
* questions.
24
*/
25
26
package com.sun.tracing;
27
28
import java.util.HashSet;
29
import java.io.PrintStream;
30
import java.lang.reflect.Field;
31
import java.security.AccessController;
32
import java.security.PrivilegedActionException;
33
import java.security.PrivilegedExceptionAction;
34
import sun.security.action.GetPropertyAction;
35
36
import sun.tracing.NullProviderFactory;
37
import sun.tracing.PrintStreamProviderFactory;
38
import sun.tracing.MultiplexProviderFactory;
39
import sun.tracing.dtrace.DTraceProviderFactory;
40
41
/**
42
* {@code ProviderFactory} is a factory class used to create instances of
43
* providers.
44
*
45
* To enable tracing in an application, this class must be used to create
46
* instances of the provider interfaces defined by users.
47
* The system-defined factory is obtained by using the
48
* {@code getDefaultFactory()} static method. The resulting instance can be
49
* used to create any number of providers.
50
*
51
* @since 1.7
52
*/
53
public abstract class ProviderFactory {
54
55
protected ProviderFactory() {}
56
57
/**
58
* Creates an implementation of a Provider interface.
59
*
60
* @param cls the provider interface to be defined.
61
* @return an implementation of {@code cls}, whose methods, when called,
62
* will trigger tracepoints in the application.
63
* @throws NullPointerException if cls is null
64
* @throws IllegalArgumentException if the class definition contains
65
* non-void methods
66
*/
67
public abstract <T extends Provider> T createProvider(Class<T> cls);
68
69
/**
70
* Returns an implementation of a {@code ProviderFactory} which
71
* creates instances of Providers.
72
*
73
* The created Provider instances will be linked to all appropriate
74
* and enabled system-defined tracing mechanisms in the JDK.
75
*
76
* @return a {@code ProviderFactory} that is used to create Providers.
77
*/
78
public static ProviderFactory getDefaultFactory() {
79
HashSet<ProviderFactory> factories = new HashSet<ProviderFactory>();
80
81
// Try to instantiate a DTraceProviderFactory
82
String prop = AccessController.doPrivileged(
83
new GetPropertyAction("com.sun.tracing.dtrace"));
84
85
if ( (prop == null || !prop.equals("disable")) &&
86
DTraceProviderFactory.isSupported() ) {
87
factories.add(new DTraceProviderFactory());
88
}
89
90
// Try to instantiate an output stream factory
91
prop = AccessController.doPrivileged(
92
new GetPropertyAction("sun.tracing.stream"));
93
if (prop != null) {
94
for (String spec : prop.split(",")) {
95
PrintStream ps = getPrintStreamFromSpec(spec);
96
if (ps != null) {
97
factories.add(new PrintStreamProviderFactory(ps));
98
}
99
}
100
}
101
102
// See how many factories we instantiated, and return an appropriate
103
// factory that encapsulates that.
104
if (factories.size() == 0) {
105
return new NullProviderFactory();
106
} else if (factories.size() == 1) {
107
return factories.toArray(new ProviderFactory[1])[0];
108
} else {
109
return new MultiplexProviderFactory(factories);
110
}
111
}
112
113
private static PrintStream getPrintStreamFromSpec(final String spec) {
114
try {
115
// spec is in the form of <class>.<field>, where <class> is
116
// a fully specified class name, and <field> is a static member
117
// in that class. The <field> must be a 'PrintStream' or subtype
118
// in order to be used.
119
final int fieldpos = spec.lastIndexOf('.');
120
final Class<?> cls = Class.forName(spec.substring(0, fieldpos));
121
122
Field f = AccessController.doPrivileged(new PrivilegedExceptionAction<Field>() {
123
public Field run() throws NoSuchFieldException {
124
return cls.getField(spec.substring(fieldpos + 1));
125
}
126
});
127
128
return (PrintStream)f.get(null);
129
} catch (ClassNotFoundException e) {
130
throw new AssertionError(e);
131
} catch (IllegalAccessException e) {
132
throw new AssertionError(e);
133
} catch (PrivilegedActionException e) {
134
throw new AssertionError(e);
135
}
136
}
137
}
138
139
140