57
57
import java .net .URL ;
58
58
import java .util .Map ;
59
59
import java .util .Set ;
60
- import java .util .function .Supplier ;
60
+ import java .util .function .Function ;
61
61
62
62
class ServicedSession implements ActiveSession {
63
63
@@ -146,7 +146,7 @@ public void stop() {
146
146
147
147
public static class Factory implements SessionFactory {
148
148
149
- private final Supplier < ? extends DriverService > createService ;
149
+ private final Function < Capabilities , ? extends DriverService > createService ;
150
150
private final String serviceClassName ;
151
151
152
152
Factory (String serviceClassName ) {
@@ -155,25 +155,51 @@ public static class Factory implements SessionFactory {
155
155
Class <? extends DriverService > driverClazz =
156
156
Class .forName (serviceClassName ).asSubclass (DriverService .class );
157
157
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
+ }
160
175
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 -> {
162
184
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
+ }
164
190
} catch (ReflectiveOperationException e ) {
165
191
throw new SessionNotCreatedException (
166
- "Unable to create new service: " + driverClazz .getSimpleName (), e );
192
+ "Unable to create new service: " + driverServiceClazz .getSimpleName (), e );
167
193
}
168
194
};
169
195
} catch (ReflectiveOperationException e ) {
170
- throw new SessionNotCreatedException ( "Cannot find service factory method" , e ) ;
196
+ return null ;
171
197
}
172
198
}
173
199
174
200
@ Override
175
201
public ActiveSession apply (Set <Dialect > downstreamDialects , Capabilities capabilities ) {
176
- DriverService service = createService .get ( );
202
+ DriverService service = createService .apply ( capabilities );
177
203
178
204
try {
179
205
service .start ();
0 commit comments