Skip to content

Commit 8cc58f4

Browse files
committed
Added support of class names list
1 parent 3dfb885 commit 8cc58f4

File tree

2 files changed

+56
-19
lines changed

2 files changed

+56
-19
lines changed

jdbc/src/main/java/tech/ydb/jdbc/settings/YdbConnectionProperties.java

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -274,31 +274,46 @@ private GrpcTransportBuilder applyChannelInitializer(GrpcTransportBuilder builde
274274
builder = builder.addChannelInitializer(prov);
275275
} else if (initializer instanceof String) {
276276
String className = (String) initializer;
277-
if (!FQCN.matcher(className).matches()) {
278-
throw new SQLException("channelInitializer must be full class name or instance of "
279-
+ "Consumer<ManagedChannelBuilder>");
280-
}
281277

282-
try {
283-
Class<?> clazz = Class.forName(className);
284-
if (!Consumer.class.isAssignableFrom(clazz)) {
285-
throw new SQLException("channelInitializer " + className + " is not implement "
278+
if (FQCN.matcher(className.trim()).matches()) {
279+
builder.addChannelInitializer(newInitializerInstance(className.trim()));
280+
} else {
281+
String[] classNames = className.split(",");
282+
if (classNames.length < 2) {
283+
throw new SQLException("channelInitializer must be full class name or instance of "
286284
+ "Consumer<ManagedChannelBuilder>");
287285
}
288-
@SuppressWarnings("unchecked")
289-
Consumer<Object> prov = clazz.asSubclass(Consumer.class)
290-
.getConstructor(new Class<?>[0])
291-
.newInstance(new Object[0]);
292-
builder = builder.addChannelInitializer(prov);
293-
} catch (ClassNotFoundException ex) {
294-
throw new SQLException("channelInitializer " + className + " not found", ex);
295-
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
296-
| IllegalArgumentException | InvocationTargetException ex) {
297-
throw new SQLException("Cannot construct channelInitializer " + className, ex);
286+
287+
for (String name: classNames) {
288+
if (!FQCN.matcher(name.trim()).matches()) {
289+
throw new SQLException("channelInitializer must be full class name or instance of "
290+
+ "Consumer<ManagedChannelBuilder>");
291+
}
292+
builder.addChannelInitializer(newInitializerInstance(name.trim()));
293+
}
298294
}
299295
} else if (initializer != null) {
300296
throw new SQLException("Cannot parse channelInitializer " + initializer.getClass().getName());
301297
}
302298
return builder;
303299
}
300+
301+
@SuppressWarnings("unchecked")
302+
private Consumer<Object> newInitializerInstance(String className) throws SQLException {
303+
try {
304+
Class<?> clazz = Class.forName(className);
305+
if (!Consumer.class.isAssignableFrom(clazz)) {
306+
throw new SQLException("channelInitializer " + className + " is not implement "
307+
+ "Consumer<ManagedChannelBuilder>");
308+
}
309+
return clazz.asSubclass(Consumer.class)
310+
.getConstructor(new Class<?>[0])
311+
.newInstance(new Object[0]);
312+
} catch (ClassNotFoundException ex) {
313+
throw new SQLException("channelInitializer " + className + " not found", ex);
314+
} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
315+
| IllegalArgumentException | InvocationTargetException ex) {
316+
throw new SQLException("Cannot construct channelInitializer " + className, ex);
317+
}
318+
}
304319
}

jdbc/src/test/java/tech/ydb/jdbc/settings/YdbConnectionPropertiesTest.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,17 @@ public void channelInitializerClassTest() throws SQLException {
139139
props.put("channelInitializer", "tech.ydb.jdbc.settings.CustomChannelInitilizer");
140140
YdbConnectionProperties cp = new YdbConnectionProperties(null, null, props);
141141
GrpcTransportBuilder builder = cp.applyToGrpcTransport(GrpcTransport.forConnectionString(YDB_URL));
142-
Assertions.assertFalse(builder.getChannelInitializers().isEmpty());
142+
Assertions.assertEquals(1, builder.getChannelInitializers().size());
143+
}
144+
145+
@Test
146+
public void channelInitializerClassListTest() throws SQLException {
147+
Properties props = new Properties();
148+
props.put("channelInitializer", "tech.ydb.jdbc.settings.CustomChannelInitilizer, "
149+
+ "tech.ydb.jdbc.settings.CustomChannelInitilizer");
150+
YdbConnectionProperties cp = new YdbConnectionProperties(null, null, props);
151+
GrpcTransportBuilder builder = cp.applyToGrpcTransport(GrpcTransport.forConnectionString(YDB_URL));
152+
Assertions.assertEquals(2, builder.getChannelInitializers().size());
143153
}
144154

145155
@Test
@@ -153,6 +163,18 @@ public void channelInitializerWrongClassNameTest() throws SQLException {
153163
);
154164
}
155165

166+
@Test
167+
public void channelInitializerWrongClassNameListTest() throws SQLException {
168+
Properties props = new Properties();
169+
props.put("channelInitializer", "tech.ydb.jdbc.settings.CustomChannelInitilizer, "
170+
+ "1tech.ydb.jdbc.settings.StaticTokenProvider");
171+
YdbConnectionProperties cp = new YdbConnectionProperties(null, null, props);
172+
ExceptionAssert.sqlException(
173+
"channelInitializer must be full class name or instance of Consumer<ManagedChannelBuilder>",
174+
() -> cp.applyToGrpcTransport(GrpcTransport.forConnectionString(YDB_URL))
175+
);
176+
}
177+
156178
@Test
157179
public void channelInitializerUnknownClassTest() throws SQLException {
158180
Properties props = new Properties();

0 commit comments

Comments
 (0)