KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > junit > RunStyleRemoteWeb


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.junit;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.junit.remote.BrowserManager;
21
22 import java.net.InetAddress JavaDoc;
23 import java.net.UnknownHostException JavaDoc;
24
25 /**
26  * Runs remotely in web mode. This feature is experimental and is not officially
27  * supported.
28  */

29 class RunStyleRemoteWeb extends RunStyle {
30
31   private static final int INITIAL_KEEPALIVE_MS = 5000;
32   private static final int PING_KEEPALIVE_MS = 2000;
33
34   // Larger values when debugging the unit test framework, so you
35
// don't get spurious timeouts.
36
// private static final int INITIAL_KEEPALIVE_MS = 500000;
37
// private static final int PING_KEEPALIVE_MS = 200000;
38

39   /**
40    * Remote browser managers.
41    */

42   private final BrowserManager[] browserManagers;
43
44   /**
45    * References to the remote browser processes.
46    */

47   private int[] remoteTokens;
48
49   /**
50    * The containing shell.
51    */

52   private final JUnitShell shell;
53
54   private boolean running = false;
55
56   /**
57    * @param shell the containing shell
58    */

59   public RunStyleRemoteWeb(JUnitShell shell, BrowserManager[] browserManagers) {
60     this.shell = shell;
61     this.browserManagers = browserManagers;
62     this.remoteTokens = new int[ browserManagers.length ];
63   }
64
65   public void maybeLaunchModule(String JavaDoc moduleName, boolean forceLaunch)
66       throws UnableToCompleteException {
67
68     if (forceLaunch || !running) {
69       shell.compileForWebMode(moduleName);
70       String JavaDoc localhost;
71
72       try {
73         localhost = InetAddress.getLocalHost().getHostAddress();
74       } catch (UnknownHostException JavaDoc e) {
75         throw new RuntimeException JavaDoc("Unable to determine my ip address", e);
76       }
77       String JavaDoc url = "http://" + localhost + ":" + shell.getPort() + "/"
78           + moduleName;
79
80       try {
81         for ( int i = 0; i < remoteTokens.length; ++i ) {
82           int remoteToken = remoteTokens[ i ];
83           BrowserManager mgr = browserManagers[ i ];
84           if ( remoteToken != 0 ) {
85             mgr.killBrowser(remoteToken);
86           }
87           remoteTokens[ i ] = mgr.launchNewBrowser(url, INITIAL_KEEPALIVE_MS);
88         }
89       } catch (Exception JavaDoc e) {
90         shell.getTopLogger().log(TreeLogger.ERROR,
91             "Error launching remote browser", e);
92         throw new UnableToCompleteException();
93       }
94
95       running = true;
96     }
97   }
98
99   public boolean wasInterrupted() {
100     for ( int i = 0; i < remoteTokens.length; ++i ) {
101       int remoteToken = remoteTokens[ i ];
102       BrowserManager mgr = browserManagers[ i ];
103       if (remoteToken > 0) {
104         try {
105           mgr.keepAlive(remoteToken, PING_KEEPALIVE_MS);
106         } catch (Exception JavaDoc e) {
107           // TODO(tobyr): We're failing on the first exception, rather than
108
// collecting them, but that's probably OK for now.
109
shell.getTopLogger().log(TreeLogger.WARN,
110               "Unexpected exception keeping remote browser alive", e);
111           return true;
112         }
113       }
114     }
115     return false;
116   }
117
118 }
Popular Tags