...42import java.util.logging.Level;43import javax.management.MalformedObjectNameException;44import javax.management.ObjectName;45@ManagedService46public class ServicedSession extends RemoteSession {47 private final DriverService service;48 public ServicedSession(49 DriverService service,50 Dialect downstream,51 Dialect upstream,52 CommandHandler codec,53 SessionId id,54 Map<String, Object> capabilities) {55 super(downstream, upstream, codec, id, capabilities);56 this.service = service;57 new JMXHelper().register(this);58 }59 @Override60 public String toString() {61 return getId().toString() + " (" + service.getClass().getName() + ")";62 }63 @Override64 public void stop() {65 // Try and kill the running session. Both W3C and OSS use the same quit endpoint66 try {67 HttpRequest request = new HttpRequest(HttpMethod.DELETE, "/session/" + getId());68 HttpResponse ignored = new HttpResponse();69 execute(request, ignored);70 } catch (IOException e) {71 // This is fine.72 }73 service.stop();74 }75 public static class Factory extends RemoteSession.Factory<DriverService> {76 private final Predicate<Capabilities> key;77 private final Function<Capabilities, ? extends DriverService> createService;78 private final String serviceClassName;79 public Factory(Predicate<Capabilities> key, String serviceClassName) {80 this.key = key;81 this.serviceClassName = serviceClassName;82 try {83 Class<? extends DriverService> driverClazz =84 Class.forName(serviceClassName).asSubclass(DriverService.class);85 Function<Capabilities, ? extends DriverService> factory =86 get(driverClazz, Capabilities.class);87 if (factory == null) {88 factory = get(driverClazz);89 }90 if (factory == null) {91 throw new IllegalArgumentException(92 "DriverService has no mechanism to create a default instance");93 }94 this.createService = factory;95 } catch (ReflectiveOperationException e) {96 throw new IllegalArgumentException(97 "DriverService class does not exist: " + serviceClassName);98 }99 }100 private Function<Capabilities, ? extends DriverService> get(101 Class<? extends DriverService> driverServiceClazz,102 Class... args) {103 try {104 Method serviceMethod = driverServiceClazz.getDeclaredMethod("createDefaultService", args);105 serviceMethod.setAccessible(true);106 return caps -> {107 try {108 if (args.length > 0) {109 return (DriverService) serviceMethod.invoke(null, caps);110 } else {111 return (DriverService) serviceMethod.invoke(null);112 }113 } catch (ReflectiveOperationException e) {114 throw new SessionNotCreatedException(115 "Unable to create new service: " + driverServiceClazz.getSimpleName(), e);116 }117 };118 } catch (ReflectiveOperationException e) {119 return null;120 }121 }122 @Override123 public boolean test(Capabilities capabilities) {124 return key.test(capabilities);125 }126 @Override127 public Optional<ActiveSession> apply(CreateSessionRequest sessionRequest) {128 Objects.requireNonNull(sessionRequest);129 DriverService service = createService.apply(sessionRequest.getCapabilities());130 try {131 service.start();132 PortProber.waitForPortUp(service.getUrl().getPort(), 30, SECONDS);133 URL url = service.getUrl();134 return performHandshake(135 service,136 url,137 sessionRequest.getDownstreamDialects(),138 sessionRequest.getCapabilities());139 } catch (IOException | IllegalStateException | NullPointerException | InvalidArgumentException e) {140 log.log(Level.INFO, e.getMessage(), e);141 service.stop();142 return Optional.empty();143 }144 }145 @Override146 protected ServicedSession newActiveSession(147 DriverService service,148 Dialect downstream,149 Dialect upstream,150 CommandHandler codec,151 SessionId id,152 Map<String, Object> capabilities) {153 return new ServicedSession(154 service,155 downstream,156 upstream,157 codec,158 id,159 capabilities);160 }161 @Override162 public String toString() {163 return getClass().getName() + " (provider: " + serviceClassName + ")";164 }165 }166 public ObjectName getObjectName() throws MalformedObjectNameException {167 return new ObjectName(String.format("org.seleniumhq.server:type=Session,browser=\"%s\",id=%s",...