Search in sources :

Example 1 with ExpressionEvaluator

use of com.oracle.bedrock.lang.ExpressionEvaluator in project oracle-bedrock by coherence-community.

the class LocalJavaApplicationLauncher method launch.

@Override
public A launch(Platform platform, MetaClass<A> metaClass, OptionsByType optionsByType) {
    // establish the diagnostics output table
    Table diagnosticsTable = new Table();
    diagnosticsTable.getOptions().add(Table.orderByColumn(0));
    if (platform != null) {
        diagnosticsTable.addRow("Target Platform", platform.getName());
    }
    // ----- establish the launch Options for the Application -----
    // add the platform options
    OptionsByType launchOptions = OptionsByType.of(platform.getOptions().asArray());
    // add the meta-class options
    metaClass.onLaunching(platform, launchOptions);
    // add the launch specific options
    launchOptions.addAll(optionsByType);
    // ----- establish an identity for the application -----
    // add a unique runtime id for expression support
    launchOptions.add(Variable.with("bedrock.runtime.id", UUID.randomUUID()));
    // ----- establish default Profiles for this Platform (and Builder) -----
    // java applications can automatically detect the following profiles
    launchOptions.get(RemoteDebugging.class);
    launchOptions.get(CommercialFeatures.class);
    // auto-detect and add externally defined profiles
    launchOptions.addAll(Profiles.getProfiles());
    for (Profile profile : launchOptions.getInstancesOf(Profile.class)) {
        profile.onLaunching(platform, metaClass, launchOptions);
    }
    // ----- give the MetaClass a last chance to manipulate any options -----
    metaClass.onLaunch(platform, launchOptions);
    // ----- determine the display name for the application -----
    DisplayName displayName = getDisplayName(launchOptions);
    // ----- establish the underlying ProcessBuilder -----
    // determine the Executable, defaulting to "java" if not defined
    Executable executable = launchOptions.getOrSetDefault(Executable.class, Executable.named("java"));
    // we'll use the native operating system process builder to create
    // and manage the local application process
    LocalProcessBuilder processBuilder = createProcessBuilder(executable.getName());
    // ----- establish the working directory -----
    // set the working directory for the Process
    WorkingDirectory workingDirectory = launchOptions.getOrSetDefault(WorkingDirectory.class, WorkingDirectory.currentDirectory());
    File directory = workingDirectory.resolve(platform, launchOptions);
    // set the resolved working directory back into the options
    launchOptions.add(WorkingDirectory.at(directory));
    if (directory != null) {
        processBuilder.directory(directory);
        diagnosticsTable.addRow("Working Directory", directory.toString());
    }
    // ----- establish environment variables -----
    EnvironmentVariables environmentVariables = launchOptions.get(EnvironmentVariables.class);
    switch(environmentVariables.getSource()) {
        case Custom:
            processBuilder.environment().clear();
            diagnosticsTable.addRow("Environment Variables", "(cleared)");
            break;
        case ThisApplication:
            Map<String, String> map = System.getenv();
            processBuilder.environment().clear();
            processBuilder.environment().putAll(map);
            diagnosticsTable.addRow("Environment Variables", "(based on parent process)");
            break;
        case TargetPlatform:
            diagnosticsTable.addRow("Environment Variables", "(based on platform defaults)");
            break;
    }
    // add the optionally defined environment variables
    Properties variables = environmentVariables.realize(platform, launchOptions.asArray());
    for (String variableName : variables.stringPropertyNames()) {
        processBuilder.environment().put(variableName, variables.getProperty(variableName));
    }
    if (variables.size() > 0) {
        Table table = Tabularize.tabularize(variables);
        diagnosticsTable.addRow("", table.toString());
    }
    // ----- establish java specific environment variables -----
    // by default we use the java home defined by the schema.  if that's not
    // defined we'll attempt to use the java home defined by this builder.
    JavaHome javaHome = launchOptions.get(JavaHome.class);
    // (using the system property)
    if (javaHome == null) {
        javaHome = JavaHome.at(System.getProperty("java.home", null));
    }
    if (javaHome != null) {
        processBuilder.environment().put("JAVA_HOME", StringHelper.doubleQuoteIfNecessary(javaHome.get()));
    }
    // ----- establish the command to start java -----
    String javaExecutable;
    if (javaHome == null) {
        // when we don't have a java home we just use the defined executable
        javaExecutable = StringHelper.doubleQuoteIfNecessary(executable.getName());
    } else {
        // when we have a java home, we prefix the executable name with the java.home/bin/
        String javaHomePath = javaHome.get();
        javaHomePath = javaHomePath.trim();
        diagnosticsTable.addRow("Java Home", javaHomePath);
        if (!javaHomePath.endsWith(File.separator)) {
            javaHomePath = javaHomePath + File.separator;
        }
        javaExecutable = StringHelper.doubleQuoteIfNecessary(javaHomePath + "bin" + File.separator + executable.getName());
    }
    processBuilder.command(javaExecutable);
    diagnosticsTable.addRow("Java Executable", javaExecutable);
    // ----- establish the class path -----
    JavaModules modular = launchOptions.get(JavaModules.class);
    boolean useModules = modular.isEnabled();
    // determine the predefined class path based on the launch options
    ClassPath classPath = launchOptions.get(ClassPath.class);
    try {
        // include the ClassPath of the Platform
        classPath = new ClassPath(classPath, ClassPath.ofClass(platform.getClass()));
        // include the ClassPath of each of the Options
        for (Option option : launchOptions.getInstancesOf(Option.class)) {
            classPath = new ClassPath(classPath, ClassPath.ofClass(option.getClass()));
        }
        // include the application runner (if defined)
        BedrockRunner bedrockRunner = optionsByType.get(BedrockRunner.class);
        if (bedrockRunner != null && bedrockRunner.isEnabled()) {
            // include the JavaApplicationLauncher
            classPath = new ClassPath(classPath, ClassPath.ofClass(bedrockRunner.getClassOfRunner()));
        }
        // add the updated ClassPath back into the launch options
        launchOptions.add(classPath);
    } catch (IOException e) {
        throw new RuntimeException("Failed to locate required classes for the class path", e);
    }
    processBuilder.command().add(useModules ? "--module-path" : "-cp");
    processBuilder.command().add(classPath.toString(launchOptions.asArray()));
    if (useModules) {
        Table modulePathTable = classPath.getTable();
        modulePathTable.getOptions().add(Cell.Separator.of(""));
        diagnosticsTable.addRow("Module Path", modulePathTable.toString());
        ClassPath path = modular.getClassPath();
        if (path != null && !path.isEmpty()) {
            processBuilder.command().add("-cp");
            processBuilder.command().add(path.toString(launchOptions.asArray()));
            Table classPathTable = path.getTable();
            classPathTable.getOptions().add(Cell.Separator.of(""));
            diagnosticsTable.addRow("Class Path", classPathTable.toString());
        }
    } else {
        Table classPathTable = classPath.getTable();
        classPathTable.getOptions().add(Cell.Separator.of(""));
        diagnosticsTable.addRow("Class Path", classPathTable.toString());
    }
    String applicationName = displayName.resolve(launchOptions);
    // ----- establish Bedrock specific system properties -----
    // configure a server channel to communicate with the native process
    final SocketBasedRemoteChannelServer server = new SocketBasedRemoteChannelServer(applicationName);
    // register the defined RemoteEventListeners with the server so that when the application starts
    // the listeners can immediately start receiving RemoteEvents
    RemoteEvents remoteEvents = launchOptions.get(RemoteEvents.class);
    remoteEvents.forEach((remoteEventListener, listenerOptions) -> server.addListener(remoteEventListener, listenerOptions));
    try {
        // NOTE: this listens on the wildcard address on an ephemeral port
        server.open();
    } catch (IOException e) {
        throw new RuntimeException("Failed to create remote execution server for the application", e);
    }
    // add Bedrock specific System Properties
    // NOTE: the Bedrock parent address for locally created applications is always "loopback" as
    // i). they are always running locally,
    // ii). they only need to connect locally, and
    // iii). the "loopback" interface should work regardless of the network we're on.
    InetAddress parentAddress;
    IPv4Preferred iPv4Preferred = launchOptions.get(IPv4Preferred.class);
    if (iPv4Preferred.isPreferred()) {
        // we have to provide the schema with an IPv4 address!
        try {
            parentAddress = InetAddress.getByName("127.0.0.1");
        } catch (UnknownHostException e) {
            // TODO: log that we couldn't determine the loopback address!
            parentAddress = InetAddress.getLoopbackAddress();
        }
    } else {
        // when the schema doesn't care, we can default to what this platform chooses
        parentAddress = InetAddress.getLoopbackAddress();
    }
    Table systemPropertiesTable = new Table();
    systemPropertiesTable.getOptions().add(Table.orderByColumn(0));
    systemPropertiesTable.getOptions().add(Cell.Separator.of(""));
    // establish the URI for this (parent) process
    String parentURI = "//" + parentAddress.getHostAddress() + ":" + server.getPort();
    systemPropertiesTable.addRow(Settings.PARENT_URI, parentURI.toString());
    processBuilder.command().add("-D" + Settings.PARENT_URI + "=" + parentURI);
    // add Orphanable configuration
    Orphanable orphanable = launchOptions.get(Orphanable.class);
    processBuilder.command().add("-D" + Settings.ORPHANABLE + "=" + orphanable.isOrphanable());
    systemPropertiesTable.addRow(Settings.ORPHANABLE, Boolean.toString(orphanable.isOrphanable()));
    // ----- establish the system properties for the java application -----
    // define the system properties based on those defined by the launch options
    Properties systemProperties = launchOptions.get(SystemProperties.class).resolve(platform, launchOptions);
    for (String propertyName : systemProperties.stringPropertyNames()) {
        String propertyValue = systemProperties.getProperty(propertyName);
        // (we don't want to have "parents" applications effect child applications
        if (propertyName.startsWith("bedrock.profile.") || !propertyName.startsWith("bedrock")) {
            processBuilder.command().add("-D" + propertyName + (propertyValue.isEmpty() ? "" : "=" + propertyValue));
            systemPropertiesTable.addRow(propertyName, propertyValue);
        }
    }
    diagnosticsTable.addRow("System Properties", systemPropertiesTable.toString());
    // ----- establish Java Virtual Machine options -----
    StringBuilder jvmOptions = new StringBuilder();
    for (JvmOption jvmOption : launchOptions.getInstancesOf(JvmOption.class)) {
        for (String value : jvmOption.resolve(launchOptions)) {
            processBuilder.command().add(value);
            if (jvmOptions.length() > 0) {
                jvmOptions.append(" ");
            }
            jvmOptions.append(value);
        }
    }
    if (jvmOptions.length() > 0) {
        diagnosticsTable.addRow("Java Options", jvmOptions.toString());
    }
    for (String propertyName : System.getProperties().stringPropertyNames()) {
        if (propertyName.startsWith("bedrock.runtime.inherit.")) {
            // resolve the property value
            String propertyValue = System.getProperty(propertyName);
            // evaluate any expressions in the property value
            ExpressionEvaluator evaluator = new ExpressionEvaluator(launchOptions.get(Variables.class));
            propertyValue = evaluator.evaluate(propertyValue, String.class);
            processBuilder.command().add(propertyValue);
        }
    }
    // ----- establish the application command line to execute -----
    // use the launcher to launch the application
    // (we don't start the application directly itself)
    String applicationLauncherClassName = JavaApplicationRunner.class.getName();
    if (useModules) {
        applicationLauncherClassName = "com.oracle.bedrock.runtime/" + applicationLauncherClassName;
        processBuilder.command().add("-m");
    }
    processBuilder.command().add(applicationLauncherClassName);
    // set the Java application class name we need to launch
    ClassName className = launchOptions.get(ClassName.class);
    if (className == null) {
        throw new IllegalArgumentException("Java Application ClassName not specified");
    }
    String applicationClassName = className.getName();
    processBuilder.command().add(applicationClassName);
    diagnosticsTable.addRow("Application Launcher", applicationLauncherClassName);
    diagnosticsTable.addRow("Application Class", applicationClassName);
    diagnosticsTable.addRow("Application", applicationName);
    // ----- included the java arguments to the command -----
    List<String> argList = launchOptions.get(Arguments.class).resolve(platform, launchOptions);
    // Set the actual arguments used back into the options
    launchOptions.add(Arguments.of(argList));
    String arguments = "";
    for (String argument : argList) {
        processBuilder.command().add(argument);
        arguments += argument + " ";
    }
    if (arguments.length() > 0) {
        diagnosticsTable.addRow("Application Arguments", arguments);
    }
    // should the standard error be redirected to the standard out?
    ErrorStreamRedirection redirection = launchOptions.get(ErrorStreamRedirection.class);
    processBuilder.redirectErrorStream(redirection.isEnabled());
    diagnosticsTable.addRow("Standard Error Device", redirection.isEnabled() ? "stdout" : "stderr");
    diagnosticsTable.addRow("Application Launch Time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
    // TODO: add all of the Profile options to the table
    // ----- start the local process -----
    boolean launchLogging = optionsByType.get(LaunchLogging.class).isEnabled();
    if (launchLogging && LOGGER.isLoggable(Level.INFO)) {
        LOGGER.log(Level.INFO, "Oracle Bedrock " + Bedrock.getVersion() + ": Starting Application...\n" + "------------------------------------------------------------------------\n" + diagnosticsTable.toString() + "\n" + "------------------------------------------------------------------------\n");
    }
    // create and start the native process
    Process process;
    try {
        process = processBuilder.start(launchOptions);
    } catch (IOException e) {
        throw new RuntimeException("Failed to build the underlying native process for the application", e);
    }
    // ----- create the local process and application -----
    // establish a LocalJavaProcess to represent the underlying Process
    LocalJavaApplicationProcess localJavaProcess = new LocalJavaApplicationProcess(process, server, systemProperties);
    // determine the application class that will represent the running application
    Class<? extends A> applicationClass = metaClass.getImplementationClass(platform, launchOptions);
    A application;
    try {
        // attempt to find a constructor(Platform, JavaApplicationProcess, Options)
        Constructor<? extends A> constructor = ReflectionHelper.getCompatibleConstructor(applicationClass, platform.getClass(), localJavaProcess.getClass(), launchOptions.getClass());
        // create the application
        application = constructor.newInstance(platform, localJavaProcess, launchOptions);
    } catch (Exception e) {
        throw new RuntimeException("Failed to instantiate the Application class specified by the MetaClass:" + metaClass, e);
    }
    if (JmxFeature.isSupportedBy(application)) {
        application.add(new JmxFeature());
    }
    // ----- wait for the application to start -----
    // ensure that the launcher process connects back to the server to
    // know that the application has started
    WaitToStart waitToStart = launchOptions.get(WaitToStart.class);
    if (waitToStart.isEnabled()) {
        Timeout timeout = launchOptions.get(Timeout.class);
        ensure(new AbstractDeferred<Boolean>() {

            @Override
            public Boolean get() throws TemporarilyUnavailableException, PermanentlyUnavailableException {
                if (!server.getRemoteChannels().iterator().hasNext()) {
                    throw new TemporarilyUnavailableException(this);
                } else {
                    return true;
                }
            }
        }, within(timeout));
    }
    // ----- notify the MetaClass that the application has been launched -----
    metaClass.onLaunched(platform, application, launchOptions);
    for (Profile profile : launchOptions.getInstancesOf(Profile.class)) {
        profile.onLaunched(platform, application, launchOptions);
    }
    // notify the ApplicationListener-based Options that the application has been launched
    for (ApplicationListener listener : launchOptions.getInstancesOf(ApplicationListener.class)) {
        listener.onLaunched(application);
    }
    return application;
}
Also used : JmxFeature(com.oracle.bedrock.runtime.java.features.JmxFeature) LocalApplicationProcess(com.oracle.bedrock.runtime.LocalApplicationProcess) ErrorStreamRedirection(com.oracle.bedrock.runtime.options.ErrorStreamRedirection) IPv4Preferred(com.oracle.bedrock.runtime.java.options.IPv4Preferred) Executable(com.oracle.bedrock.runtime.options.Executable) JvmOption(com.oracle.bedrock.runtime.java.options.JvmOption) WorkingDirectory(com.oracle.bedrock.runtime.options.WorkingDirectory) Timeout(com.oracle.bedrock.options.Timeout) Arguments(com.oracle.bedrock.runtime.options.Arguments) WaitToStart(com.oracle.bedrock.runtime.java.options.WaitToStart) PermanentlyUnavailableException(com.oracle.bedrock.deferred.PermanentlyUnavailableException) Option(com.oracle.bedrock.Option) JvmOption(com.oracle.bedrock.runtime.java.options.JvmOption) File(java.io.File) InetAddress(java.net.InetAddress) RemoteEvents(com.oracle.bedrock.runtime.java.options.RemoteEvents) TemporarilyUnavailableException(com.oracle.bedrock.deferred.TemporarilyUnavailableException) JavaModules(com.oracle.bedrock.runtime.java.options.JavaModules) SystemProperties(com.oracle.bedrock.runtime.java.options.SystemProperties) Properties(java.util.Properties) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator) Profile(com.oracle.bedrock.runtime.Profile) EnvironmentVariables(com.oracle.bedrock.runtime.options.EnvironmentVariables) Variables(com.oracle.bedrock.options.Variables) EnvironmentVariables(com.oracle.bedrock.runtime.options.EnvironmentVariables) LaunchLogging(com.oracle.bedrock.options.LaunchLogging) DisplayName(com.oracle.bedrock.runtime.options.DisplayName) ClassName(com.oracle.bedrock.runtime.java.options.ClassName) SocketBasedRemoteChannelServer(com.oracle.bedrock.runtime.concurrent.socket.SocketBasedRemoteChannelServer) Table(com.oracle.bedrock.table.Table) BedrockRunner(com.oracle.bedrock.runtime.java.options.BedrockRunner) UnknownHostException(java.net.UnknownHostException) JavaHome(com.oracle.bedrock.runtime.java.options.JavaHome) IOException(java.io.IOException) Date(java.util.Date) PermanentlyUnavailableException(com.oracle.bedrock.deferred.PermanentlyUnavailableException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TemporarilyUnavailableException(com.oracle.bedrock.deferred.TemporarilyUnavailableException) Orphanable(com.oracle.bedrock.runtime.options.Orphanable) SystemProperties(com.oracle.bedrock.runtime.java.options.SystemProperties) ApplicationListener(com.oracle.bedrock.runtime.ApplicationListener) OptionsByType(com.oracle.bedrock.OptionsByType) SimpleDateFormat(java.text.SimpleDateFormat)

Example 2 with ExpressionEvaluator

use of com.oracle.bedrock.lang.ExpressionEvaluator in project oracle-bedrock by coherence-community.

the class SystemProperties method resolve.

/**
 * Creates a standard java {@link Properties} instance containing the
 * {@link SystemProperty}s based on the specified {@link Platform} and {@link JavaApplication}
 * launch {@link Option}s.
 * <p>
 * If the value of a {@link SystemProperty} is defined as an {@link Iterator}, the next value from the
 * said {@link Iterator} will be used as a value for the returned property.  If the value of a
 * {@link SystemProperty} is defined as a {@link SystemProperty.ContextSensitiveValue}, the
 * {@link SystemProperty.ContextSensitiveValue#resolve(String, Platform, OptionsByType)} is called
 * to resolve the value.
 *
 * @param platform       the target {@link Platform} for the returned {@link Properties}
 * @param optionsByType  the {@link OptionsByType} for resolving the {@link Properties}
 *
 * @return a new {@link Properties} instance
 */
public Properties resolve(Platform platform, OptionsByType optionsByType) {
    ExpressionEvaluator evaluator = new ExpressionEvaluator(optionsByType);
    Properties properties = new Properties();
    for (SystemProperty property : this.properties.values()) {
        String name = property.getName();
        Object value = property.getValue();
        if (value != null) {
            if (value instanceof SystemProperty.ContextSensitiveValue) {
                SystemProperty.ContextSensitiveValue contextSensitiveValue = (SystemProperty.ContextSensitiveValue) value;
                value = contextSensitiveValue.resolve(name, platform, optionsByType);
            }
            if (value instanceof Iterator<?>) {
                Iterator<?> iterator = (Iterator<?>) value;
                if (iterator.hasNext()) {
                    value = iterator.next().toString();
                } else {
                    throw new IndexOutOfBoundsException(String.format("No more values available for the property [%s]", name));
                }
            }
            if (value != null) {
                String expression = value.toString().trim();
                if (!expression.isEmpty()) {
                    // resolve the use of expressions in the value
                    Object result = evaluator.evaluate(expression, Object.class);
                    expression = result == null ? "" : result.toString();
                }
                OptionsByType propertyOptions = property.getOptions();
                Iterable<SystemProperty.ResolveHandler> handlers = propertyOptions.getInstancesOf(SystemProperty.ResolveHandler.class);
                for (SystemProperty.ResolveHandler handler : handlers) {
                    try {
                        handler.onResolve(name, expression, optionsByType);
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
                // record the property
                properties.put(name, expression);
            }
        }
    }
    return properties;
}
Also used : Properties(java.util.Properties) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator) Iterator(java.util.Iterator) OptionsByType(com.oracle.bedrock.OptionsByType)

Example 3 with ExpressionEvaluator

use of com.oracle.bedrock.lang.ExpressionEvaluator in project oracle-bedrock by coherence-community.

the class EnvironmentVariables method realize.

/**
 * Creates a standard {@link Properties} instance containing the
 * {@link EnvironmentVariable}s based on the specified {@link Platform} and {@link Option}s.
 * <p>
 * If the value of a {@link EnvironmentVariable} is defined as an {@link Iterator}, the next value from the
 * said {@link Iterator} will be used as a value for the returned property.  If the value of a
 * {@link EnvironmentVariable} is defined as a {@link EnvironmentVariable.ContextSensitiveValue}, the
 * {@link EnvironmentVariable.ContextSensitiveValue#getValue(String, Platform, Option...)} is called
 * to resolve the value.
 *
 * @param platform  the target {@link Platform} for the returned {@link Properties}
 * @param options   the {@link Option}s for realizing the {@link Properties}
 *
 * @return a new {@link Properties} instance
 */
public Properties realize(Platform platform, Option... options) {
    OptionsByType optionsByType = OptionsByType.of(options);
    ExpressionEvaluator evaluator = new ExpressionEvaluator(optionsByType);
    Properties properties = new Properties();
    for (EnvironmentVariable variable : this.variables.values()) {
        String name = variable.getName();
        Object value = variable.getValue();
        if (value != null) {
            if (value instanceof EnvironmentVariable.ContextSensitiveValue) {
                EnvironmentVariable.ContextSensitiveValue contextSensitiveValue = (EnvironmentVariable.ContextSensitiveValue) value;
                value = contextSensitiveValue.getValue(name, platform, options);
            }
            if (value instanceof Iterator<?>) {
                Iterator<?> iterator = (Iterator<?>) value;
                if (iterator.hasNext()) {
                    value = iterator.next().toString();
                } else {
                    throw new IndexOutOfBoundsException(String.format("No more values available for the variable [%s]", name));
                }
            }
            if (value != null) {
                String expression = value.toString().trim();
                if (!expression.isEmpty()) {
                    // resolve the use of expressions in the values
                    Object result = evaluator.evaluate(expression, Object.class);
                    expression = result == null ? "" : result.toString();
                }
                // record the property
                properties.put(name, expression);
            }
        }
    }
    return properties;
}
Also used : Iterator(java.util.Iterator) Properties(java.util.Properties) OptionsByType(com.oracle.bedrock.OptionsByType) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator)

Example 4 with ExpressionEvaluator

use of com.oracle.bedrock.lang.ExpressionEvaluator in project oracle-bedrock by coherence-community.

the class WorkingDirectory method resolve.

/**
 * Resolve and obtain the {@link File} representing the working directory to use for an application.
 *
 * @param platform       the {@link Platform} that the application will run on
 * @param optionsByType  the {@link OptionsByType} to use
 *
 * @return  the {@link File} pointing to the working directory to use for an application
 */
public File resolve(Platform platform, OptionsByType optionsByType) {
    if (workingDirectory == null) {
        return null;
    }
    if (workingDirectory instanceof File) {
        return (File) workingDirectory;
    }
    DisplayName displayName = optionsByType.get(DisplayName.class);
    Discriminator discriminator = optionsByType.get(Discriminator.class);
    ExpressionEvaluator evaluator = new ExpressionEvaluator(optionsByType);
    evaluator.defineVariable("applicationName", displayName.resolve(optionsByType));
    evaluator.defineVariable("displayName", displayName.resolve(null));
    if (discriminator != null) {
        evaluator.defineVariable("discriminator", discriminator.getValue());
    }
    evaluator.defineVariable("platform", platform);
    Object directoryValue = workingDirectory;
    if (directoryValue instanceof WorkingDirectory.ContextSensitiveDirectoryName) {
        WorkingDirectory.ContextSensitiveDirectoryName contextSensitiveValue = (WorkingDirectory.ContextSensitiveDirectoryName) directoryValue;
        directoryValue = contextSensitiveValue.resolve(platform, optionsByType);
    }
    if (directoryValue instanceof Iterator<?>) {
        Iterator<?> iterator = (Iterator<?>) directoryValue;
        if (iterator.hasNext()) {
            directoryValue = iterator.next().toString();
        } else {
            throw new IndexOutOfBoundsException(String.format("No more values available for the working directory [%s]", workingDirectory));
        }
    }
    if (directoryValue instanceof File) {
        return (File) directoryValue;
    }
    String expression = directoryValue.toString().trim();
    Object result = evaluator.evaluate(expression, Object.class);
    return new File(String.valueOf(result));
}
Also used : Iterator(java.util.Iterator) File(java.io.File) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator)

