Skip to content

Commit

Permalink
Added logging of unused runtime properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaPapa committed Jan 22, 2018
1 parent 2f22b1e commit 62a7257
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 12 deletions.
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,46 @@ 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) {
boolean isFound = false;
StringBuilder logResult = new StringBuilder();
String nodeName = nodeDescriptor.getDataChannelDescriptor().getName()
+ "."
+ nodeDescriptor.getName();
logResult.append("Found unused runtime properties for '").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(", ");
isFound = true;
}
property = properties.get(propertyConstant);
if (property != null) {
logResult.append(propertyConstant).append(", ");
isFound = true;
}
}
logResult.delete(logResult.length() - 2, logResult.length());
logResult.append(". This runtime properties was ignored. Configuration were taken from project DataSource. ");
logResult.append("For using configuration from runtime properties, move driver and url configuration to properties.");
if (isFound) {
logger.info(logResult.toString());
}
}

protected boolean shouldConfigureDataSourceFromProperties(
DataNodeDescriptor nodeDescriptor) {

Expand All @@ -126,28 +166,26 @@ 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) {
if (channelName != 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) {
if (channelName != 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

0 comments on commit 62a7257

Please sign in to comment.