1616
1717package io .appium .java_client .remote ;
1818
19+ import static com .google .common .base .Preconditions .checkNotNull ;
1920import static com .google .common .base .Throwables .throwIfUnchecked ;
2021import static java .util .Optional .ofNullable ;
2122
2223import com .google .common .base .Function ;
24+ import com .google .common .base .Supplier ;
2325import com .google .common .base .Throwables ;
2426
2527import org .openqa .selenium .WebDriverException ;
3638import java .net .ConnectException ;
3739import java .net .URL ;
3840import java .util .Map ;
41+ import java .util .Optional ;
3942
4043public class AppiumCommandExecutor extends HttpCommandExecutor {
4144
42- private final DriverService service ;
45+ private final Optional < DriverService > serviceOptional ;
4346
44- public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
45- URL addressOfRemoteServer , HttpClient .Factory httpClientFactory ) {
46- super (additionalCommands , addressOfRemoteServer , httpClientFactory );
47- service = null ;
47+ private AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands , DriverService service ,
48+ URL addressOfRemoteServer ,
49+ HttpClient .Factory httpClientFactory ) {
50+ super (additionalCommands ,
51+ ofNullable (service )
52+ .map ((Function <DriverService , URL >) DriverService ::getUrl )
53+ .orElse (addressOfRemoteServer ), httpClientFactory );
54+ serviceOptional = ofNullable (service );
4855 }
4956
5057 public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands , DriverService service ,
5158 HttpClient .Factory httpClientFactory ) {
52- super (additionalCommands , service .getUrl (), httpClientFactory );
53- this .service = service ;
59+ this (additionalCommands , checkNotNull (service ), null , httpClientFactory );
5460 }
61+ public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
62+ URL addressOfRemoteServer , HttpClient .Factory httpClientFactory ) {
63+ this (additionalCommands , null , checkNotNull (addressOfRemoteServer ), httpClientFactory );
64+ }
65+
5566
5667 public AppiumCommandExecutor (Map <String , CommandInfo > additionalCommands ,
5768 URL addressOfRemoteServer ) {
@@ -65,7 +76,7 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
6576
6677 @ Override public Response execute (Command command ) throws WebDriverException {
6778 if (DriverCommand .NEW_SESSION .equals (command .getName ())) {
68- ofNullable ( service ) .ifPresent (driverService -> {
79+ serviceOptional .ifPresent (driverService -> {
6980 try {
7081 driverService .start ();
7182 } catch (IOException e ) {
@@ -80,21 +91,20 @@ public AppiumCommandExecutor(Map<String, CommandInfo> additionalCommands,
8091 Throwable rootCause = Throwables .getRootCause (t );
8192 if (rootCause instanceof ConnectException
8293 && rootCause .getMessage ().contains ("Connection refused" )) {
83- throw ofNullable ( service ) .map ((Function <DriverService , WebDriverException >) service -> {
94+ throw serviceOptional .map ((Function <DriverService , WebDriverException >) service -> {
8495 if (service .isRunning ()) {
8596 return new WebDriverException ("The session is closed!" , rootCause );
8697 }
8798
8899 return new WebDriverException ("The appium server has accidentally died!" , rootCause );
89- }).orElse ( new WebDriverException (rootCause .getMessage (), rootCause ));
100+ }).orElseGet (( Supplier < WebDriverException >) () -> new WebDriverException (rootCause .getMessage (), rootCause ));
90101 }
91102 throwIfUnchecked (t );
92103 throw new WebDriverException (t );
93104 } finally {
94- if (DriverCommand .QUIT .equals (command .getName ()) && service != null ) {
95- service . stop ( );
105+ if (DriverCommand .QUIT .equals (command .getName ())) {
106+ serviceOptional . ifPresent ( DriverService :: stop );
96107 }
97108 }
98109 }
99-
100110}
0 commit comments