Example 5 with ExpressionEvaluator

use of com.oracle.bedrock.lang.ExpressionEvaluator in project oracle-bedrock by coherence-community.

the class Arguments method resolve.

/**
 * Creates {@link List} of {@link String} values representing the command line arguments to pass to
 * an application
 * <p>
 * If the value of a {@link Argument} is defined as an {@link Iterator}, the next value from the
 * said {@link Iterator} will be used as a argument value. If the value of a {@link Argument} is
 * defined as a {@link Argument.ContextSensitiveArgument}, the
 * {@link Argument.ContextSensitiveArgument#resolve(Platform, OptionsByType)} method is called
 * to resolve the value.
 *
 * @param platform       the target {@link Platform} for the returned {@link List} of arguments
 * @param optionsByType  the {@link OptionsByType}s for realizing the {@link List} of arguments
 *
 * @return a new {@link List} of application command line arguments
 */
public List<String> resolve(Platform platform, OptionsByType optionsByType) {
    if (optionsByType == null) {
        optionsByType = OptionsByType.empty();
    }
    ExpressionEvaluator evaluator = new ExpressionEvaluator(optionsByType);
    List<String> argList = new ArrayList<>();
    for (Argument argument : this.arguments) {
        String name = argument.getName();
        char separator = argument.getSeparator();
        List<String> values = argument.resolve(platform, evaluator, optionsByType);
        if (name != null && !name.isEmpty()) {
            if (values != null && !values.isEmpty()) {
                if (separator == ' ') {
                    values.stream().filter((arg) -> arg != null && !arg.isEmpty()).forEach((arg) -> {
                        argList.add(name);
                        argList.add(arg);
                    });
                } else {
                    values.stream().filter((arg) -> arg != null && !arg.isEmpty()).forEach((arg) -> argList.add(name + separator + arg));
                }
            }
        } else if (values != null && !values.isEmpty()) {
            values.stream().filter((s) -> s != null && !s.isEmpty()).forEach(argList::add);
        }
    }
    return argList;
}
Also used : Option(com.oracle.bedrock.Option) Arrays(java.util.Arrays) List(java.util.List) Stream(java.util.stream.Stream) Iterator(java.util.Iterator) Platform(com.oracle.bedrock.runtime.Platform) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator) OptionsByType(com.oracle.bedrock.OptionsByType) Collections(java.util.Collections) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) ExpressionEvaluator(com.oracle.bedrock.lang.ExpressionEvaluator)

Aggregations

ExpressionEvaluator (com.oracle.bedrock.lang.ExpressionEvaluator)11 OptionsByType (com.oracle.bedrock.OptionsByType)4 File (java.io.File)4 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)4 Arguments (com.oracle.bedrock.runtime.options.Arguments)3 Properties (java.util.Properties)3 SleepingApplication (applications.SleepingApplication)2 Option (com.oracle.bedrock.Option)2 Variables (com.oracle.bedrock.options.Variables)2 LocalPlatform (com.oracle.bedrock.runtime.LocalPlatform)2 JavaApplication (com.oracle.bedrock.runtime.java.JavaApplication)2 ClassName (com.oracle.bedrock.runtime.java.options.ClassName)2 JvmOption (com.oracle.bedrock.runtime.java.options.JvmOption)2 EnvironmentVariables (com.oracle.bedrock.runtime.options.EnvironmentVariables)2 Orphanable (com.oracle.bedrock.runtime.options.Orphanable)2 Table (com.oracle.bedrock.table.Table)2 Test (org.junit.Test)2 PermanentlyUnavailableException (com.oracle.bedrock.deferred.PermanentlyUnavailableException)1 TemporarilyUnavailableException (com.oracle.bedrock.deferred.TemporarilyUnavailableException)1