22
22
import java .net .URI ;
23
23
import java .security .GeneralSecurityException ;
24
24
25
+ import org .neo4j .driver .internal .cluster .LoadBalancer ;
25
26
import org .neo4j .driver .internal .cluster .RoutingSettings ;
26
27
import org .neo4j .driver .internal .net .BoltServerAddress ;
27
28
import org .neo4j .driver .internal .net .SocketConnector ;
28
29
import org .neo4j .driver .internal .net .pooling .PoolSettings ;
29
30
import org .neo4j .driver .internal .net .pooling .SocketConnectionPool ;
30
31
import org .neo4j .driver .internal .security .SecurityPlan ;
31
32
import org .neo4j .driver .internal .spi .ConnectionPool ;
33
+ import org .neo4j .driver .internal .spi .ConnectionProvider ;
32
34
import org .neo4j .driver .internal .spi .Connector ;
33
35
import org .neo4j .driver .internal .util .Clock ;
34
36
import org .neo4j .driver .v1 .AuthToken ;
@@ -50,13 +52,10 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
50
52
BoltServerAddress address = BoltServerAddress .from ( uri );
51
53
SecurityPlan securityPlan = createSecurityPlan ( address , config );
52
54
ConnectionPool connectionPool = createConnectionPool ( authToken , securityPlan , config );
53
- SessionFactory sessionFactory = createSessionFactory ( config );
54
55
55
56
try
56
57
{
57
- return createDriver ( address , uri .getScheme (), connectionPool , config , routingSettings , securityPlan ,
58
- sessionFactory
59
- );
58
+ return createDriver ( address , uri .getScheme (), connectionPool , config , routingSettings , securityPlan );
60
59
}
61
60
catch ( Throwable driverError )
62
61
{
@@ -74,42 +73,68 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
74
73
}
75
74
76
75
private Driver createDriver ( BoltServerAddress address , String scheme , ConnectionPool connectionPool ,
77
- Config config , RoutingSettings routingSettings , SecurityPlan securityPlan ,
78
- SessionFactory sessionFactory )
76
+ Config config , RoutingSettings routingSettings , SecurityPlan securityPlan )
79
77
{
80
78
switch ( scheme .toLowerCase () )
81
79
{
82
80
case "bolt" :
83
- return createDirectDriver ( address , connectionPool , config , securityPlan , sessionFactory );
81
+ return createDirectDriver ( address , connectionPool , config , securityPlan );
84
82
case "bolt+routing" :
85
- return createRoutingDriver ( address , connectionPool , config , routingSettings , securityPlan ,
86
- sessionFactory );
83
+ return createRoutingDriver ( address , connectionPool , config , routingSettings , securityPlan );
87
84
default :
88
85
throw new ClientException ( format ( "Unsupported URI scheme: %s" , scheme ) );
89
86
}
90
87
}
91
88
92
89
/**
93
- * Creates new {@link DirectDriver} .
90
+ * Creates a new driver for "bolt" scheme .
94
91
* <p>
95
92
* <b>This method is protected only for testing</b>
96
93
*/
97
- protected DirectDriver createDirectDriver ( BoltServerAddress address , ConnectionPool connectionPool ,
98
- Config config , SecurityPlan securityPlan , SessionFactory sessionFactory )
94
+ protected Driver createDirectDriver ( BoltServerAddress address , ConnectionPool connectionPool , Config config ,
95
+ SecurityPlan securityPlan )
99
96
{
100
- return new DirectDriver ( address , connectionPool , securityPlan , sessionFactory , config .logging () );
97
+ ConnectionProvider connectionProvider = new DirectConnectionProvider ( address , connectionPool );
98
+ SessionFactory sessionFactory = createSessionFactory ( connectionProvider , config );
99
+ return createDriver ( config , securityPlan , sessionFactory );
101
100
}
102
101
103
102
/**
104
- * Creates new {@link RoutingDriver} .
103
+ * Creates new a new driver for "bolt+routing" scheme .
105
104
* <p>
106
105
* <b>This method is protected only for testing</b>
107
106
*/
108
- protected RoutingDriver createRoutingDriver ( BoltServerAddress address , ConnectionPool connectionPool ,
109
- Config config , RoutingSettings routingSettings , SecurityPlan securityPlan , SessionFactory sessionFactory )
107
+ protected Driver createRoutingDriver ( BoltServerAddress address , ConnectionPool connectionPool ,
108
+ Config config , RoutingSettings routingSettings , SecurityPlan securityPlan )
110
109
{
111
- return new RoutingDriver ( routingSettings , address , connectionPool , securityPlan , sessionFactory ,
112
- createClock (), config .logging () );
110
+ if ( !securityPlan .isRoutingCompatible () )
111
+ {
112
+ throw new IllegalArgumentException ( "The chosen security plan is not compatible with a routing driver" );
113
+ }
114
+ ConnectionProvider connectionProvider = createLoadBalancer ( address , connectionPool , config , routingSettings );
115
+ SessionFactory sessionFactory = createSessionFactory ( connectionProvider , config );
116
+ return createDriver ( config , securityPlan , sessionFactory );
117
+ }
118
+
119
+ /**
120
+ * Creates new {@link Driver}.
121
+ * <p>
122
+ * <b>This method is protected only for testing</b>
123
+ */
124
+ protected InternalDriver createDriver ( Config config , SecurityPlan securityPlan , SessionFactory sessionFactory )
125
+ {
126
+ return new InternalDriver ( securityPlan , sessionFactory , config .logging () );
127
+ }
128
+
129
+ /**
130
+ * Creates new {@link LoadBalancer} for the routing driver.
131
+ * <p>
132
+ * <b>This method is protected only for testing</b>
133
+ */
134
+ protected LoadBalancer createLoadBalancer ( BoltServerAddress address , ConnectionPool connectionPool , Config config ,
135
+ RoutingSettings routingSettings )
136
+ {
137
+ return new LoadBalancer ( routingSettings , connectionPool , createClock (), config .logging (), address );
113
138
}
114
139
115
140
/**
@@ -150,13 +175,14 @@ protected Connector createConnector( ConnectionSettings connectionSettings, Secu
150
175
return new SocketConnector ( connectionSettings , securityPlan , logging );
151
176
}
152
177
153
- private static SessionFactory createSessionFactory ( Config config )
178
+ /**
179
+ * Creates new {@link SessionFactory}.
180
+ * <p>
181
+ * <b>This method is protected only for testing</b>
182
+ */
183
+ protected SessionFactory createSessionFactory ( ConnectionProvider connectionProvider , Config config )
154
184
{
155
- if ( config .logLeakedSessions () )
156
- {
157
- return new LeakLoggingNetworkSessionFactory ( config .logging () );
158
- }
159
- return new NetworkSessionFactory ();
185
+ return new SessionFactoryImpl ( connectionProvider , config , config .logging () );
160
186
}
161
187
162
188
private static SecurityPlan createSecurityPlan ( BoltServerAddress address , Config config )
0 commit comments