vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/Launcher.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 23 Dec 2012 17:02:34 +0100
branchlauncher
changeset 370 ed48023d1d85
parent 368 6b96afdb4200
child 371 bafc670aa10d
permissions -rw-r--r--
JavaScript and HTTP launchers separated
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.bck2brwsr.vmtest;
    19 
    20 import java.io.IOException;
    21 import org.apidesign.bck2brwsr.launcher.Bck2BrwsrLauncher;
    22 import org.apidesign.bck2brwsr.launcher.JSLauncher;
    23 import org.apidesign.bck2brwsr.launcher.MethodInvocation;
    24 
    25 /**
    26  *
    27  * @author Jaroslav Tulach <jtulach@netbeans.org>
    28  */
    29 final class Launcher {
    30     private final String sen;
    31     private Object launcher;
    32     
    33     Launcher() {
    34         this(null);
    35     }
    36     Launcher(String sen) {
    37         this.sen = sen;
    38     }
    39 
    40     synchronized Object clear() {
    41         Object l = launcher;
    42         launcher = null;
    43         return l;
    44     }
    45 
    46     synchronized MethodInvocation addMethod(Class<?> clazz, String name) throws IOException {
    47         if (launcher == null) {
    48             if (sen != null) {
    49                 JSLauncher js = new JSLauncher();
    50                 js.addClassLoader(clazz.getClassLoader());
    51                 js.initialize();
    52                 launcher = js;
    53             } else {
    54                 Bck2BrwsrLauncher l = new Bck2BrwsrLauncher();
    55                 l.setTimeout(180000);
    56                 launcher = l;
    57             }
    58         }
    59         if (launcher instanceof JSLauncher) {
    60             return ((JSLauncher)launcher).addMethod(clazz, name);
    61         } else {
    62             return ((Bck2BrwsrLauncher)launcher).addMethod(clazz, name);
    63         }
    64     }
    65 
    66     void exec() throws Exception {
    67         Object l = clear();
    68         if (l instanceof Bck2BrwsrLauncher) {
    69             ((Bck2BrwsrLauncher)l).execute();
    70         }
    71     }
    72     
    73 }