Skip to content

Commit

Permalink
Convert to Java 11
Browse files Browse the repository at this point in the history
  • Loading branch information
brettwooldridge committed Jul 13, 2021
1 parent 6546dd2 commit 1991355
Show file tree
Hide file tree
Showing 22 changed files with 353 additions and 353 deletions.
43 changes: 20 additions & 23 deletions src/main/java/com/zaxxer/hikari/HikariConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,10 @@
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.security.AccessControlException;
import java.sql.Connection;
import java.util.Properties;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
Expand All @@ -45,7 +42,7 @@
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;

@SuppressWarnings({"SameParameterValue"})
@SuppressWarnings({"SameParameterValue", "unused"})
public class HikariConfig implements HikariConfigMXBean
{
private static final Logger LOGGER = LoggerFactory.getLogger(HikariConfig.class);
Expand Down Expand Up @@ -123,7 +120,7 @@ public HikariConfig()
isAutoCommit = true;
keepaliveTime = DEFAULT_KEEPALIVE_TIME;

String systemProp = System.getProperty("hikaricp.configurationFile");
var systemProp = System.getProperty("hikaricp.configurationFile");
if (systemProp != null) {
loadProperties(systemProp);
}
Expand Down Expand Up @@ -477,7 +474,7 @@ public void setDriverClassName(String driverClassName)
{
checkIfSealed();

Class<?> driverClass = attemptFromContextLoader(driverClassName);
var driverClass = attemptFromContextLoader(driverClassName);
try {
if (driverClass == null) {
driverClass = this.getClass().getClassLoader().loadClass(driverClassName);
Expand Down Expand Up @@ -875,7 +872,7 @@ public void setExceptionOverrideClassName(String exceptionOverrideClassName)
{
checkIfSealed();

Class<?> overrideClass = attemptFromContextLoader(exceptionOverrideClassName);
var overrideClass = attemptFromContextLoader(exceptionOverrideClassName);
try {
if (overrideClass == null) {
overrideClass = this.getClass().getClassLoader().loadClass(exceptionOverrideClassName);
Expand Down Expand Up @@ -944,7 +941,7 @@ void seal()
*/
public void copyStateTo(HikariConfig other)
{
for (Field field : HikariConfig.class.getDeclaredFields()) {
for (var field : HikariConfig.class.getDeclaredFields()) {
if (!Modifier.isFinal(field.getModifiers())) {
field.setAccessible(true);
try {
Expand All @@ -964,10 +961,10 @@ public void copyStateTo(HikariConfig other)
// ***********************************************************************

private Class<?> attemptFromContextLoader(final String driverClassName) {
final ClassLoader threadContextClassLoader = Thread.currentThread().getContextClassLoader();
final var threadContextClassLoader = Thread.currentThread().getContextClassLoader();
if (threadContextClassLoader != null) {
try {
final Class<?> driverClass = threadContextClassLoader.loadClass(driverClassName);
final var driverClass = threadContextClassLoader.loadClass(driverClassName);
LOGGER.debug("Driver class {} found in Thread context class loader {}", driverClassName, threadContextClassLoader);
return driverClass;
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -1101,12 +1098,12 @@ private void checkIfSealed()
private void logConfiguration()
{
LOGGER.debug("{} - configuration:", poolName);
final Set<String> propertyNames = new TreeSet<>(PropertyElf.getPropertyNames(HikariConfig.class));
for (String prop : propertyNames) {
final var propertyNames = new TreeSet<>(PropertyElf.getPropertyNames(HikariConfig.class));
for (var prop : propertyNames) {
try {
Object value = PropertyElf.getProperty(prop, this);
var value = PropertyElf.getProperty(prop, this);
if ("dataSourceProperties".equals(prop)) {
Properties dsProps = PropertyElf.copyProperties(dataSourceProperties);
var dsProps = PropertyElf.copyProperties(dataSourceProperties);
dsProps.setProperty("password", "<masked>");
value = dsProps;
}
Expand Down Expand Up @@ -1142,10 +1139,10 @@ else if (value == null) {

private void loadProperties(String propertyFileName)
{
final File propFile = new File(propertyFileName);
try (final InputStream is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) {
final var propFile = new File(propertyFileName);
try (final var is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) {
if (is != null) {
Properties props = new Properties();
var props = new Properties();
props.load(is);
PropertyElf.setTargetFromProperties(this, props);
}
Expand All @@ -1160,21 +1157,21 @@ private void loadProperties(String propertyFileName)

private String generatePoolName()
{
final String prefix = "HikariPool-";
final var prefix = "HikariPool-";
try {
// Pool number is global to the VM to avoid overlapping pool numbers in classloader scoped environments
synchronized (System.getProperties()) {
final String next = String.valueOf(Integer.getInteger("com.zaxxer.hikari.pool_number", 0) + 1);
final var next = String.valueOf(Integer.getInteger("com.zaxxer.hikari.pool_number", 0) + 1);
System.setProperty("com.zaxxer.hikari.pool_number", next);
return prefix + next;
}
} catch (AccessControlException e) {
// The SecurityManager didn't allow us to read/write system properties
// so just generate a random pool number instead
final ThreadLocalRandom random = ThreadLocalRandom.current();
final StringBuilder buf = new StringBuilder(prefix);
final var random = ThreadLocalRandom.current();
final var buf = new StringBuilder(prefix);

for (int i = 0; i < 4; i++) {
for (var i = 0; i < 4; i++) {
buf.append(ID_CHARACTERS[random.nextInt(62)]);
}

Expand All @@ -1188,7 +1185,7 @@ private Object getObjectOrPerformJndiLookup(Object object)
{
if (object instanceof String) {
try {
InitialContext initCtx = new InitialContext();
var initCtx = new InitialContext();
return initCtx.lookup((String) object);
}
catch (NamingException e) {
Expand Down
28 changes: 14 additions & 14 deletions src/main/java/com/zaxxer/hikari/HikariDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public PrintWriter getLogWriter() throws SQLException
@Override
public void setLogWriter(PrintWriter out) throws SQLException
{
HikariPool p = pool;
var p = pool;
if (p != null) {
p.getUnwrappedDataSource().setLogWriter(out);
}
Expand All @@ -157,7 +157,7 @@ public void setLogWriter(PrintWriter out) throws SQLException
@Override
public void setLoginTimeout(int seconds) throws SQLException
{
HikariPool p = pool;
var p = pool;
if (p != null) {
p.getUnwrappedDataSource().setLoginTimeout(seconds);
}
Expand All @@ -167,7 +167,7 @@ public void setLoginTimeout(int seconds) throws SQLException
@Override
public int getLoginTimeout() throws SQLException
{
HikariPool p = pool;
var p = pool;
return (p != null ? p.getUnwrappedDataSource().getLoginTimeout() : 0);
}

Expand All @@ -187,9 +187,9 @@ public <T> T unwrap(Class<T> iface) throws SQLException
return (T) this;
}

HikariPool p = pool;
var p = pool;
if (p != null) {
final DataSource unwrappedDataSource = p.getUnwrappedDataSource();
final var unwrappedDataSource = p.getUnwrappedDataSource();
if (iface.isInstance(unwrappedDataSource)) {
return (T) unwrappedDataSource;
}
Expand All @@ -210,9 +210,9 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException
return true;
}

HikariPool p = pool;
var p = pool;
if (p != null) {
final DataSource unwrappedDataSource = p.getUnwrappedDataSource();
final var unwrappedDataSource = p.getUnwrappedDataSource();
if (iface.isInstance(unwrappedDataSource)) {
return true;
}
Expand All @@ -233,10 +233,10 @@ public boolean isWrapperFor(Class<?> iface) throws SQLException
@Override
public void setMetricRegistry(Object metricRegistry)
{
boolean isAlreadySet = getMetricRegistry() != null;
var isAlreadySet = getMetricRegistry() != null;
super.setMetricRegistry(metricRegistry);

HikariPool p = pool;
var p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("MetricRegistry can only be set one time");
Expand All @@ -251,10 +251,10 @@ public void setMetricRegistry(Object metricRegistry)
@Override
public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory)
{
boolean isAlreadySet = getMetricsTrackerFactory() != null;
var isAlreadySet = getMetricsTrackerFactory() != null;
super.setMetricsTrackerFactory(metricsTrackerFactory);

HikariPool p = pool;
var p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("MetricsTrackerFactory can only be set one time");
Expand All @@ -269,10 +269,10 @@ public void setMetricsTrackerFactory(MetricsTrackerFactory metricsTrackerFactory
@Override
public void setHealthCheckRegistry(Object healthCheckRegistry)
{
boolean isAlreadySet = getHealthCheckRegistry() != null;
var isAlreadySet = getHealthCheckRegistry() != null;
super.setHealthCheckRegistry(healthCheckRegistry);

HikariPool p = pool;
var p = pool;
if (p != null) {
if (isAlreadySet) {
throw new IllegalStateException("HealthCheckRegistry can only be set one time");
Expand Down Expand Up @@ -344,7 +344,7 @@ public void close()
return;
}

HikariPool p = pool;
var p = pool;
if (p != null) {
try {
LOGGER.info("{} - Shutdown initiated...", getPoolName());
Expand Down
36 changes: 14 additions & 22 deletions src/main/java/com/zaxxer/hikari/HikariJNDIFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,13 @@

package com.zaxxer.hikari;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;
import java.util.Set;
import com.zaxxer.hikari.util.PropertyElf;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.RefAddr;
import javax.naming.Reference;
import javax.naming.*;
import javax.naming.spi.ObjectFactory;
import javax.sql.DataSource;

import com.zaxxer.hikari.util.PropertyElf;
import java.util.Hashtable;
import java.util.Properties;

/**
* A JNDI factory that produces HikariDataSource instances.
Expand All @@ -44,14 +36,14 @@ synchronized public Object getObjectInstance(Object obj, Name name, Context name
{
// We only know how to deal with <code>javax.naming.Reference</code> that specify a class name of "javax.sql.DataSource"
if (obj instanceof Reference && "javax.sql.DataSource".equals(((Reference) obj).getClassName())) {
Reference ref = (Reference) obj;
Set<String> hikariPropSet = PropertyElf.getPropertyNames(HikariConfig.class);
var ref = (Reference) obj;
var hikariPropSet = PropertyElf.getPropertyNames(HikariConfig.class);

Properties properties = new Properties();
Enumeration<RefAddr> enumeration = ref.getAll();
var properties = new Properties();
var enumeration = ref.getAll();
while (enumeration.hasMoreElements()) {
RefAddr element = enumeration.nextElement();
String type = element.getType();
var element = enumeration.nextElement();
var type = element.getType();
if (type.startsWith("dataSource.") || hikariPropSet.contains(type)) {
properties.setProperty(type, element.getContent().toString());
}
Expand All @@ -63,7 +55,7 @@ synchronized public Object getObjectInstance(Object obj, Name name, Context name

private DataSource createDataSource(final Properties properties, final Context context) throws NamingException
{
String jndiName = properties.getProperty("dataSourceJNDI");
var jndiName = properties.getProperty("dataSourceJNDI");
if (jndiName != null) {
return lookupJndiDataSource(properties, context, jndiName);
}
Expand All @@ -77,15 +69,15 @@ private DataSource lookupJndiDataSource(final Properties properties, final Conte
throw new RuntimeException("JNDI context does not found for dataSourceJNDI : " + jndiName);
}

DataSource jndiDS = (DataSource) context.lookup(jndiName);
var jndiDS = (DataSource) context.lookup(jndiName);
if (jndiDS == null) {
final Context ic = new InitialContext();
final var ic = new InitialContext();
jndiDS = (DataSource) ic.lookup(jndiName);
ic.close();
}

if (jndiDS != null) {
HikariConfig config = new HikariConfig(properties);
var config = new HikariConfig(properties);
config.setDataSource(jndiDS);
return new HikariDataSource(config);
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/zaxxer/hikari/metrics/PoolStats.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public int getMinConnections() {
private boolean shouldLoad()
{
for (; ; ) {
final long now = currentTime();
final long reloadTime = reloadAt.get();
final var now = currentTime();
final var reloadTime = reloadAt.get();
if (reloadTime > now) {
return false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,18 @@ public final class CodahaleHealthChecker
*/
public static void registerHealthChecks(final HikariPool pool, final HikariConfig hikariConfig, final HealthCheckRegistry registry)
{
final Properties healthCheckProperties = hikariConfig.getHealthCheckProperties();
final MetricRegistry metricRegistry = (MetricRegistry) hikariConfig.getMetricRegistry();
final var healthCheckProperties = hikariConfig.getHealthCheckProperties();
final var metricRegistry = (MetricRegistry) hikariConfig.getMetricRegistry();

final long checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs", String.valueOf(hikariConfig.getConnectionTimeout())));
final var checkTimeoutMs = Long.parseLong(healthCheckProperties.getProperty("connectivityCheckTimeoutMs", String.valueOf(hikariConfig.getConnectionTimeout())));
registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "ConnectivityCheck"), new ConnectivityHealthCheck(pool, checkTimeoutMs));

final long expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty("expected99thPercentileMs", "0"));
final var expected99thPercentile = Long.parseLong(healthCheckProperties.getProperty("expected99thPercentileMs", "0"));
if (metricRegistry != null && expected99thPercentile > 0) {
SortedMap<String,Timer> timers = metricRegistry.getTimers((name, metric) -> name.equals(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Wait")));
var timers = metricRegistry.getTimers((name, metric) -> name.equals(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Wait")));

if (!timers.isEmpty()) {
final Timer timer = timers.entrySet().iterator().next().getValue();
final var timer = timers.entrySet().iterator().next().getValue();
registry.register(MetricRegistry.name(hikariConfig.getPoolName(), "pool", "Connection99Percent"), new Connection99Percent(timer, expected99thPercentile));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* };
* </pre></blockquote>
*/
@SuppressWarnings("ALL")
public class MicrometerMetricsTracker implements IMetricsTracker
{
/** Prefix used for all HikariCP metric names. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void remove(String name)
private GaugeMetricFamily createGauge(String metric, String help,
Function<PoolStats, Integer> metricValueFunction)
{
GaugeMetricFamily metricFamily = new GaugeMetricFamily(metric, help, LABEL_NAMES);
var metricFamily = new GaugeMetricFamily(metric, help, LABEL_NAMES);
poolStatsMap.forEach((k, v) -> metricFamily.addMetric(
Collections.singletonList(k),
metricValueFunction.apply(v)
Expand Down
Loading

0 comments on commit 1991355

Please sign in to comment.