5757import java .net .URL ;
5858import java .util .Map ;
5959import java .util .Set ;
60- import java .util .function .Supplier ;
60+ import java .util .function .Function ;
6161
6262class ServicedSession implements ActiveSession {
6363
@@ -146,7 +146,7 @@ public void stop() {
146146
147147 public static class Factory implements SessionFactory {
148148
149- private final Supplier < ? extends DriverService > createService ;
149+ private final Function < Capabilities , ? extends DriverService > createService ;
150150 private final String serviceClassName ;
151151
152152 Factory (String serviceClassName ) {
@@ -155,25 +155,51 @@ public static class Factory implements SessionFactory {
155155 Class <? extends DriverService > driverClazz =
156156 Class .forName (serviceClassName ).asSubclass (DriverService .class );
157157
158- Method serviceMethod = driverClazz .getMethod ("createDefaultService" );
159- serviceMethod .setAccessible (true );
158+ Function <Capabilities , ? extends DriverService > factory =
159+ get (driverClazz , "createDefaultService" , Capabilities .class );
160+ if (factory == null ) {
161+ factory = get (driverClazz , "createDefaultService" );
162+ }
163+
164+ if (factory == null ) {
165+ throw new IllegalArgumentException (
166+ "DriverService has no mechansim to create a default instance" );
167+ }
168+
169+ this .createService = factory ;
170+ } catch (ReflectiveOperationException e ) {
171+ throw new IllegalArgumentException (
172+ "DriverService class does not exist: " + serviceClassName );
173+ }
174+ }
160175
161- this .createService = () -> {
176+ private Function <Capabilities , ? extends DriverService > get (
177+ Class <? extends DriverService > driverServiceClazz ,
178+ String methodName ,
179+ Class ... args ) {
180+ try {
181+ Method serviceMethod = driverServiceClazz .getDeclaredMethod (methodName , args );
182+ serviceMethod .setAccessible (true );
183+ return caps -> {
162184 try {
163- return (DriverService ) serviceMethod .invoke (null );
185+ if (args .length > 0 ) {
186+ return (DriverService ) serviceMethod .invoke (null , caps );
187+ } else {
188+ return (DriverService ) serviceMethod .invoke (null );
189+ }
164190 } catch (ReflectiveOperationException e ) {
165191 throw new SessionNotCreatedException (
166- "Unable to create new service: " + driverClazz .getSimpleName (), e );
192+ "Unable to create new service: " + driverServiceClazz .getSimpleName (), e );
167193 }
168194 };
169195 } catch (ReflectiveOperationException e ) {
170- throw new SessionNotCreatedException ( "Cannot find service factory method" , e ) ;
196+ return null ;
171197 }
172198 }
173199
174200 @ Override
175201 public ActiveSession apply (Set <Dialect > downstreamDialects , Capabilities capabilities ) {
176- DriverService service = createService .get ( );
202+ DriverService service = createService .apply ( capabilities );
177203
178204 try {
179205 service .start ();
0 commit comments