comparison graal/com.oracle.jvmci.service/src/com/oracle/jvmci/service/Services.java @ 21604:93f282187d90

moved JVMCI service API into separate com.oracle.jvmci.service module (JBS:GRAAL-53)
author Doug Simon <doug.simon@oracle.com>
date Fri, 29 May 2015 17:01:57 +0200
parents graal/com.oracle.jvmci.runtime/src/com/oracle/jvmci/runtime/Services.java@d563baeca9df
children 71b338926f2e
comparison
equal deleted inserted replaced
21603:4437f0da4b26 21604:93f282187d90
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 com.oracle.jvmci.service;
24
25 import static java.lang.String.*;
26
27 import java.util.*;
28
29 /**
30 * A mechanism on top of the standard {@link ServiceLoader} that enables a runtime to efficiently
31 * load services marked by {@link Service}. This may be important for services loaded early in the
32 * runtime initialization process.
33 */
34 public class Services {
35
36 private static final ClassValue<List<Service>> cache = new ClassValue<List<Service>>() {
37 @Override
38 protected List<Service> computeValue(Class<?> type) {
39 Service[] names = getServiceImpls(type);
40 if (names == null || names.length == 0) {
41 throw new InternalError(
42 format("No implementations for %s found (ensure %s extends %s and that in suite.py the \"annotationProcessors\" attribute for the project enclosing %s includes \"com.oracle.jvmci.service.processor\")",
43 type.getSimpleName(), type.getSimpleName(), Service.class, type.getSimpleName()));
44 }
45 return Arrays.asList(names);
46 }
47 };
48
49 /**
50 * Gets an {@link Iterable} of the implementations available for a given service.
51 */
52 @SuppressWarnings("unchecked")
53 public static <S> Iterable<S> load(Class<S> service) {
54 if (Service.class.isAssignableFrom(service)) {
55 try {
56 return (Iterable<S>) cache.get(service);
57 } catch (UnsatisfiedLinkError e) {
58 // Fall back to standard SerivceLoader
59 }
60 }
61 return ServiceLoader.load(service, Services.class.getClassLoader());
62 }
63
64 private static native <S> S[] getServiceImpls(Class<?> service);
65 }