001/*
002 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.nfi;
024
025import java.lang.reflect.*;
026
027import com.oracle.nfi.api.*;
028
029/**
030 * Class for obtaining the {@link NativeFunctionInterface} (if any) provided by the VM.
031 */
032public final class NativeFunctionInterfaceRuntime {
033    private static final NativeFunctionInterface INSTANCE;
034
035    /**
036     * Gets the {@link NativeFunctionInterface} (if any) provided by the VM.
037     *
038     * @return null if the VM does not provide a {@link NativeFunctionInterface}
039     */
040    public static NativeFunctionInterface getNativeFunctionInterface() {
041        return INSTANCE;
042    }
043
044    static {
045
046        NativeFunctionInterface instance = null;
047
048        NativeFunctionInterfaceAccess access = null;
049        Class<?> servicesClass = null;
050        try {
051            servicesClass = Class.forName("jdk.internal.jvmci.service.Services");
052        } catch (ClassNotFoundException e) {
053            try {
054                // Legacy support
055                servicesClass = Class.forName("com.oracle.jvmci.service.Services");
056            } catch (ClassNotFoundException e2) {
057                // JVMCI is unavailable
058            }
059        }
060        if (servicesClass != null) {
061            try {
062                Method m = servicesClass.getDeclaredMethod("loadSingle", Class.class, boolean.class);
063                access = (NativeFunctionInterfaceAccess) m.invoke(null, NativeFunctionInterfaceAccess.class, false);
064            } catch (Throwable e) {
065                // Fail fast for other errors
066                throw (InternalError) new InternalError().initCause(e);
067            }
068        }
069        // TODO: try standard ServiceLoader?
070        if (access != null) {
071            instance = access.getNativeFunctionInterface();
072        }
073        INSTANCE = instance;
074    }
075}