Files
mercury/java/runtime/MercuryOptions.java
Julien Fischer 89293d8faa Add MercuryFatalError exception to the Java runtime.
Add MercuryFatalError, a new Java exception that is intended to be used for a
similar purpose to the C runtime's MR_fatal_error() function.

Throw a MercuryFatalError exception instead of calling System.exit() in a spot.
Calling exit() is fine for executables, but for code that is deployed in an
application server calling exit() may shut down the entire server.

java/runtime/MercuryFatalError.java:
    Add the new exception.

java/runtime/MercuryOptions.java:
    Do not call System.exit() when we encounter an unrecognized
    option in MERCURY_OPTIONS, throw a MercuryFatalError exception
    instead.

    Refactor the process() method in order to avoid indentation.

    Catch NumberFormatExceptions thrown when attempting to convert
    integer option values; rethrow them as MercuryFatalErrors.
    (XXX the C version of the runtime just ignores this error.)

java/runtime/JavaInternal.java:
    Catch and report MercuryFatalErrors in the runMain() method.

java/runtime/MercuryWorkerThread.java:
    Add an XXX about a call to System.exit() here.
2024-01-19 21:57:20 +11:00

64 lines
1.7 KiB
Java

// vim: ts=4 sw=4 expandtab ft=java
//
// Copyright (C) 2014, 2016, 2018, 2024 The Mercury Team.
// This file is distributed under the terms specified in COPYING.LIB.
//
package jmercury.runtime;
import java.util.List;
/**
* Process the MERCURY_OPTIONS environment variable.
*/
public class MercuryOptions {
private int num_processors;
/**
* Create a new MercuryOptions object.
* This constructor is package-private (no access declaration).
*/
MercuryOptions()
{
// Zero means autodetect.
num_processors = 0;
}
public void process()
{
String options = System.getenv("MERCURY_OPTIONS");
if (options == null) {
return;
}
Getopt getopt = new Getopt(options);
for (Getopt.Option option : getopt) {
if (option.optionIs("P")) {
try {
num_processors = option.getValueInt();
} catch (java.lang.NumberFormatException e) {
throw new MercuryFatalError(
"the value of the -P option must be " +
"a non-negative integer", e);
}
} else {
throw new MercuryFatalError("Unrecognized option: " + option);
}
}
List<String> args = getopt.getArguments();
if (args.size() > 0) {
throw new MercuryFatalError(
"Error parsing MERCURY_OPTIONS environment variable,"
+ " unexpected: " + args.get(0));
}
}
/**
* Get the number of processors in the machine.
*/
public int getNumProcessors() {
return num_processors;
}
}