in log4j-core/src/main/java/org/apache/logging/log4j/core/appender/db/jdbc/FactoryMethodConnectionSource.java [71:157]
public static FactoryMethodConnectionSource createConnectionSource(
@PluginAttribute("class") final String className, @PluginAttribute("method") final String methodName) {
if (Strings.isEmpty(className) || Strings.isEmpty(methodName)) {
LOGGER.error("No class name or method name specified for the connection factory method.");
return null;
}
final Method method;
try {
final Class<?> factoryClass = Loader.loadClass(className);
method = factoryClass.getMethod(methodName);
} catch (final Exception e) {
LOGGER.error(e.toString(), e);
return null;
}
final Class<?> returnType = method.getReturnType();
String returnTypeString = returnType.getName();
DataSource dataSource;
if (returnType == DataSource.class) {
try {
dataSource = (DataSource) method.invoke(null);
returnTypeString += "[" + dataSource + ']';
} catch (final Exception e) {
LOGGER.error(e.toString(), e);
return null;
}
} else if (returnType == Connection.class) {
dataSource = new DataSource() {
@Override
public Connection getConnection() throws SQLException {
try {
return (Connection) method.invoke(null);
} catch (final Exception e) {
throw new SQLException("Failed to obtain connection from factory method.", e);
}
}
@Override
public Connection getConnection(final String username, final String password) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public int getLoginTimeout() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public PrintWriter getLogWriter() throws SQLException {
throw new UnsupportedOperationException();
}
@Override
@SuppressWarnings("unused")
public java.util.logging.Logger getParentLogger() {
throw new UnsupportedOperationException();
}
@Override
public boolean isWrapperFor(final Class<?> iface) throws SQLException {
return false;
}
@Override
public void setLoginTimeout(final int seconds) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public void setLogWriter(final PrintWriter out) throws SQLException {
throw new UnsupportedOperationException();
}
@Override
public <T> T unwrap(final Class<T> iface) throws SQLException {
return null;
}
};
} else {
LOGGER.error(
"Method [{}.{}()] returns unsupported type [{}].", className, methodName, returnType.getName());
return null;
}
return new FactoryMethodConnectionSource(dataSource, className, methodName, returnTypeString);
}