public HikariConfigExt(Properties properties) { super(); Enumeration<?> propertyNames = properties.propertyNames(); while (propertyNames.hasMoreElements()) { Object key = propertyNames.nextElement(); String propName = key.toString(); Object propValue = properties.getProperty(propName); if (propValue == null) { propValue = properties.get(key); } if (propName.startsWith("dataSource.")) { this.addDataSourceProperty(propName.substring("dataSource.".length()), propValue); } else { try { Properties tmp = new Properties(); tmp.setProperty(propName, propValue.toString()); PropertyElf.setTargetFromProperties(this, tmp); } catch(RuntimeException err) { // skip error for non existed properties } } } }
private void logConfiguration() { LOGGER.debug("{} - configuration:", poolName); final Set<String> propertyNames = new TreeSet<>(PropertyElf.getPropertyNames(HikariConfig.class)); for (String prop : propertyNames) { try { Object value = PropertyElf.getProperty(prop, this); if ("dataSourceProperties".equals(prop)) { Properties dsProps = PropertyElf.copyProperties(dataSourceProperties); dsProps.setProperty("password", "<masked>"); value = dsProps; } value = (prop.contains("password") ? "<masked>" : value); LOGGER.debug((prop + "................................................").substring(0, 32) + (value != null ? value : "")); } catch (Exception e) { continue; } } }
protected void loadProperties(String propertyFileName) { final File propFile = new File(propertyFileName); try (final InputStream is = propFile.isFile() ? new FileInputStream(propFile) : this.getClass().getResourceAsStream(propertyFileName)) { if (is != null) { Properties props = new Properties(); props.load(is); PropertyElf.setTargetFromProperties(this, props); } else { throw new IllegalArgumentException("Property file " + propertyFileName + " was not found."); } } catch (IOException io) { throw new RuntimeException("Error loading properties file", io); } }
@Override public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { // We only know how to deal with <code>javax.naming.Reference</code> that specify a class name of "javax.sql.DataSource" if ((obj == null) || !(obj instanceof Reference)) { return null; } Reference ref = (Reference) obj; if (!"javax.sql.DataSource".equals(ref.getClassName())) { throw new NamingException(ref.getClassName() + " is not a valid class name/type for this JNDI factory."); } Set<String> hikariPropSet = PropertyElf.getPropertyNames(HikariConfig.class); Properties properties = new Properties(); Enumeration<RefAddr> enumeration = ref.getAll(); while (enumeration.hasMoreElements()) { RefAddr element = enumeration.nextElement(); String type = element.getType(); if (type.startsWith("dataSource.") || hikariPropSet.contains(type)) { properties.setProperty(type, element.getContent().toString()); } } return createDataSource(properties, nameCtx); }
@Test public void testProperty2() throws Exception { Properties propfile2 = new Properties(); propfile2.load(TestPropertySetter.class.getResourceAsStream("/propfile2.properties")); HikariConfig config = new HikariConfig(propfile2); config.validate(); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); DataSource dataSource = (DataSource) clazz.newInstance(); PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); }
@Test public void testObjectProperty() throws Exception { HikariConfig config = new HikariConfig(); config.setDataSourceClassName("com.zaxxer.hikari.mocks.StubDataSource"); PrintWriter writer = new PrintWriter(new ByteArrayOutputStream()); config.addDataSourceProperty("logWriter", writer); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); DataSource dataSource = (DataSource) clazz.newInstance(); PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); Assert.assertSame(PrintWriter.class, dataSource.getLogWriter().getClass()); }
@Test public void testPropertyUpperCase() throws Exception { Properties propfile3 = new Properties(); propfile3.load(TestPropertySetter.class.getResourceAsStream("/propfile3.properties")); HikariConfig config = new HikariConfig(propfile3); config.validate(); Class<?> clazz = this.getClass().getClassLoader().loadClass(config.getDataSourceClassName()); DataSource dataSource = (DataSource) clazz.newInstance(); PropertyElf.setTargetFromProperties(dataSource, config.getDataSourceProperties()); }
@Test public void testSetNonExistantPropertyName() throws Exception { try { Properties props = new Properties(); props.put("what", "happened"); PropertyElf.setTargetFromProperties(new HikariConfig(), props); Assert.fail(); } catch (RuntimeException e) { } }
/** * Construct a HikariPool with the specified configuration. * * @param config a HikariConfig instance */ public HikariPool(final HikariConfig config) { this.config = config; this.poolElf = new PoolElf(config); this.dataSource = poolElf.initializeDataSource(); this.poolName = config.getPoolName(); this.connectionBag = new ConcurrentBag<>(this); this.totalConnections = new AtomicInteger(); this.connectionTimeout = config.getConnectionTimeout(); this.lastConnectionFailure = new AtomicReference<>(); this.suspendResumeLock = config.isAllowPoolSuspension() ? new SuspendResumeLock(true) : SuspendResumeLock.FAUX_LOCK; this.addConnectionExecutor = createThreadPoolExecutor(config.getMaximumPoolSize(), "Hikari connection filler (pool " + poolName + ")", config.getThreadFactory(), new ThreadPoolExecutor.DiscardPolicy()); this.closeConnectionExecutor = createThreadPoolExecutor(4, "Hikari connection closer (pool " + poolName + ")", config.getThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); if (config.getScheduledExecutorService() == null) { ThreadFactory threadFactory = config.getThreadFactory() != null ? config.getThreadFactory() : new DefaultThreadFactory("Hikari housekeeper (pool " + poolName + ")", true); this.houseKeepingExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory, new ThreadPoolExecutor.DiscardPolicy()); this.houseKeepingExecutorService.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); this.houseKeepingExecutorService.setRemoveOnCancelPolicy(true); } else { this.houseKeepingExecutorService = config.getScheduledExecutorService(); } this.houseKeepingExecutorService.scheduleAtFixedRate(new HouseKeeper(), HOUSEKEEPING_PERIOD_MS, HOUSEKEEPING_PERIOD_MS, TimeUnit.MILLISECONDS); this.leakTask = new LeakTask(config.getLeakDetectionThreshold(), houseKeepingExecutorService); if (config.getMetricsTrackerFactory() != null) { setMetricsTrackerFactory(config.getMetricsTrackerFactory()); } else { setMetricRegistry(config.getMetricRegistry()); } setHealthCheckRegistry(config.getHealthCheckRegistry()); poolElf.registerMBeans(this); PropertyElf.flushCaches(); initializeConnections(); }
/** * Construct a HikariConfig from the specified properties object. * * @param properties the name of the property file */ public HikariConfig(Properties properties) { this(); PropertyElf.setTargetFromProperties(this, properties); }
@Test public void testGetPropertyNames() throws Exception { Set<String> propertyNames = PropertyElf.getPropertyNames(HikariConfig.class); Assert.assertTrue(propertyNames.contains("dataSourceClassName")); }