17
17
18
18
package org .openqa .selenium .remote .server ;
19
19
20
+ import com .google .common .cache .Cache ;
21
+ import com .google .common .cache .CacheBuilder ;
22
+ import com .google .common .cache .RemovalListener ;
20
23
import com .google .common .collect .ImmutableList ;
24
+ import com .google .common .collect .ImmutableSet ;
21
25
import com .google .common .io .Files ;
22
26
23
27
import org .openqa .selenium .Capabilities ;
27
31
import org .openqa .selenium .remote .DesiredCapabilities ;
28
32
import org .openqa .selenium .remote .SessionId ;
29
33
30
- import java .util .Collections ;
31
34
import java .util .List ;
32
- import java .util .Map ;
33
35
import java .util .ServiceLoader ;
34
36
import java .util .Set ;
35
- import java .util .concurrent .ConcurrentHashMap ;
36
37
import java .util .logging .Logger ;
37
38
38
39
public class DefaultDriverSessions implements DriverSessions {
@@ -42,8 +43,7 @@ public class DefaultDriverSessions implements DriverSessions {
42
43
private final DriverFactory factory ;
43
44
private final Clock clock ;
44
45
45
- private final Map <SessionId , Session > sessionIdToDriver =
46
- new ConcurrentHashMap <>();
46
+ private final Cache <SessionId , Session > sessionIdToDriver ;
47
47
48
48
private static List <DriverProvider > defaultDriverProviders =
49
49
new ImmutableList .Builder <DriverProvider >()
@@ -71,6 +71,12 @@ public DefaultDriverSessions(Platform runningOn, DriverFactory factory, Clock cl
71
71
this .clock = clock ;
72
72
registerDefaults (runningOn );
73
73
registerServiceLoaders (runningOn );
74
+
75
+ RemovalListener <SessionId , Session > listener = notification -> notification .getValue ().close ();
76
+
77
+ this .sessionIdToDriver = CacheBuilder .newBuilder ()
78
+ .removalListener (listener )
79
+ .build ();
74
80
}
75
81
76
82
private void registerDefaults (Platform current ) {
@@ -128,17 +134,14 @@ public SessionId newSession(Capabilities desiredCapabilities) throws Exception {
128
134
}
129
135
130
136
public Session get (SessionId sessionId ) {
131
- return sessionIdToDriver .get (sessionId );
137
+ return sessionIdToDriver .getIfPresent (sessionId );
132
138
}
133
139
134
140
public void deleteSession (SessionId sessionId ) {
135
- final Session removedSession = sessionIdToDriver .remove (sessionId );
136
- if (removedSession != null ) {
137
- removedSession .close ();
138
- }
141
+ sessionIdToDriver .invalidate (sessionId );
139
142
}
140
143
141
144
public Set <SessionId > getSessions () {
142
- return Collections . unmodifiableSet (sessionIdToDriver .keySet ());
145
+ return ImmutableSet . copyOf (sessionIdToDriver . asMap () .keySet ());
143
146
}
144
147
}
0 commit comments