This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 138504
Collapse All | Expand All

(-)a/java.project/src/org/netbeans/api/java/project/runner/ProjectRunner.java (-36 / +28 lines)
Lines 40-47 Link Here
40
package org.netbeans.api.java.project.runner;
40
package org.netbeans.api.java.project.runner;
41
41
42
import java.io.IOException;
42
import java.io.IOException;
43
import java.util.Properties;
43
import java.util.Map;
44
import java.util.logging.Logger;
44
import java.util.logging.Logger;
45
import org.netbeans.api.java.classpath.ClassPath;
46
import org.netbeans.api.java.platform.JavaPlatform;
45
import org.netbeans.spi.java.project.runner.ProjectRunnerImplementation;
47
import org.netbeans.spi.java.project.runner.ProjectRunnerImplementation;
46
import org.openide.filesystems.FileObject;
48
import org.openide.filesystems.FileObject;
47
import org.openide.util.Lookup;
49
import org.openide.util.Lookup;
Lines 51-58 Link Here
51
 * <p>Class that allows to execute given file(s). API clients can check whether given
53
 * <p>Class that allows to execute given file(s). API clients can check whether given
52
 * command is support, by calling
54
 * command is support, by calling
53
 * {@link #isSupported(String)} and execute the command by calling
55
 * {@link #isSupported(String)} and execute the command by calling
54
 * {@link #execute(String, Properties, List)}. Please consult documentation of particular
56
 * {@link #execute(String, Map)}. Please consult documentation of particular
55
 * commands for the list of supported properties.</p>
57
 * commands for the list of supported properties.</p>
58
 *
59
 * The following "standard" properties are supported by most commands (unless stated otherwise):
60
 * <table>
61
 * <tr><td>execute.file      </td> <td>file to be executed (optional)</td> <td>{@link String} (absolute path) or {@link FileObject}</td></tr>
62
 * <tr><td>work.dir          </td> <td> working directory, project directory of execute.file will be used if missing </td> <td> {@link String} or {@link FileObject}</td></tr>
63
 * <tr><td>classname         </td> <td> class to execute, will be autodetected from execute.file if missing </td> <td> {@link String}</td></tr>
64
 * <tr><td>execute.classpath </td> <td> execute classpath, will be autodetected from execute.file if missing </td> <td> {@link ClassPath}</td></tr>
65
 * <tr><td>platform.java     </td> <td> java tool which should be used for execution, will be autodetected from platform property if missing </td> <td> {@link String} or {@link FileObject}</td></tr>
66
 * <tr><td>platform          </td> <td> java platform on which the class should be executed, default if missing, not needed if platform.java is set </td> <td> {@link JavaPlatform}</td></tr>
67
 * <tr><td>project.name      </td> <td> name of the current project, will be autodetected from execute.file if missing </td> <td> {@link String}</td></tr>
68
 * <tr><td>run.jvmargs       </td> <td> JVM arguments </td> <td> {@link Iterable} of {@link String}s</td></tr>
69
 * <tr><td>application.args  </td> <td> application arguments </td> <td> {@link Iterable} of {@link String}s</td></tr>
70
 * </table>
56
 *
71
 *
57
 * @see ProjectRunnerImplementation
72
 * @see ProjectRunnerImplementation
58
 * @since 1.19
73
 * @since 1.19
Lines 65-77 Link Here
65
     * "Test" run the given file. Classfiles produced by the Java infrastructure will be
80
     * "Test" run the given file. Classfiles produced by the Java infrastructure will be
66
     * executed.
81
     * executed.
67
     *
82
     *
68
     * Supported properties:
69
     * <ul>
70
     * <li><strong>run.jvmargs</strong> arguments that will be passed to the Java Virtual Machine</li>
71
     * <li><strong>application.args</strong> arguments that will be passed to the executed files</li>
72
     * <li><strong>run-sys-prop.&lt;property-name&gt;</strong> define system property <code>property-name</code></li>
73
     * </ul>
74
     *
75
     * @since 1.19
83
     * @since 1.19
76
     */
84
     */
77
    public static final String QUICK_RUN = "run";
85
    public static final String QUICK_RUN = "run";
Lines 79-91 Link Here
79
    /**
87
    /**
80
     * "Test" run the given file in the debugging mode. Classfiles produced by the Java infrastructure will be
88
     * "Test" run the given file in the debugging mode. Classfiles produced by the Java infrastructure will be
81
     * executed.
89
     * executed.
82
     *
83
     * Supported properties:
84
     * <ul>
85
     * <li><strong>run.jvmargs</strong> arguments that will be passed to the Java Virtual Machine</li>
86
     * <li><strong>application.args</strong> arguments that will be passed to the executed files</li>
87
     * <li><strong>run-sys-prop.&lt;property-name&gt;</strong> define system property <code>property-name</code></li>
88
     * </ul>
89
     *
90
     *
90
     * @since 1.19
91
     * @since 1.19
91
     */
92
     */
Lines 95-105 Link Here
95
     * "Test" run the given test. Classfiles produced by the Java infrastructure will be
96
     * "Test" run the given test. Classfiles produced by the Java infrastructure will be
96
     * executed.
97
     * executed.
97
     *
98
     *
98
     * Supported properties:
99
     * <strong>application.args</strong> property is not supported.
99
     * <ul>
100
     * <li><strong>run.jvmargs</strong> arguments that will be passed to the Java Virtual Machine</li>
101
     * <li><strong>test-sys-prop.&lt;property-name&gt;</strong> define system property <code>property-name</code></li>
102
     * </ul>
103
     *
100
     *
104
     * @since 1.19
101
     * @since 1.19
105
     */
102
     */
Lines 109-119 Link Here
109
     * "Test" run the given test in the debugging mode. Classfiles produced by the Java infrastructure will be
106
     * "Test" run the given test in the debugging mode. Classfiles produced by the Java infrastructure will be
110
     * executed.
107
     * executed.
111
     *
108
     *
112
     * Supported properties:
109
     * <strong>application.args</strong> property is not supported.
113
     * <ul>
114
     * <li><strong>run.jvmargs</strong> arguments that will be passed to the Java Virtual Machine</li>
115
     * <li><strong>test-sys-prop.&lt;property-name&gt;</strong> define system property <code>property-name</code></li>
116
     * </ul>
117
     *
110
     *
118
     * @since 1.19
111
     * @since 1.19
119
     */
112
     */
Lines 128-141 Link Here
128
     * @param toRun either the file that would be executed, or the project folder
121
     * @param toRun either the file that would be executed, or the project folder
129
     * @return true if and only if the given command is supported for given file/folder
122
     * @return true if and only if the given command is supported for given file/folder
130
     *
123
     *
131
     * @since 1.19
124
     * @since 1.22
132
     */
125
     */
133
    public static boolean isSupported(String command, FileObject toRun) {
126
    public static boolean isSupported(String command, Map<String, ?> properties) {
134
        Parameters.notNull("command", command);
127
        Parameters.notNull("command", command);
135
        Parameters.notNull("toRun", toRun);
128
        Parameters.notNull("properties", properties);
136
129
137
        for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) {
130
        for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) {
138
            if (i.isSupported(command, toRun)) {
131
            if (i.isSupported(command, properties)) {
139
                return true;
132
                return true;
140
            }
133
            }
141
        }
134
        }
Lines 152-167 Link Here
152
     * @param toRun file to run
145
     * @param toRun file to run
153
     * @throws java.io.IOException if execution fails
146
     * @throws java.io.IOException if execution fails
154
     *
147
     *
155
     * @since 1.19
148
     * @since 1.22
156
     */
149
     */
157
    public static void execute(String command, Properties props, FileObject toRun) throws IOException {
150
    public static void execute(String command, Map<String, ?> properties) throws IOException {
158
        Parameters.notNull("command", command);
151
        Parameters.notNull("command", command);
159
        Parameters.notNull("props", props);
152
        Parameters.notNull("properties", properties);
160
        Parameters.notNull("toRun", toRun);
161
        
153
        
162
        for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) {
154
        for (ProjectRunnerImplementation i : Lookup.getDefault().lookupAll(ProjectRunnerImplementation.class)) {
163
            if (i.isSupported(command, toRun)) {
155
            if (i.isSupported(command, properties)) {
164
                i.execute(command, props, toRun);
156
                i.execute(command, properties);
165
                break;
157
                break;
166
            }
158
            }
167
        }
159
        }
(-)a/java.project/src/org/netbeans/spi/java/project/runner/ProjectRunnerImplementation.java (-2 / +3 lines)
Lines 40-45 Link Here
40
package org.netbeans.spi.java.project.runner;
40
package org.netbeans.spi.java.project.runner;
41
41
42
import java.io.IOException;
42
import java.io.IOException;
43
import java.util.Map;
43
import java.util.Properties;
44
import java.util.Properties;
44
import org.netbeans.api.java.project.runner.ProjectRunner;
45
import org.netbeans.api.java.project.runner.ProjectRunner;
45
import org.openide.filesystems.FileObject;
46
import org.openide.filesystems.FileObject;
Lines 63-69 Link Here
63
     *
64
     *
64
     * @since 1.19
65
     * @since 1.19
65
     */
66
     */
66
    public boolean isSupported(String command, FileObject file);
67
    public boolean isSupported(String command, Map<String, ?> properties);
67
68
68
    /**
69
    /**
69
     * Implementation of {@link ProjectRunner#execute(java.lang.String, java.util.Properties, org.openide.filesystems.FileObject)}.
70
     * Implementation of {@link ProjectRunner#execute(java.lang.String, java.util.Properties, org.openide.filesystems.FileObject)}.
Lines 75-80 Link Here
75
     *
76
     *
76
     * @since 1.19
77
     * @since 1.19
77
     */
78
     */
78
    public void execute(String command, Properties props, FileObject toRun) throws IOException;
79
    public void execute(String command, Map<String, ?> properties) throws IOException;
79
    
80
    
80
}
81
}
(-)a/java.source.ant/src/org/netbeans/modules/java/source/ant/ProjectRunnerImpl.java (-44 / +136 lines)
Lines 46-51 Link Here
46
import java.net.URL;
46
import java.net.URL;
47
import java.net.URLConnection;
47
import java.net.URLConnection;
48
import java.text.MessageFormat;
48
import java.text.MessageFormat;
49
import java.util.Collections;
50
import java.util.HashMap;
51
import java.util.LinkedList;
52
import java.util.List;
53
import java.util.Map;
54
import java.util.Map.Entry;
49
import java.util.Properties;
55
import java.util.Properties;
50
import java.util.logging.Level;
56
import java.util.logging.Level;
51
import java.util.logging.Logger;
57
import java.util.logging.Logger;
Lines 56-62 Link Here
56
import org.apache.tools.ant.module.api.support.AntScriptUtils;
62
import org.apache.tools.ant.module.api.support.AntScriptUtils;
57
import org.netbeans.api.java.classpath.ClassPath;
63
import org.netbeans.api.java.classpath.ClassPath;
58
import org.netbeans.api.java.platform.JavaPlatform;
64
import org.netbeans.api.java.platform.JavaPlatform;
59
import org.netbeans.api.java.platform.JavaPlatformManager;
60
import org.netbeans.api.project.FileOwnerQuery;
65
import org.netbeans.api.project.FileOwnerQuery;
61
import org.netbeans.api.project.Project;
66
import org.netbeans.api.project.Project;
62
import org.netbeans.api.project.ProjectUtils;
67
import org.netbeans.api.project.ProjectUtils;
Lines 65-70 Link Here
65
import org.openide.filesystems.FileUtil;
70
import org.openide.filesystems.FileUtil;
66
import org.openide.util.ChangeSupport;
71
import org.openide.util.ChangeSupport;
67
import org.openide.util.Exceptions;
72
import org.openide.util.Exceptions;
73
import org.openide.util.Parameters;
68
import org.openide.util.WeakListeners;
74
import org.openide.util.WeakListeners;
69
import org.w3c.dom.Attr;
75
import org.w3c.dom.Attr;
70
import org.w3c.dom.DOMException;
76
import org.w3c.dom.DOMException;
Lines 84-141 Link Here
84
90
85
    private static final Logger LOG = Logger.getLogger(ProjectRunnerImpl.class.getName());
91
    private static final Logger LOG = Logger.getLogger(ProjectRunnerImpl.class.getName());
86
    
92
    
87
    public boolean isSupported(String command, FileObject file) {
93
    public boolean isSupported(String command, Map<String, ?> properties) {
88
        return locateScript(command) != null && checkRunSupported(file);
94
        return locateScript(command) != null;
89
    }
95
    }
90
96
91
    public void execute(String command, Properties props, FileObject toRun) throws IOException {
97
    public void execute(String command, Map<String, ?> properties) throws IOException {
92
        Project project = FileOwnerQuery.getOwner(toRun);
98
        String[] projectName = new String[1];
93
        ClassPath exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE);
99
        Properties antProps = computeProperties(properties, projectName);
94
        ClassPath boot = ClassPath.getClassPath(toRun, ClassPath.BOOT);
95
        ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE);
96
        JavaPlatform foundPlatform = JavaPlatformManager.getDefault().getDefaultPlatform();
97
98
        for (JavaPlatform p : JavaPlatformManager.getDefault().getInstalledPlatforms()) {
99
            if (boot.entries().equals(p.getBootstrapLibraries().entries())) {
100
                LOG.log(Level.FINE, "found platform={0}", p.getDisplayName());
101
                foundPlatform = p;
102
                break;
103
            }
104
        }
105
106
        LOG.log(Level.FINE, "using platform={0}", foundPlatform.getDisplayName());
107
        LOG.log(Level.FINE, "execute classpath={0}", exec);
108
109
        String cp = exec.toString(ClassPath.PathConversionMode.FAIL);
110
111
        Properties antProps = (Properties) props.clone();
112
113
        antProps.setProperty("classpath", cp);
114
        antProps.setProperty("classname", source.getResourceName(toRun, '.', false));
115
        if (antProps.get("work.dir") == null && project != null) {                //NOI18N
116
            FileObject projDirectory = project.getProjectDirectory();
117
            assert projDirectory != null;
118
            File file = FileUtil.toFile(projDirectory);
119
            if (file != null) {
120
                antProps.setProperty("work.dir", file.getAbsolutePath());       //NOI18N
121
            }
122
        }
123
        antProps.setProperty("platform.java", FileUtil.toFile(foundPlatform.findTool("java")).getAbsolutePath());
124
        
100
        
125
        FileObject script = buildScript(command);
101
        FileObject script = buildScript(command);
126
        String projectName = project != null ? ProjectUtils.getInformation(project).getDisplayName() : "";
102
        AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName[0]);
127
        AntProjectCookie apc = new FakeAntProjectCookie(AntScriptUtils.antProjectCookieFor(script), projectName);
128
        AntTargetExecutor.Env execenv = new AntTargetExecutor.Env();
103
        AntTargetExecutor.Env execenv = new AntTargetExecutor.Env();
129
        Properties p = execenv.getProperties();
104
        Properties props = execenv.getProperties();
130
        p.putAll(antProps);
105
        props.putAll(antProps);
131
        execenv.setProperties(p);
106
        execenv.setProperties(props);
132
107
133
        AntTargetExecutor.createTargetExecutor(execenv).execute(apc, null);
108
        AntTargetExecutor.createTargetExecutor(execenv).execute(apc, null);
134
    }
109
    }
