All Downloads are FREE. Search and download functionalities are using the official Maven repository.

protoj.lang.command.JavaCommand Maven / Gradle / Ivy

The newest version!
/**
 * Copyright 2009 Ashley Williams
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you
 * may not use this file except in compliance with the License. You may
 * obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * permissions and limitations under the License.
 */
package protoj.lang.command;

import java.util.List;

import joptsimple.OptionSet;
import joptsimple.OptionSpec;

import org.apache.commons.beanutils.PropertyUtils;

import protoj.lang.Command;
import protoj.lang.CommandStore;
import protoj.lang.DispatchFeature;
import protoj.lang.InstructionChain;
import protoj.lang.StandardProject;
import protoj.util.ArgRunnable;
import protoj.util.JavaTask;

/**
 * Used to delegate to a new vm. Since ProtoJ already provides the ability to
 * launch a new vm then this is an ideal way of replacing an os specific command
 * in the startup script for a project.
 * 

* This class is tightly coupled to {@link DispatchFeature} since it requires * special treatment to terminate the dispatch loop early. It can be thought of * as the part of the DispatchFeature implementation that needs to exist in * command form. * * @author Ashley Williams * */ public final class JavaCommand { private final class Body implements Runnable { public void run() { OptionSet options = delegate.getOptions(); if (options.has(optOption)) { List javaOptions = options.valuesOf(optOption); startVm(javaOptions); } } } /** * Bootstraps to a specified java class. The options parameter contains name * value pairs to be applied to the underlying ant task by beans * introspection, so that the vm launcher can easily be configured from the * command line. *

* The bean being configured is the JavaTask instance. For example a string * with the content "javaTask.classname=com.acme.MyMain" is equivalent to * calling * JavaTask.getJavaTask().setClassname("javaTask.classname=com.acme.MyMain"). * * @param opts */ public void startVm(final List opts) { final StandardProject project = parent.getParent(); InstructionChain chain = project.getInstructionChain(); String[] args = chain.createArgsAndRemove(true); DispatchFeature dispatchFeature = project.getDispatchFeature(); dispatchFeature.startVm(null, args, null, new ArgRunnable() { public void run(JavaTask command) { project.getLogger().info("applying java options:"); for (String opt : opts) { String[] property = opt.split("="); String name = property[0]; String value = property[1]; project.getLogger().info( String.format("%s=%s", name, value)); PropertyUtils.setProperty(command, name, value); } } }); // ensure remaining commands don't get executed in this vm since they // have been bootstrapped to a new vm - we can assume we are in the // DispatchFeature dispatch loop since this command class is a helper of // that class and not useable anywhere else chain.breakVisit(); } /** * Provides the basic command functionality. */ private Command delegate; /** * The parent of this command. */ private final DispatchFeature parent; private OptionSpec optOption; public JavaCommand(DispatchFeature dispatchFeature) { this.parent = dispatchFeature; CommandStore store = dispatchFeature.getParent().getCommandStore(); delegate = store.addCommand("java", "16m", new Body()); delegate.initHelpResource("/protoj-common/language/english/java.txt"); delegate.initBootstrapCurrentVm(); optOption = delegate.getParser().accepts(getOptOption()) .withRequiredArg(); } public String getOptOption() { return "opt"; } public Command getDelegate() { return delegate; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy