Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging of unused runtime properties. #258

Merged
merged 1 commit into from
Jan 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class DelegatingDataSourceFactory implements DataSourceFactory {
protected Map<DataSource, ScopeEventListener> managedDataSources;

public DelegatingDataSourceFactory() {
managedDataSources = new ConcurrentHashMap<DataSource, ScopeEventListener>();
managedDataSources = new ConcurrentHashMap<>();
}

@Override
Expand Down Expand Up @@ -94,7 +94,7 @@ protected void attachToScope(DataSource dataSource) {
}

protected DataSourceFactory getDataSourceFactory(DataNodeDescriptor nodeDescriptor) {
String typeName = null;
String typeName;

if (shouldConfigureDataSourceFromProperties(nodeDescriptor)) {
typeName = PropertyDataSourceFactory.class.getName();
Expand All @@ -116,6 +116,49 @@ protected DataSourceFactory getDataSourceFactory(DataNodeDescriptor nodeDescript
return objectFactory.newInstance(DataSourceFactory.class, typeName);
}

private String getDataNodePropertyName(DataNodeDescriptor nodeDescriptor, String propertyConstant) {
return propertyConstant
+ "."
+ nodeDescriptor.getDataChannelDescriptor().getName()
+ "."
+ nodeDescriptor.getName();
}

private void findUnusedProperties(DataNodeDescriptor nodeDescriptor) {
if(!logger.isInfoEnabled() || nodeDescriptor.getDataChannelDescriptor() == null) {
return;
}
boolean found = false;
StringBuilder logResult = new StringBuilder();
String nodeName = nodeDescriptor.getDataChannelDescriptor().getName()
+ "."
+ nodeDescriptor.getName();
logResult.append("Following runtime properties were ignored for node '").append(nodeName).append("': ");
String[] verifiableProperties = new String[] {
Constants.JDBC_USERNAME_PROPERTY, Constants.JDBC_PASSWORD_PROPERTY,
Constants.JDBC_MAX_CONNECTIONS_PROPERTY, Constants.JDBC_MIN_CONNECTIONS_PROPERTY,
Constants.JDBC_MAX_QUEUE_WAIT_TIME, Constants.JDBC_VALIDATION_QUERY_PROPERTY
};
for (String propertyConstant : verifiableProperties) {
String property = properties.get(getDataNodePropertyName(nodeDescriptor, propertyConstant));
if (property != null) {
logResult.append(getDataNodePropertyName(nodeDescriptor, propertyConstant)).append(", ");
found = true;
}
property = properties.get(propertyConstant);
if (property != null) {
logResult.append(propertyConstant).append(", ");
found = true;
}
}
if (found) {
logResult.delete(logResult.length() - 2, logResult.length())
.append(". Will use project DataSource configuration. ")
.append("Set driver and url properties to enable DataSource configuration override. ");
logger.info(logResult.toString());
}
}

protected boolean shouldConfigureDataSourceFromProperties(
DataNodeDescriptor nodeDescriptor) {

Expand All @@ -126,28 +169,22 @@ protected boolean shouldConfigureDataSourceFromProperties(
String driver = properties.get(Constants.JDBC_DRIVER_PROPERTY);

if (driver == null && channelName != null) {
driver = properties.get(Constants.JDBC_DRIVER_PROPERTY
+ "."
+ nodeDescriptor.getDataChannelDescriptor().getName()
+ "."
+ nodeDescriptor.getName());
driver = properties.get(getDataNodePropertyName(nodeDescriptor, Constants.JDBC_DRIVER_PROPERTY));
}

if (driver == null) {
findUnusedProperties(nodeDescriptor);
return false;
}

String url = properties.get(Constants.JDBC_URL_PROPERTY);

if (url == null && channelName != null) {
url = properties.get(Constants.JDBC_URL_PROPERTY
+ "."
+ nodeDescriptor.getDataChannelDescriptor().getName()
+ "."
+ nodeDescriptor.getName());
url = properties.get(getDataNodePropertyName(nodeDescriptor, Constants.JDBC_URL_PROPERTY));
}

if (url == null) {
findUnusedProperties(nodeDescriptor);
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,55 @@ public void testGetDataSourceFactory_Explicit() throws Exception {
((MockDataSourceFactory1) factory).getInjector());
}

@Test
public void testGetDataSourceFactory_UnusedProperties() throws Exception {
final RuntimeProperties properties = mock(RuntimeProperties.class);
when(properties.get(Constants.JDBC_DRIVER_PROPERTY)).thenReturn("x");
when(properties.get(Constants.JDBC_URL_PROPERTY)).thenReturn(null);
when(properties.get(Constants.JDBC_USERNAME_PROPERTY)).thenReturn("username");
when(properties.get(Constants.JDBC_PASSWORD_PROPERTY)).thenReturn("12345");

DataChannelDescriptor channelDescriptor = new DataChannelDescriptor();
channelDescriptor.setName("X");
DataNodeDescriptor nodeDescriptor = new DataNodeDescriptor();
nodeDescriptor.setName("node1");
nodeDescriptor.setDataSourceFactoryType(MockDataSourceFactory1.class.getName());
nodeDescriptor.setDataChannelDescriptor(channelDescriptor);

Module testModule = new Module() {

public void configure(Binder binder) {
binder.bind(ClassLoaderManager.class).to(DefaultClassLoaderManager.class);
binder.bind(AdhocObjectFactory.class).to(DefaultAdhocObjectFactory.class);
binder.bind(ResourceLocator.class).to(MockResourceLocator.class);
binder.bind(Key.get(ResourceLocator.class, Constants.SERVER_RESOURCE_LOCATOR)).to(MockResourceLocator.class);
binder.bind(RuntimeProperties.class).toInstance(properties);
binder.bind(JdbcEventLogger.class).to(Slf4jJdbcEventLogger.class);
}
};

Injector injector = DIBootstrap.createInjector(testModule);

DelegatingDataSourceFactory factoryLoader = new DelegatingDataSourceFactory();
injector.injectMembers(factoryLoader);
DataSourceFactory factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertFalse(factory instanceof PropertyDataSourceFactory);

nodeDescriptor.setName("node2");
when(properties.get(Constants.JDBC_MIN_CONNECTIONS_PROPERTY + ".X.node2")).thenReturn("3");
when(properties.get(Constants.JDBC_PASSWORD_PROPERTY + ".X.node2")).thenReturn("123456");
factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertFalse(factory instanceof PropertyDataSourceFactory);

nodeDescriptor.setName("node3");
when(properties.get(Constants.JDBC_URL_PROPERTY + ".X.node3")).thenReturn("url");
factory = factoryLoader.getDataSourceFactory(nodeDescriptor);
assertNotNull(factory);
assertTrue(factory instanceof PropertyDataSourceFactory);
}

@Test
public void testGetDataSourceFactory_Property() throws Exception {

Expand Down