2222import java .net .URI ;
2323import java .security .GeneralSecurityException ;
2424
25+ import org .neo4j .driver .internal .cluster .LoadBalancer ;
2526import org .neo4j .driver .internal .cluster .RoutingSettings ;
2627import org .neo4j .driver .internal .net .BoltServerAddress ;
2728import org .neo4j .driver .internal .net .SocketConnector ;
2829import org .neo4j .driver .internal .net .pooling .PoolSettings ;
2930import org .neo4j .driver .internal .net .pooling .SocketConnectionPool ;
3031import org .neo4j .driver .internal .security .SecurityPlan ;
3132import org .neo4j .driver .internal .spi .ConnectionPool ;
33+ import org .neo4j .driver .internal .spi .ConnectionProvider ;
3234import org .neo4j .driver .internal .spi .Connector ;
3335import org .neo4j .driver .internal .util .Clock ;
3436import org .neo4j .driver .v1 .AuthToken ;
@@ -50,13 +52,10 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
5052 BoltServerAddress address = BoltServerAddress .from ( uri );
5153 SecurityPlan securityPlan = createSecurityPlan ( address , config );
5254 ConnectionPool connectionPool = createConnectionPool ( authToken , securityPlan , config );
53- SessionFactory sessionFactory = createSessionFactory ( config );
5455
5556 try
5657 {
57- return createDriver ( address , uri .getScheme (), connectionPool , config , routingSettings , securityPlan ,
58- sessionFactory
59- );
58+ return createDriver ( address , uri .getScheme (), connectionPool , config , routingSettings , securityPlan );
6059 }
6160 catch ( Throwable driverError )
6261 {
@@ -74,42 +73,68 @@ public final Driver newInstance( URI uri, AuthToken authToken, RoutingSettings r
7473 }
7574
7675 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 )
7977 {
8078 switch ( scheme .toLowerCase () )
8179 {
8280 case "bolt" :
83- return createDirectDriver ( address , connectionPool , config , securityPlan , sessionFactory );
81+ return createDirectDriver ( address , connectionPool , config , securityPlan );
8482 case "bolt+routing" :
85- return createRoutingDriver ( address , connectionPool , config , routingSettings , securityPlan ,
86- sessionFactory );
83+ return createRoutingDriver ( address , connectionPool , config , routingSettings , securityPlan );
8784 default :
8885 throw new ClientException ( format ( "Unsupported URI scheme: %s" , scheme ) );
8986 }
9087 }
9188
9289 /**
93- * Creates new {@link DirectDriver} .
90+ * Creates a new driver for "bolt" scheme .
9491 * <p>
9592 * <b>This method is protected only for testing</b>
9693 */
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 )
9996 {
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 );
101100 }
102101
103102 /**
104- * Creates new {@link RoutingDriver} .
103+ * Creates new a new driver for "bolt+routing" scheme .
105104 * <p>
106105 * <b>This method is protected only for testing</b>
107106 */
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 )
110109 {
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 );
113138 }
114139
115140 /**
@@ -150,13 +175,14 @@ protected Connector createConnector( ConnectionSettings connectionSettings, Secu
150175 return new SocketConnector ( connectionSettings , securityPlan , logging );
151176 }
152177
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 )
154184 {
155- if ( config .logLeakedSessions () )
156- {
157- return new LeakLoggingNetworkSessionFactory ( config .logging () );
158- }
159- return new NetworkSessionFactory ();
185+ return new SessionFactoryImpl ( connectionProvider , config , config .logging () );
160186 }
161187
162188 private static SecurityPlan createSecurityPlan ( BoltServerAddress address , Config config )
0 commit comments