1 /* |
|
2 * Copyright (c) 2008, 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 sun.tracing; |
|
27 |
|
28 import java.lang.reflect.Method; |
|
29 import java.lang.reflect.InvocationTargetException; |
|
30 import java.util.HashMap; |
|
31 import java.util.HashSet; |
|
32 import java.util.Set; |
|
33 |
|
34 import com.sun.tracing.ProviderFactory; |
|
35 import com.sun.tracing.Provider; |
|
36 import com.sun.tracing.Probe; |
|
37 |
|
38 /** |
|
39 * Factory class to create tracing Providers. |
|
40 * |
|
41 * This factory creates a "multiplex provider", which is a provider that |
|
42 * encapsulates a list of providers and whose probes trigger a corresponding |
|
43 * trigger in each of the encapsulated providers' probes. |
|
44 * |
|
45 * This is used when there are multiple tracing frameworks activated at once. |
|
46 * A user-defined provider gets implementation for each of the activated |
|
47 * frameworks and this multiplex framework is what is ultimately passed |
|
48 * back to the user. All probe triggers are multiplexed to each |
|
49 * active framework. |
|
50 * |
|
51 * @since 1.7 |
|
52 */ |
|
53 public class MultiplexProviderFactory extends ProviderFactory { |
|
54 |
|
55 private Set<ProviderFactory> factories; |
|
56 |
|
57 public MultiplexProviderFactory(Set<ProviderFactory> factories) { |
|
58 this.factories = factories; |
|
59 } |
|
60 |
|
61 public <T extends Provider> T createProvider(Class<T> cls) { |
|
62 HashSet<Provider> providers = new HashSet<Provider>(); |
|
63 for (ProviderFactory factory : factories) { |
|
64 providers.add(factory.createProvider(cls)); |
|
65 } |
|
66 MultiplexProvider provider = new MultiplexProvider(cls, providers); |
|
67 provider.init(); |
|
68 return provider.newProxyInstance(); |
|
69 } |
|
70 } |
|
71 |
|
72 class MultiplexProvider extends ProviderSkeleton { |
|
73 |
|
74 private Set<Provider> providers; |
|
75 |
|
76 protected ProbeSkeleton createProbe(Method m) { |
|
77 return new MultiplexProbe(m, providers); |
|
78 } |
|
79 |
|
80 MultiplexProvider(Class<? extends Provider> type, Set<Provider> providers) { |
|
81 super(type); |
|
82 this.providers = providers; |
|
83 } |
|
84 |
|
85 public void dispose() { |
|
86 for (Provider p : providers) { |
|
87 p.dispose(); |
|
88 } |
|
89 super.dispose(); |
|
90 } |
|
91 } |
|
92 |
|
93 class MultiplexProbe extends ProbeSkeleton { |
|
94 |
|
95 private Set<Probe> probes; |
|
96 |
|
97 MultiplexProbe(Method m, Set<Provider> providers) { |
|
98 super(m.getParameterTypes()); |
|
99 probes = new HashSet<Probe>(); |
|
100 for (Provider p : providers) { |
|
101 Probe probe = p.getProbe(m); |
|
102 if (probe != null) { |
|
103 probes.add(probe); |
|
104 } |
|
105 } |
|
106 } |
|
107 |
|
108 public boolean isEnabled() { |
|
109 for (Probe p : probes) { |
|
110 if (p.isEnabled()) { |
|
111 return true; |
|
112 } |
|
113 } |
|
114 return false; |
|
115 } |
|
116 |
|
117 public void uncheckedTrigger(Object[] args) { |
|
118 for (Probe p : probes) { |
|
119 try { |
|
120 // try the fast path |
|
121 ProbeSkeleton ps = (ProbeSkeleton)p; |
|
122 ps.uncheckedTrigger(args); |
|
123 } catch (ClassCastException e) { |
|
124 // Probe.trigger takes an "Object ..." varargs parameter, |
|
125 // so we can't call it directly. |
|
126 try { |
|
127 Method m = Probe.class.getMethod( |
|
128 "trigger", Class.forName("[java.lang.Object")); |
|
129 m.invoke(p, args); |
|
130 } catch (Exception e1) { |
|
131 assert false; // This shouldn't happen |
|
132 } |
|
133 } |
|
134 } |
|
135 } |
|
136 } |
|
137 |
|