diff --git a/java.project/src/org/netbeans/api/java/project/runner/ProjectRunner.java b/java.project/src/org/netbeans/api/java/project/runner/ProjectRunner.java --- a/java.project/src/org/netbeans/api/java/project/runner/ProjectRunner.java +++ b/java.project/src/org/netbeans/api/java/project/runner/ProjectRunner.java @@ -40,8 +40,10 @@ package org.netbeans.api.java.project.runner; import java.io.IOException; -import java.util.Properties; +import java.util.Map; import java.util.logging.Logger; +import org.netbeans.api.java.classpath.ClassPath; +import org.netbeans.api.java.platform.JavaPlatform; import org.netbeans.spi.java.project.runner.ProjectRunnerImplementation; import org.openide.filesystems.FileObject; import org.openide.util.Lookup; @@ -51,8 +53,21 @@ *

Class that allows to execute given file(s). API clients can check whether given * command is support, by calling * {@link #isSupported(String)} and execute the command by calling - * {@link #execute(String, Properties, List)}. Please consult documentation of particular + * {@link #execute(String, Map)}. Please consult documentation of particular * commands for the list of supported properties.

+ * + * The following "standard" properties are supported by most commands (unless stated otherwise): + * + * + * + * + * + * + * + * + * + * + *
execute.file file to be executed (optional) {@link String} (absolute path) or {@link FileObject}
work.dir working directory, project directory of execute.file will be used if missing {@link String} or {@link FileObject}
classname class to execute, will be autodetected from execute.file if missing {@link String}
execute.classpath execute classpath, will be autodetected from execute.file if missing {@link ClassPath}
platform.java java tool which should be used for execution, will be autodetected from platform property if missing {@link String} or {@link FileObject}
platform java platform on which the class should be executed, default if missing, not needed if platform.java is set {@link JavaPlatform}
project.name name of the current project, will be autodetected from execute.file if missing {@link String}
run.jvmargs JVM arguments {@link Iterable} of {@link String}s
application.args application arguments {@link Iterable} of {@link String}s
* * @see ProjectRunnerImplementation * @since 1.19 @@ -65,13 +80,6 @@ * "Test" run the given file. Classfiles produced by the Java infrastructure will be * executed. * - * Supported properties: - * - * * @since 1.19 */ public static final String QUICK_RUN = "run"; @@ -79,13 +87,6 @@ /** * "Test" run the given file in the debugging mode. Classfiles produced by the Java infrastructure will be * executed. - * - * Supported properties: - * * * @since 1.19 */ @@ -95,11 +96,7 @@ * "Test" run the given test. Classfiles produced by the Java infrastructure will be * executed. * - * Supported properties: - * + * application.args property is not supported. * * @since 1.19 */ @@ -109,11 +106,7 @@ * "Test" run the given test in the debugging mode. Classfiles produced by the Java infrastructure will be * executed. * - * Supported properties: - * + * application.args property is not supported. * * @since 1.19 */ @@ -128,14 +121,14 @@ * @param toRun either the file that would be executed, or the project folder * @return true if and only if the given command is supported for given file/folder * - * @since 1.19 + * @since 1.22 */ - public static boolean isSupported(String command, FileObject toRun) { + public static boolean isSupported(String command, Map properties) { Parameters.notNull("command", command); - Parameters.notNull("toRun", toRun); + Parameters.notNull("properties", properties); for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) { - if (i.isSupported(command, toRun)) { + if (i.isSupported(command, properties)) { return true; } } @@ -152,16 +145,15 @@ * @param toRun file to run * @throws java.io.IOException if execution fails * - * @since 1.19 + * @since 1.22 */ - public static void execute(String command, Properties props, FileObject toRun) throws IOException { + public static void execute(String command, Map properties) throws IOException { Parameters.notNull("command", command); - Parameters.notNull("props", props); - Parameters.notNull("toRun", toRun); + Parameters.notNull("properties", properties); for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) { - if (i.isSupported(command, toRun)) { - i.execute(command, props, toRun); + if (i.isSupported(command, properties)) { + i.execute(command, properties); break; } } diff --git a/java.project/src/org/netbeans/spi/java/project/runner/ProjectRunnerImplementation.java b/java.project/src/org/netbeans/spi/java/project/runner/ProjectRunnerImplementation.java --- a/java.project/src/org/netbeans/spi/java/project/runner/ProjectRunnerImplementation.java +++ b/java.project/src/org/netbeans/spi/java/project/runner/ProjectRunnerImplementation.java @@ -40,6 +40,7 @@ package org.netbeans.spi.java.project.runner; import java.io.IOException; +import java.util.Map; import java.util.Properties; import org.netbeans.api.java.project.runner.ProjectRunner; import org.openide.filesystems.FileObject; @@ -63,7 +64,7 @@ * * @since 1.19 */ - public boolean isSupported(String command, FileObject file); + public boolean isSupported(String command, Map properties); /** * Implementation of {@link ProjectRunner#execute(java.lang.String, java.util.Properties, org.openide.filesystems.FileObject)}. @@ -75,6 +76,6 @@ * * @since 1.19 */ - public void execute(String command, Properties props, FileObject toRun) throws IOException; + public void execute(String command, Map properties) throws IOException; } diff --git a/java.source.ant/src/org/netbeans/modules/java/source/ant/ProjectRunnerImpl.java b/java.source.ant/src/org/netbeans/modules/java/source/ant/ProjectRunnerImpl.java --- a/java.source.ant/src/org/netbeans/modules/java/source/ant/ProjectRunnerImpl.java +++ b/java.source.ant/src/org/netbeans/modules/java/source/ant/ProjectRunnerImpl.java @@ -46,6 +46,12 @@ import java.net.URL; import java.net.URLConnection; import java.text.MessageFormat; +import java.util.Collections; +import java.util.HashMap; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.logging.Level; import java.util.logging.Logger; @@ -56,7 +62,6 @@ import org.apache.tools.ant.module.api.support.AntScriptUtils; import org.netbeans.api.java.classpath.ClassPath; import org.netbeans.api.java.platform.JavaPlatform; -import org.netbeans.api.java.platform.JavaPlatformManager; import org.netbeans.api.project.FileOwnerQuery; import org.netbeans.api.project.Project; import org.netbeans.api.project.ProjectUtils; @@ -65,6 +70,7 @@ import org.openide.filesystems.FileUtil; import org.openide.util.ChangeSupport; import org.openide.util.Exceptions; +import org.openide.util.Parameters; import org.openide.util.WeakListeners; import org.w3c.dom.Attr; import org.w3c.dom.DOMException; @@ -84,58 +90,144 @@ private static final Logger LOG = Logger.getLogger(ProjectRunnerImpl.class.getName()); - public boolean isSupported(String command, FileObject file) { - return locateScript(command) != null && checkRunSupported(file); + public boolean isSupported(String command, Map properties) { + return locateScript(command) != null; } - public void execute(String command, Properties props, FileObject toRun) throws IOException { - Project project = FileOwnerQuery.getOwner(toRun); - ClassPath exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE); - ClassPath boot = ClassPath.getClassPath(toRun, ClassPath.BOOT); - ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE); - JavaPlatform foundPlatform = JavaPlatformManager.getDefault().getDefaultPlatform(); - - for (JavaPlatform p : JavaPlatformManager.getDefault().getInstalledPlatforms()) { - if (boot.entries().equals(p.getBootstrapLibraries().entries())) { - LOG.log(Level.FINE, "found platform={0}", p.getDisplayName()); - foundPlatform = p; - break; - } - } - - LOG.log(Level.FINE, "using platform={0}", foundPlatform.getDisplayName()); - LOG.log(Level.FINE, "execute classpath={0}", exec); - - String cp = exec.toString(ClassPath.PathConversionMode.FAIL); - - Properties antProps = (Properties) props.clone(); - - antProps.setProperty("classpath", cp); - antProps.setProperty("classname", source.getResourceName(toRun, '.', false)); - if (antProps.get("work.dir") == null && project != null) { //NOI18N - FileObject projDirectory = project.getProjectDirectory(); - assert projDirectory != null; - File file = FileUtil.toFile(projDirectory); - if (file != null) { - antProps.setProperty("work.dir", file.getAbsolutePath()); //NOI18N - } - } - antProps.setProperty("platform.java", FileUtil.toFile(foundPlatform.findTool("java")).getAbsolutePath()); + public void execute(String command, Map properties) throws IOException { + String[] projectName = new String[1]; + Properties antProps = computeProperties(properties, projectName); FileObject script = buildScript(command); - String projectName = project != null ? ProjectUtils.getInformation(project).getDisplayName() : ""; - AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName); + AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName[0]); AntTargetExecutor.Env execenv = new AntTargetExecutor.Env(); - Properties p = execenv.getProperties(); - p.putAll(antProps); - execenv.setProperties(p); + Properties props = execenv.getProperties(); + props.putAll(antProps); + execenv.setProperties(props); AntTargetExecutor.createTargetExecutor(execenv).execute(apc, null); } - private static boolean checkRunSupported(FileObject file) { - //XXX: finish - return true; + static Properties computeProperties(Map properties, String[] projectNameOut) { + properties = new HashMap(properties); + FileObject toRun = getValue(properties, "execute.file", FileObject.class); + String workDir = getValue(properties, "work.dir", String.class); + String className = getValue(properties, "classname", String.class); + ClassPath exec = getValue(properties, "execute.classpath", ClassPath.class); + String javaTool = getValue(properties, "platform.java", String.class); + String projectName = getValue(properties, "project.name", String.class); + Iterable runJVMArgs = getMultiValue(properties, "run.jvmargs", String.class); + Iterable args = getMultiValue(properties, "application.args", String.class); + if (workDir == null) { + Parameters.notNull("toRun", toRun); + Project project = FileOwnerQuery.getOwner(toRun); + if (project != null) { + //NOI18N + FileObject projDirectory = project.getProjectDirectory(); + assert projDirectory != null; + File file = FileUtil.toFile(projDirectory); + if (file != null) { + workDir = file.getAbsolutePath(); //NOI18N + } + } + } + if (className == null) { + Parameters.notNull("toRun", toRun); + ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE); + className = source.getResourceName(toRun, '.', false); + } + if (exec == null) { + Parameters.notNull("toRun", toRun); + exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE); + } + if (javaTool == null) { + JavaPlatform p = getValue(properties, "platform", JavaPlatform.class); + + if (p == null) { + p = JavaPlatform.getDefault(); + } + + javaTool = FileUtil.toFile(p.findTool("java")).getAbsolutePath(); + } + if (projectName == null) { + Project project = getValue(properties, "project", Project.class); + if (project != null) { + projectName = ProjectUtils.getInformation(project).getDisplayName(); + } + if (projectName == null && toRun != null) { + project = FileOwnerQuery.getOwner(toRun); + if (project != null) { + //NOI18N + projectName = ProjectUtils.getInformation(project).getDisplayName(); + } + } + if (projectName == null) { + projectName = ""; + } + } + + LOG.log(Level.FINE, "execute classpath={0}", exec); + String cp = exec.toString(ClassPath.PathConversionMode.FAIL); + Properties antProps = new Properties(); + antProps.setProperty("classpath", cp); + antProps.setProperty("classname", className); + antProps.setProperty("platform.java", javaTool); + antProps.setProperty("work.dir", workDir); + antProps.setProperty("run.jvmargs", toOneLine(runJVMArgs)); + antProps.setProperty("application.args", toOneLine(args)); + + for (Entry e : properties.entrySet()) { + if (e.getValue() instanceof String) { + antProps.setProperty(e.getKey(), (String) e.getValue()); + } + } + + projectNameOut[0] = projectName; + + return antProps; + } + + private static T getValue(Map properties, String name, Class type) { + Object v = properties.remove(name); + + if (v instanceof FileObject && type == String.class) { + FileObject f = (FileObject) v; + File file = FileUtil.toFile(f); + + v = file.getAbsolutePath(); + } + + return type.cast(v); + } + + private static Iterable getMultiValue(Map properties, String name, Class type) { + Iterable v = (Iterable) properties.remove(name); + List result = new LinkedList(); + + if (v == null) { + return Collections.emptyList(); + } + + for (Object o : v) { + result.add(type.cast(o)); + } + + return result; + } + + private static String toOneLine(Iterable it) { + StringBuilder result = new StringBuilder(); + boolean first = true; + + for (String s : it) { + if (!first) { + result.append(' '); + } + first = false; + result.append(s); + } + + return result.toString(); } private static URL locateScript(String actionName) {