vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/LaunchSetup.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 06 Feb 2013 13:30:31 +0100
changeset 681 93196a859fc5
parent 384 269d99fd6421
permissions -rw-r--r--
Lazy initialization of the JavaScript runner to prevent needless compilation of the VM
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@681
    45
            js = Launcher.createJavaScript();
jaroslav@681
    46
        }
jaroslav@384
    47
        return js;
jaroslav@383
    48
    } 
jaroslav@383
    49
    
jaroslav@384
    50
    public synchronized Launcher brwsr(String cmd) {
jaroslav@384
    51
        Launcher s = brwsrs.get(cmd);
jaroslav@383
    52
        if (s == null) {
jaroslav@384
    53
            s = Launcher.createBrowser(cmd);
jaroslav@384
    54
            brwsrs.put(cmd, s);
jaroslav@383
    55
        }
jaroslav@383
    56
        return s;
jaroslav@356
    57
    }
jaroslav@356
    58
jaroslav@372
    59
    @BeforeGroups("run")
jaroslav@372
    60
    public void initializeLauncher() throws IOException {
jaroslav@681
    61
        if (js(false) != null) {
jaroslav@681
    62
            js(true).initialize();
jaroslav@681
    63
        }
jaroslav@384
    64
        for (Launcher launcher : brwsrs.values()) {
jaroslav@384
    65
            launcher.initialize();
jaroslav@384
    66
        }
jaroslav@356
    67
    }
jaroslav@356
    68
jaroslav@372
    69
    @AfterGroups("run")
jaroslav@372
    70
    public void shutDownLauncher() throws IOException, InterruptedException {
jaroslav@681
    71
        if (js(false) != null) {
jaroslav@681
    72
            js(true).shutdown();
jaroslav@681
    73
        }
jaroslav@384
    74
        for (Launcher launcher : brwsrs.values()) {
jaroslav@384
    75
            launcher.shutdown();
jaroslav@384
    76
        }
jaroslav@356
    77
    }
jaroslav@356
    78
}