@@ -18,19 +18,19 @@ public class BrowserStackLocal {
1818 private static Logger logger ;
1919 private static final int MAX_CONNECT_WAIT = 30000 ; // 30s x 2 = 60s
2020 private static final int MAX_CONNECT_ATTEMPTS = 2 ;
21-
21+
2222 private final File binaryFile ;
2323 private final String argumentString ;
24-
24+
2525 private Process process ;
2626 private Thread processThread ;
27-
27+
2828 private StringBuffer output ;
2929 private String lastError ;
3030 private TunnelState tunnelState ;
31-
31+
3232 private BrowserStackLocalListener listener ;
33-
33+
3434 private static final Object monitor = new Object ();
3535 private static final Map <Pattern , TunnelState > stateMatchers = new HashMap <Pattern , TunnelState >();
3636
@@ -44,13 +44,13 @@ protected BrowserStackLocal(File binaryFile, String argumentString) {
4444 throw new IllegalArgumentException ("Invalid arguments" );
4545 }
4646
47- this .binaryFile = binaryFile ;
48- this .argumentString = argumentString ;
49- this .output = new StringBuffer ();
50- this .tunnelState = TunnelState .IDLE ;
51- this .logger = BrowserStackTunnel .logger ;
47+ this .binaryFile = binaryFile ;
48+ this .argumentString = argumentString ;
49+ this .output = new StringBuffer ();
50+ this .tunnelState = TunnelState .IDLE ;
51+ this .logger = BrowserStackTunnel .logger ;
5252
53- Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
53+ Runtime .getRuntime ().addShutdownHook (new Thread (new Runnable () {
5454 @ Override
5555 public void run () {
5656 BrowserStackLocal .this .kill ();
@@ -66,31 +66,32 @@ protected void run() {
6666 processThread = new Thread (new Runnable () {
6767 @ Override
6868 public void run () {
69- notifyTunnelStateChanged (TunnelState .CONNECTING );
70-
71- try {
72- logger .fine ("Arguments -- " + argumentString );
73- process = new ProcessBuilder ((binaryFile .getAbsolutePath () + " " + argumentString ).split (" " )).start ();
74- BufferedReader br = new BufferedReader (new InputStreamReader (process .getInputStream ()));
75-
76- String line ;
77- while ((line = br .readLine ()) != null ) {
78- output .append (line ).append ("\n " );
79-
80- if (processOutput (output .toString ())) {
81- logger .fine (output .toString ());
82- output .setLength (0 );
83- }
84- }
85- } catch (IOException e ) {
86- if (listener != null ) {
87- listener .onError (e .getMessage ());
88- }
89- } finally {
90- logger .fine (output .toString ());
91- output .setLength (0 );
92- notifyTunnelStateChanged (TunnelState .DISCONNECTED );
93- }
69+ notifyTunnelStateChanged (TunnelState .CONNECTING );
70+
71+ try {
72+ logger .fine ("Arguments -- " + argumentString );
73+ process = new ProcessBuilder ((binaryFile .getAbsolutePath () + " " + argumentString )
74+ .split (" " )).start ();
75+ BufferedReader br = new BufferedReader (new InputStreamReader (process .getInputStream ()));
76+
77+ String line ;
78+ while ((line = br .readLine ()) != null ) {
79+ output .append (line ).append ("\n " );
80+
81+ if (processOutput (output .toString ())) {
82+ logger .fine (output .toString ());
83+ output .setLength (0 );
84+ }
85+ }
86+ } catch (IOException e ) {
87+ if (listener != null ) {
88+ listener .onError (e .getMessage ());
89+ }
90+ } finally {
91+ logger .fine (output .toString ());
92+ output .setLength (0 );
93+ notifyTunnelStateChanged (TunnelState .DISCONNECTED );
94+ }
9495 }
9596 });
9697
@@ -104,17 +105,17 @@ protected void run(BrowserStackLocalListener listener) {
104105
105106 protected void runSync (BrowserStackLocalListener listener ) {
106107 setListener (listener );
107-
108+
108109 if (process != null ) {
109110 kill ();
110111 }
111-
112+
112113 notifyTunnelStateChanged (TunnelState .CONNECTING );
113114 run ();
114-
115+
115116 int connAttempts = 0 ;
116117 boolean connFailed = false ;
117-
118+
118119 synchronized (monitor ) {
119120 while (tunnelState == TunnelState .CONNECTING ) {
120121 logger .info ("Waiting: " + connAttempts );
@@ -123,14 +124,14 @@ protected void runSync(BrowserStackLocalListener listener) {
123124 } catch (InterruptedException e ) {
124125 logger .info ("Exc: " + e .getMessage () + " " + isConnected ());
125126 }
126-
127+
127128 if (MAX_CONNECT_ATTEMPTS > 0 && ++connAttempts >= MAX_CONNECT_ATTEMPTS ) {
128129 connFailed = true ;
129130 break ;
130131 }
131132 }
132133 }
133-
134+
134135 if (connFailed ) {
135136 killWithError ("Failed to connect to BrowserStack" );
136137 }
@@ -141,12 +142,12 @@ protected void kill() {
141142 process .destroy ();
142143 process = null ;
143144 }
144-
145+
145146 if (processThread != null && processThread .isAlive ()) {
146147 processThread .interrupt ();
147148 processThread = null ;
148149 }
149-
150+
150151 logger .fine (output .toString ());
151152 output .setLength (0 );
152153 tunnelState = TunnelState .DISCONNECTED ;
@@ -177,24 +178,24 @@ private void killWithError(String message) {
177178 private boolean processOutput (final String output ) {
178179 if (output != null && !output .trim ().isEmpty ()) {
179180 String error ;
180-
181+
181182 for (Map .Entry <Pattern , TunnelState > entry : stateMatchers .entrySet ()) {
182183 Matcher m = entry .getKey ().matcher (output );
183-
184+
184185 if (m .find ()) {
185186 if (entry .getValue () == TunnelState .ERROR ) {
186187 error = (m .groupCount () > 0 ) ? m .group (1 ) : output ;
187188 } else {
188189 error = null ;
189190 }
190-
191+
191192 setError (error );
192193 notifyTunnelStateChanged (entry .getValue ());
193194 return true ;
194195 }
195196 }
196197 }
197-
198+
198199 return false ;
199200 }
200201
@@ -203,20 +204,21 @@ private void notifyTunnelStateChanged(TunnelState state) {
203204 if (listener != null ) {
204205 listener .onTunnelStateChange (state );
205206 }
206-
207+
207208 synchronized (monitor ) {
208209 monitor .notifyAll ();
209210 }
210211 }
211-
212+
212213 tunnelState = state ;
213214 }
214215
215216 private void setError (String message ) {
216217 lastError = message ;
217-
218+
218219 if (listener != null ) {
219220 listener .lastError = lastError ;
220221 }
221222 }
223+
222224}
0 commit comments