135
110
136
    private static boolean checkRunSupported(FileObject file) {
111
    static Properties computeProperties(Map<String, ?> properties, String[] projectNameOut) {
137
        //XXX: finish
112
        properties = new HashMap<String, Object>(properties);
138
        return true;
113
        FileObject toRun = getValue(properties, "execute.file", FileObject.class);
114
        String workDir = getValue(properties, "work.dir", String.class);
115
        String className = getValue(properties, "classname", String.class);
116
        ClassPath exec = getValue(properties, "execute.classpath", ClassPath.class);
117
        String javaTool = getValue(properties, "platform.java", String.class);
118
        String projectName = getValue(properties, "project.name", String.class);
119
        Iterable<String> runJVMArgs = getMultiValue(properties, "run.jvmargs", String.class);
120
        Iterable<String> args = getMultiValue(properties, "application.args", String.class);
121
        if (workDir == null) {
122
            Parameters.notNull("toRun", toRun);
123
            Project project = FileOwnerQuery.getOwner(toRun);
124
            if (project != null) {
125
                //NOI18N
126
                FileObject projDirectory = project.getProjectDirectory();
127
                assert projDirectory != null;
128
                File file = FileUtil.toFile(projDirectory);
129
                if (file != null) {
130
                    workDir = file.getAbsolutePath(); //NOI18N
131
                }
132
            }
133
        }
134
        if (className == null) {
135
            Parameters.notNull("toRun", toRun);
136
            ClassPath source = ClassPath.getClassPath(toRun, ClassPath.SOURCE);
137
            className = source.getResourceName(toRun, '.', false);
138
        }
139
        if (exec == null) {
140
            Parameters.notNull("toRun", toRun);
141
            exec = ClassPath.getClassPath(toRun, ClassPath.EXECUTE);
142
        }
143
        if (javaTool == null) {
144
            JavaPlatform p = getValue(properties, "platform", JavaPlatform.class);
145
146
            if (p == null) {
147
                p = JavaPlatform.getDefault();
148
            }
149
            
150
            javaTool = FileUtil.toFile(p.findTool("java")).getAbsolutePath();
151
        }
152
        if (projectName == null) {
153
            Project project = getValue(properties, "project", Project.class);
154
            if (project != null) {
155
                projectName = ProjectUtils.getInformation(project).getDisplayName();
156
            }
157
            if (projectName == null && toRun != null) {
158
                project = FileOwnerQuery.getOwner(toRun);
159
                if (project != null) {
160
                    //NOI18N
161
                    projectName = ProjectUtils.getInformation(project).getDisplayName();
162
                }
163
            }
164
            if (projectName == null) {
165
                projectName = "";
166
            }
167
        }
168
169
        LOG.log(Level.FINE, "execute classpath={0}", exec);
170
        String cp = exec.toString(ClassPath.PathConversionMode.FAIL);
171
        Properties antProps = new Properties();
172
        antProps.setProperty("classpath", cp);
173
        antProps.setProperty("classname", className);
174
        antProps.setProperty("platform.java", javaTool);
175
        antProps.setProperty("work.dir", workDir);
176
        antProps.setProperty("run.jvmargs", toOneLine(runJVMArgs));
177
        antProps.setProperty("application.args", toOneLine(args));
178
179
        for (Entry<String, ?> e : properties.entrySet()) {
180
            if (e.getValue() instanceof String) {
181
                antProps.setProperty(e.getKey(), (String) e.getValue());
182
            }
183
        }
184
        
185
        projectNameOut[0] = projectName;
186
        
187
        return antProps;
188
    }
189
190
    private static <T> T getValue(Map<String, ?> properties, String name, Class<T> type) {
191
        Object v = properties.remove(name);
192
193
        if (v instanceof FileObject && type == String.class) {
194
            FileObject f = (FileObject) v;
195
            File file = FileUtil.toFile(f);
196
197
            v = file.getAbsolutePath();
198
        }
199
200
        return type.cast(v);
201
    }
202
203
    private static <T> Iterable<T> getMultiValue(Map<String, ?> properties, String name, Class<T> type) {
204
        Iterable v = (Iterable) properties.remove(name);
205
        List<T>  result = new LinkedList<T>();
206
207
        if (v == null) {
208
            return Collections.emptyList();
209
        }
210
        
211
        for (Object o : v) {
212
            result.add(type.cast(o));
213
        }
214
215
        return result;
216
    }
217
218
    private static String toOneLine(Iterable<String> it) {
219
        StringBuilder result = new StringBuilder();
220
        boolean first = true;
221
222
        for (String s : it) {
223
            if (!first) {
224
                result.append(' ');
225
            }
226
            first = false;
227
            result.append(s);
228
        }
229
230
        return result.toString();
139
    }
231
    }
140
232
141
    private static URL locateScript(String actionName) {
233
    private static URL locateScript(String actionName) {

Return to bug 138504