Mercurial > hg > graal-jvmci-8
comparison jvmci/jdk.vm.ci.services/src/jdk/vm/ci/services/Services.java @ 22761:f2206f5bb62e
removed @ServiceProvider mechanism (GRAAL-1380)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 30 Dec 2015 17:55:07 +0100 |
parents | jvmci/jdk.vm.ci.service/src/jdk/vm/ci/service/Services.java@1bbd4a7c274b |
children | 4b58c92e939b |
comparison
equal
deleted
inserted
replaced
22760:4cf1946f59fc | 22761:f2206f5bb62e |
---|---|
1 /* | |
2 * Copyright (c) 2014, 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. | |
8 * | |
9 * This code is distributed in the hope that it will be useful, but WITHOUT | |
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
12 * version 2 for more details (a copy is included in the LICENSE file that | |
13 * accompanied this code). | |
14 * | |
15 * You should have received a copy of the GNU General Public License version | |
16 * 2 along with this work; if not, write to the Free Software Foundation, | |
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. | |
18 * | |
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA | |
20 * or visit www.oracle.com if you need additional information or have any | |
21 * questions. | |
22 */ | |
23 package jdk.vm.ci.services; | |
24 | |
25 import java.util.Arrays; | |
26 import java.util.Collections; | |
27 import java.util.Formatter; | |
28 import java.util.List; | |
29 | |
30 import sun.reflect.Reflection; | |
31 | |
32 /** | |
33 * An mechanism for accessing service providers via JVMCI. These providers are loaded via a JVMCI | |
34 * class loader that is hidden from application code. Hence the {@link SecurityManager} checks in | |
35 * {@link #load(Class)} and {@link #loadSingle(Class, boolean)}. | |
36 */ | |
37 public final class Services { | |
38 | |
39 private Services() { | |
40 } | |
41 | |
42 private static final String SUPPRESS_PROPERTY_NAME = "jvmci.service.suppressNoClassDefFoundError"; | |
43 | |
44 /** | |
45 * Determines whether to suppress the {@link NoClassDefFoundError} raised if a service provider | |
46 * class specified in a {@code <jre>/jvmci/services/*} file is missing. | |
47 */ | |
48 private static final boolean SuppressNoClassDefFoundError = Boolean.getBoolean(SUPPRESS_PROPERTY_NAME); | |
49 | |
50 private static final ClassValue<List<?>> cache = new ClassValue<List<?>>() { | |
51 @Override | |
52 protected List<?> computeValue(Class<?> type) { | |
53 try { | |
54 return Arrays.asList(getServiceImpls(type)); | |
55 } catch (NoClassDefFoundError e) { | |
56 if (SuppressNoClassDefFoundError) { | |
57 return Collections.emptyList(); | |
58 } else { | |
59 NoClassDefFoundError newEx = new NoClassDefFoundError(e.getMessage() + " (suppress with -D" + SUPPRESS_PROPERTY_NAME + "=true)"); | |
60 if (e.getCause() != null) { | |
61 newEx.initCause(e.getCause()); | |
62 } | |
63 throw newEx; | |
64 } | |
65 } | |
66 } | |
67 }; | |
68 | |
69 /** | |
70 * Gets an {@link Iterable} of the JVMCI providers available for a given service. | |
71 * | |
72 * @throws SecurityException if a security manager is present and it denies | |
73 * <tt>{@link RuntimePermission}("jvmciServices")</tt> | |
74 */ | |
75 @SuppressWarnings("unchecked") | |
76 public static <S> Iterable<S> load(Class<S> service) { | |
77 SecurityManager sm = System.getSecurityManager(); | |
78 if (sm != null) { | |
79 sm.checkPermission(new RuntimePermission("jvmciServices")); | |
80 } | |
81 try { | |
82 return (Iterable<S>) cache.get(service); | |
83 } catch (UnsatisfiedLinkError e) { | |
84 return Collections.emptyList(); | |
85 } | |
86 } | |
87 | |
88 /** | |
89 * Gets the JVMCI provider for a given service for which at most one provider must be available. | |
90 * | |
91 * @param service the service whose provider is being requested | |
92 * @param required specifies if an {@link InternalError} should be thrown if no provider of | |
93 * {@code service} is available | |
94 * @throws SecurityException if a security manager is present and it denies | |
95 * <tt>{@link RuntimePermission}("jvmciServices")</tt> | |
96 */ | |
97 @SuppressWarnings({"unchecked"}) | |
98 public static <S> S loadSingle(Class<S> service, boolean required) { | |
99 SecurityManager sm = System.getSecurityManager(); | |
100 if (sm != null) { | |
101 sm.checkPermission(new RuntimePermission("jvmciServices")); | |
102 } | |
103 Iterable<S> providers; | |
104 try { | |
105 providers = (Iterable<S>) cache.get(service); | |
106 } catch (UnsatisfiedLinkError e) { | |
107 providers = Collections.emptyList(); | |
108 } | |
109 | |
110 S singleProvider = null; | |
111 for (S provider : providers) { | |
112 if (singleProvider != null) { | |
113 throw new InternalError(String.format("Multiple %s providers found: %s, %s", service.getName(), singleProvider.getClass().getName(), provider.getClass().getName())); | |
114 } | |
115 singleProvider = provider; | |
116 } | |
117 if (singleProvider == null && required) { | |
118 String javaHome = System.getProperty("java.home"); | |
119 String vmName = System.getProperty("java.vm.name"); | |
120 Formatter errorMessage = new Formatter(); | |
121 errorMessage.format("The VM does not expose required service %s.%n", service.getName()); | |
122 errorMessage.format("Currently used Java home directory is %s.%n", javaHome); | |
123 errorMessage.format("Currently used VM configuration is: %s", vmName); | |
124 throw new UnsupportedOperationException(errorMessage.toString()); | |
125 } | |
126 return singleProvider; | |
127 } | |
128 | |
129 static { | |
130 Reflection.registerMethodsToFilter(Services.class, "getServiceImpls"); | |
131 Reflection.registerFieldsToFilter(Services.class, "cache"); | |
132 } | |
133 | |
134 private static native <S> S[] getServiceImpls(Class<?> service); | |
135 } |