rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/LaunchSetup.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 01 Mar 2013 17:22:14 +0100
changeset 803 dccfe152dacd
parent 800 3661b82478e0
child 1537 897d7f70c5a5
permissions -rw-r--r--
Using 'vmtest.js' property with possibly value 'brwsr' and default value 'script'
jaroslav@356
     1
/**
jaroslav@356
     2
 * Back 2 Browser Bytecode Translator
jaroslav@356
     3
 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@356
     4
 *
jaroslav@356
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@356
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@356
     7
 * the Free Software Foundation, version 2 of the License.
jaroslav@356
     8
 *
jaroslav@356
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@356
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@356
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@356
    12
 * GNU General Public License for more details.
jaroslav@356
    13
 *
jaroslav@356
    14
 * You should have received a copy of the GNU General Public License
jaroslav@356
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@356
    16
 * If not, see http://opensource.org/licenses/GPL-2.0.
jaroslav@356
    17
 */
jaroslav@372
    18
package org.apidesign.bck2brwsr.vmtest.impl;
jaroslav@356
    19
jaroslav@370
    20
import java.io.IOException;
jaroslav@383
    21
import java.util.LinkedHashMap;
jaroslav@383
    22
import java.util.Map;
jaroslav@382
    23
import org.apidesign.bck2brwsr.launcher.Launcher;
jaroslav@372
    24
import org.testng.annotations.AfterGroups;
jaroslav@372
    25
import org.testng.annotations.BeforeGroups;
jaroslav@356
    26
jaroslav@356
    27
/**
jaroslav@356
    28
 *
jaroslav@356
    29
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@356
    30
 */
jaroslav@383
    31
public final class LaunchSetup {
jaroslav@384
    32
    static LaunchSetup INSTANCE = new LaunchSetup();
jaroslav@356
    33
    
jaroslav@681
    34
    private Launcher js;
jaroslav@384
    35
    private final Map<String,Launcher> brwsrs = new LinkedHashMap<>();
jaroslav@372
    36
    
jaroslav@384
    37
    private LaunchSetup() {
jaroslav@383
    38
    }
jaroslav@383
    39
    
jaroslav@681
    40
    public Launcher javaScript() {
jaroslav@681
    41
        return js(true);
jaroslav@681
    42
    } 
jaroslav@681
    43
    private synchronized  Launcher js(boolean create) {
jaroslav@681
    44
        if (js == null && create) {
jaroslav@803
    45
            final String p = System.getProperty("vmtest.js", "script"); // NOI18N
jaroslav@800
    46
            switch (p) {
jaroslav@800
    47
                case "brwsr": js = Launcher.createBrowser(null); break; // NOI18N
jaroslav@803
    48
                case "script": js = Launcher.createJavaScript(); break; // NOI18N
jaroslav@800
    49
                default: throw new IllegalArgumentException(
jaroslav@803
    50
                    "Unknown value of vmtest.js property: " + p
jaroslav@800
    51
                );
jaroslav@800
    52
            }
jaroslav@681
    53
        }
jaroslav@384
    54
        return js;
jaroslav@383
    55
    } 
jaroslav@383
    56
    
jaroslav@384
    57
    public synchronized Launcher brwsr(String cmd) {
jaroslav@384
    58
        Launcher s = brwsrs.get(cmd);
jaroslav@383
    59
        if (s == null) {
jaroslav@384
    60
            s = Launcher.createBrowser(cmd);
jaroslav@384
    61
            brwsrs.put(cmd, s);
jaroslav@383
    62
        }
jaroslav@383
    63
        return s;
jaroslav@356
    64
    }
jaroslav@356
    65
jaroslav@372
    66
    @BeforeGroups("run")
jaroslav@372
    67
    public void initializeLauncher() throws IOException {
jaroslav@681
    68
        if (js(false) != null) {
jaroslav@681
    69
            js(true).initialize();
jaroslav@681
    70
        }
jaroslav@384
    71
        for (Launcher launcher : brwsrs.values()) {
jaroslav@384
    72
            launcher.initialize();
jaroslav@384
    73
        }
jaroslav@356
    74
    }
jaroslav@356
    75
jaroslav@372
    76
    @AfterGroups("run")
jaroslav@372
    77
    public void shutDownLauncher() throws IOException, InterruptedException {
jaroslav@681
    78
        if (js(false) != null) {
jaroslav@681
    79
            js(true).shutdown();
jaroslav@681
    80
        }
jaroslav@384
    81
        for (Launcher launcher : brwsrs.values()) {
jaroslav@384
    82
            launcher.shutdown();
jaroslav@384
    83
        }
jaroslav@356
    84
    }
jaroslav@356
    85
}