1+ package com .BS ;
2+
3+ import java .io .BufferedReader ;
4+ import java .io .InputStreamReader ;
5+ import java .util .ArrayList ;
6+ import java .util .HashMap ;
7+ import java .util .Iterator ;
8+ import java .util .List ;
9+ import java .util .Map ;
10+ import java .util .Set ;
11+
12+ class Local {
13+
14+ Process BrowserStackLocal = null ;
15+ List <String > command ;
16+ HashMap <String , String > parameters ;
17+
18+ void start (HashMap <String ,String > options ) throws Exception
19+ {
20+ LocalBinary lb = new LocalBinary ();
21+ command = new ArrayList <String >();
22+ command .add (lb .binary_path );
23+ command .add (options .get ("key" ));
24+
25+ makeCommand (options );
26+
27+ if (BrowserStackLocal == null )
28+ {
29+ ProcessBuilder processBuilder = new ProcessBuilder (command );
30+
31+ System .out .println ("Setting up Local Testing connection..." );
32+ BrowserStackLocal = processBuilder .start ();
33+ BufferedReader reader = new BufferedReader (new InputStreamReader (BrowserStackLocal .getInputStream ()));
34+ String string ;
35+ int j = 0 ;
36+ while ((string = reader .readLine ()) != null )
37+ {
38+ if (string .equalsIgnoreCase ("Press Ctrl-C to exit" ))
39+ {
40+ System .out .println ("Local Testing connection has been established." );
41+ break ;
42+ }
43+
44+ if (string .contains ("*** Error" ))
45+ {
46+ throw new BrowserStackLocalException (string );
47+ }
48+ if (j ++ > 20 )
49+ {
50+ throw new BrowserStackLocalException ("Could not start BrowserStackLocal" );
51+ }
52+ }
53+
54+ }
55+
56+ }
57+
58+ void stop ()
59+ {
60+ if (BrowserStackLocal != null )
61+ {
62+ BrowserStackLocal .destroy ();
63+ System .out .println ("Disconnected successfully" );
64+ }
65+ }
66+
67+ boolean isRunning ()
68+ {
69+ try
70+ {
71+ BrowserStackLocal .exitValue ();
72+ return false ;
73+ }
74+ catch (Exception e )
75+ {
76+ return true ;
77+ }
78+ }
79+
80+ void init ()
81+ {
82+ parameters = new HashMap <String , String >();
83+ parameters .put ("v" ,"-v" );
84+ parameters .put ("f" ,"-f" );
85+ parameters .put ("h" ,"-h" );
86+ parameters .put ("version" , "-version" );
87+ parameters .put ("force" , "-force" );
88+ parameters .put ("only" , "-only" );
89+ parameters .put ("forcelocal" , "-forcelocal" );
90+ parameters .put ("onlyAutomate" , "-onlyAutomate" );
91+ parameters .put ("proxyHost" , "-proxyHost" );
92+ parameters .put ("proxyPort" , "-proxyPort" );
93+ parameters .put ("proxyUser" , "-proxyUser" );
94+ parameters .put ("proxyPass" , "-proxyPass" );
95+ parameters .put ("hosts" , "-hosts" );
96+ parameters .put ("logfile" , "-logfile" );
97+ }
98+
99+ Local () throws Exception
100+ {
101+ init ();
102+ }
103+
104+ void makeCommand (HashMap <String ,String > options )
105+ {
106+ Set set = options .entrySet ();
107+ Iterator i = set .iterator ();
108+ while (i .hasNext ())
109+ {
110+ Map .Entry me = (Map .Entry )i .next ();
111+ String parameter = me .getKey ().toString ().trim ();
112+ if (parameters .get (parameter )!=null )
113+ if (me .getValue ()!=null )
114+ {
115+ command .add (parameters .get (parameter ));
116+ command .add ((String ) me .getValue ());
117+ }
118+ else
119+ command .add (parameters .get (parameter ));
120+ }
121+ }
122+ }
0 commit comments