Skip to content
Snippets Groups Projects
Commit 31376c0a authored by Mick Jordan's avatar Mick Jordan
Browse files

refactor RFFIFactory initialization

parent 80ba6abb
Branches
No related tags found
No related merge requests found
/*
* Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.runtime.ffi;
import com.oracle.truffle.r.runtime.*;
/**
* Selects a particular subclass of {@link RFFIFactory}. Specification is based on system property
* {@value #FACTORY_CLASS_PROPERTY}. Current default is a JNR-based implementation.
*/
public class Load_RFFIFactory {
private static final String FACTORY_CLASS_PROPERTY = "fastr.ffi.factory.class";
private static final String PACKAGE_PREFIX = "com.oracle.truffle.r.runtime.ffi.";
private static final String SUFFIX = "_RFFIFactory";
private static final String DEFAULT_FACTORY = "jnr";
private static final String DEFAULT_FACTORY_CLASS = mapSimpleName(DEFAULT_FACTORY);
private static String mapSimpleName(String simpleName) {
return PACKAGE_PREFIX + simpleName + "." + simpleName.toUpperCase() + SUFFIX;
}
public static RFFIFactory initialize() {
String prop = System.getProperty(FACTORY_CLASS_PROPERTY);
if (prop != null) {
if (!prop.contains(".")) {
// simple name
prop = mapSimpleName(prop);
}
} else {
prop = DEFAULT_FACTORY_CLASS;
}
try {
return (RFFIFactory) Class.forName(prop).newInstance();
} catch (Exception ex) {
Utils.fail("Failed to instantiate class: " + prop);
return null;
}
}
}
...@@ -25,44 +25,15 @@ package com.oracle.truffle.r.runtime.ffi; ...@@ -25,44 +25,15 @@ package com.oracle.truffle.r.runtime.ffi;
import com.oracle.truffle.r.runtime.*; import com.oracle.truffle.r.runtime.*;
/** /**
* Factory class for the different possible implementations of the {@link RFFI} interface. * Factory class for the different possible implementations of the {@link RFFI} interface. The
* Specification is based on system property {@value #FACTORY_CLASS_PROPERTY}. Current default is a * choice of factory is made by the R engine and set here by the call to {@link #setRFFIFactory}.
* JNR-based implementation.
*/ */
public abstract class RFFIFactory { public abstract class RFFIFactory {
private static final String FACTORY_CLASS_PROPERTY = "fastr.ffi.factory.class"; protected static RFFI theRFFI;
private static final String PACKAGE_PREFIX = "com.oracle.truffle.r.runtime.ffi.";
private static final String SUFFIX = "_RFFIFactory";
private static final String DEFAULT_FACTORY = "jnr";
private static final String DEFAULT_FACTORY_CLASS = mapSimpleName(DEFAULT_FACTORY);
static { public static void setRFFIFactory(RFFIFactory factory) {
String prop = System.getProperty(FACTORY_CLASS_PROPERTY); theRFFI = factory.createRFFI();
if (prop != null) {
if (!prop.contains(".")) {
// simple name
prop = mapSimpleName(prop);
}
} else {
prop = DEFAULT_FACTORY_CLASS;
}
try {
theFactory = (RFFIFactory) Class.forName(prop).newInstance();
} catch (Exception ex) {
Utils.fail("Failed to instantiate class: " + prop);
}
}
protected static RFFIFactory theFactory;
protected static final RFFI theRFFI = theFactory.createRFFI();
private static String mapSimpleName(String simpleName) {
return PACKAGE_PREFIX + simpleName + "." + simpleName.toUpperCase() + SUFFIX;
}
protected static RFFIFactory getFactory() {
return theFactory;
} }
public static RFFI getRFFI() { public static RFFI getRFFI() {
......
...@@ -30,6 +30,7 @@ import java.util.*; ...@@ -30,6 +30,7 @@ import java.util.*;
import com.oracle.truffle.api.frame.*; import com.oracle.truffle.api.frame.*;
import com.oracle.truffle.r.engine.*; import com.oracle.truffle.r.engine.*;
import com.oracle.truffle.r.runtime.*; import com.oracle.truffle.r.runtime.*;
import com.oracle.truffle.r.runtime.ffi.*;
import jline.console.*; import jline.console.*;
...@@ -37,7 +38,17 @@ import jline.console.*; ...@@ -37,7 +38,17 @@ import jline.console.*;
* Emulates the (Gnu)R command as precisely as possible. * Emulates the (Gnu)R command as precisely as possible.
*/ */
public class RCommand { public class RCommand {
// CheckStyle: stop system..print check
/**
* The choice of {@link RFFIFactory} is made statically so that it is bound into an AOT-compiled
* VM. The decision is node made directly in {@link RFFIFactory} to avoid some project
* dependencies that cause build problems.
*/
static {
RFFIFactory.setRFFIFactory(Load_RFFIFactory.initialize());
}
// CheckStyle: stop system..print check
public static void main(String[] args) { public static void main(String[] args) {
RCmdOptionsParser.Result result = RCmdOptionsParser.parseArguments(RCmdOptions.Client.R, args); RCmdOptionsParser.Result result = RCmdOptionsParser.parseArguments(RCmdOptions.Client.R, args);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment