OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 177
Line 14... Line 14...
14
 package org.openconcerto.utils.prog;
14
 package org.openconcerto.utils.prog;
15
 
15
 
16
import org.openconcerto.utils.FileUtils;
16
import org.openconcerto.utils.FileUtils;
17
import org.openconcerto.utils.OSFamily;
17
import org.openconcerto.utils.OSFamily;
18
import org.openconcerto.utils.ProcessStreams;
18
import org.openconcerto.utils.ProcessStreams;
19
import org.openconcerto.utils.ProcessStreams.Action;
-
 
20
import org.openconcerto.utils.PropertiesUtils;
19
import org.openconcerto.utils.PropertiesUtils;
21
 
20
 
22
import java.io.File;
21
import java.io.File;
23
import java.io.IOException;
22
import java.io.IOException;
-
 
23
import java.lang.ProcessBuilder.Redirect;
24
import java.lang.management.ManagementFactory;
24
import java.lang.management.ManagementFactory;
25
import java.util.ArrayList;
25
import java.util.ArrayList;
26
import java.util.Arrays;
26
import java.util.Arrays;
27
import java.util.Collections;
27
import java.util.Collections;
28
import java.util.List;
28
import java.util.List;
Line 165... Line 165...
165
 
165
 
166
    public static final Process restart(final Class<?> mainClass, final String... args) throws IOException {
166
    public static final Process restart(final Class<?> mainClass, final String... args) throws IOException {
167
        return restart(mainClass, Arrays.asList(args));
167
        return restart(mainClass, Arrays.asList(args));
168
    }
168
    }
169
 
169
 
170
    public static final Process restart(final Action action, final Class<?> mainClass, final String... args) throws IOException {
170
    public static final Process restart(final Redirect action, final Class<?> mainClass, final String... args) throws IOException {
171
        return restart(action, mainClass, Arrays.asList(args));
171
        return restart(action, mainClass, Arrays.asList(args));
172
    }
172
    }
173
 
173
 
174
    /**
174
    /**
175
     * Restart the VM. If this VM was launched using a native application (e.g. .exe or .app) then
175
     * Restart the VM. If this VM was launched using a native application (e.g. .exe or .app) then
Line 180... Line 180...
180
     * @return the new process, <code>null</code> if the program wasn't started.
180
     * @return the new process, <code>null</code> if the program wasn't started.
181
     * @throws IOException if the VM couldn't be launched.
181
     * @throws IOException if the VM couldn't be launched.
182
     * @see #NO_RESTART
182
     * @see #NO_RESTART
183
     */
183
     */
184
    public static final Process restart(final Class<?> mainClass, final List<String> args) throws IOException {
184
    public static final Process restart(final Class<?> mainClass, final List<String> args) throws IOException {
185
        return restart(Action.CLOSE, mainClass, args);
185
        return restart(ProcessStreams.DISCARD, mainClass, args);
186
    }
186
    }
187
 
187
 
188
    public static final Process restart(final Action action, final Class<?> mainClass, final List<String> args) throws IOException {
188
    public static final Process restart(final Redirect action, final Class<?> mainClass, final List<String> args) throws IOException {
189
        if (Boolean.getBoolean(NO_RESTART))
189
        if (Boolean.getBoolean(NO_RESTART))
190
            return null;
190
            return null;
191
        final File wd = FileUtils.getWD();
191
        final File wd = FileUtils.getWD();
192
        final List<String> command = getNativeCommand(args);
192
        final List<String> command = getNativeCommand(args);
193
        if (command != null) {
193
        if (command != null) {
194
            return ProcessStreams.handle(new ProcessBuilder(command).directory(wd).start(), action);
194
            return new ProcessBuilder(command).directory(wd).redirectErrorStream(true).redirectOutput(action).start();
195
        } else {
195
        } else {
196
            try {
196
            try {
197
                mainClass.getMethod("main", String[].class);
197
                mainClass.getMethod("main", String[].class);
198
            } catch (NoSuchMethodException e) {
198
            } catch (NoSuchMethodException e) {
199
                throw new IllegalArgumentException(mainClass + " doesn't containt a main()", e);
199
                throw new IllegalArgumentException(mainClass + " doesn't containt a main()", e);
Line 208... Line 208...
208
                protected File getPropFile(String mainClass) {
208
                protected File getPropFile(String mainClass) {
209
                    return null;
209
                    return null;
210
                }
210
                }
211
 
211
 
212
                @Override
212
                @Override
213
                protected Action getStreamAction() {
213
                protected Redirect getStreamRedirect() {
214
                    return action;
214
                    return action;
215
                }
215
                }
216
            }.launch(mainClass.getName(), args);
216
            }.launch(mainClass.getName(), args);
217
        }
217
        }
218
    }
218
    }
Line 276... Line 276...
276
            res = props.getProperty(propName, res);
276
            res = props.getProperty(propName, res);
277
        }
277
        }
278
        return split(res);
278
        return split(res);
279
    }
279
    }
280
 
280
 
281
    protected final Process launch(final String mainClass) throws IOException {
281
    public final Process launch(final String mainClass) throws IOException {
282
        return this.launch(mainClass, Collections.<String> emptyList());
282
        return this.launch(mainClass, Collections.<String> emptyList());
283
    }
283
    }
284
 
284
 
285
    /**
285
    /**
286
     * Launch a new Java VM. This method will try to launch {@link #getJavaBinary() java} from the
286
     * Launch a new Java VM. This method will try to launch {@link #getJavaBinary() java} from the
Line 300... Line 300...
300
     * @param mainClass the main class.
300
     * @param mainClass the main class.
301
     * @param progParams the program arguments for <code>mainClass</code>.
301
     * @param progParams the program arguments for <code>mainClass</code>.
302
     * @return the new Process.
302
     * @return the new Process.
303
     * @throws IOException if the process couldn't be started.
303
     * @throws IOException if the process couldn't be started.
304
     */
304
     */
305
    protected final Process launch(final String mainClass, final List<String> progParams) throws IOException {
305
    public final Process launch(final String mainClass, final List<String> progParams) throws IOException {
306
        final boolean debug = Boolean.getBoolean("launcher.debug");
306
        final boolean debug = Boolean.getBoolean("launcher.debug");
307
        final String javaBinary = getJavaBinary();
307
        final String javaBinary = getJavaBinary();
308
        final File sameJava = new File(System.getProperty("java.home"), "bin/" + javaBinary);
308
        final File sameJava = new File(System.getProperty("java.home"), "bin/" + javaBinary);
309
        final String java = sameJava.canExecute() ? sameJava.getAbsolutePath() : javaBinary;
309
        final String java = sameJava.canExecute() ? sameJava.getAbsolutePath() : javaBinary;
310
        final File propFile = this.getPropFile(mainClass);
310
        final File propFile = this.getPropFile(mainClass);
Line 351... Line 351...
351
            System.err.println("Command line : " + procBuilder.command());
351
            System.err.println("Command line : " + procBuilder.command());
352
            System.err.println("Dir : " + procBuilder.directory());
352
            System.err.println("Dir : " + procBuilder.directory());
353
            System.err.println("Std out and err :");
353
            System.err.println("Std out and err :");
354
        }
354
        }
355
 
355
 
356
        final Process res = procBuilder.start();
-
 
357
        ProcessStreams.handle(res, debug ? Action.REDIRECT : this.getStreamAction());
356
        procBuilder.redirectErrorStream(true).redirectOutput(debug ? Redirect.INHERIT : this.getStreamRedirect());
358
 
-
 
359
        return res;
357
        return procBuilder.start();
360
    }
358
    }
361
 
359
 
362
    protected void modifyEnv(Map<String, String> environment) {
360
    protected void modifyEnv(Map<String, String> environment) {
363
    }
361
    }
364
 
362
 
365
    protected Action getStreamAction() {
363
    protected Redirect getStreamRedirect() {
366
        return Action.CLOSE;
364
        return ProcessStreams.DISCARD;
367
    }
365
    }
368
 
366
 
369
    protected boolean enableRemoteDebug(Properties props) {
367
    protected boolean enableRemoteDebug(Properties props) {
370
        final String prop = props.getProperty("remoteDebug");
368
        final String prop = props.getProperty("remoteDebug");
371
        return prop == null ? remoteDebugDefault() : Boolean.parseBoolean(prop);
369
        return prop == null ? remoteDebugDefault() : Boolean.parseBoolean(prop);