com.zaxxer.hikari.util.PropertyElf Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HikariCP-java7 Show documentation
Show all versions of HikariCP-java7 Show documentation
Ultimate JDBC Connection Pool
/*
* Copyright (C) 2013 Brett Wooldridge
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.zaxxer.hikari.util;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zaxxer.hikari.HikariConfig;
/**
* A class that reflectively sets bean properties on a target object.
*
* @author Brett Wooldridge
*/
public final class PropertyElf
{
private static final Logger LOGGER = LoggerFactory.getLogger(PropertyElf.class);
private static final Pattern GETTER_PATTERN = Pattern.compile("(get|is)[A-Z].+");
public static void setTargetFromProperties(final Object target, final Properties properties)
{
if (target == null || properties == null) {
return;
}
List methods = Arrays.asList(target.getClass().getMethods());
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 (target instanceof HikariConfig && propName.startsWith("dataSource.")) {
((HikariConfig) target).addDataSourceProperty(propName.substring("dataSource.".length()), propValue);
}
else {
setProperty(target, propName, propValue, methods);
}
}
}
/**
* Get the bean-style property names for the specified object.
*
* @param targetClass the target object
* @return a set of property names
*/
public static Set getPropertyNames(final Class> targetClass)
{
HashSet set = new HashSet<>();
Matcher matcher = GETTER_PATTERN.matcher("");
for (Method method : targetClass.getMethods()) {
String name = method.getName();
if (method.getParameterTypes().length == 0 && matcher.reset(name).matches()) {
name = name.replaceFirst("(get|is)", "");
try {
if (targetClass.getMethod("set" + name, method.getReturnType()) != null) {
name = Character.toLowerCase(name.charAt(0)) + name.substring(1);
set.add(name);
}
}
catch (Exception e) {
continue;
}
}
}
return set;
}
public static Object getProperty(final String propName, final Object target)
{
try {
// use the english locale to avoid the infamous turkish locale bug
String capitalized = "get" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1);
Method method = target.getClass().getMethod(capitalized);
return method.invoke(target);
}
catch (Exception e) {
try {
String capitalized = "is" + propName.substring(0, 1).toUpperCase(Locale.ENGLISH) + propName.substring(1);
Method method = target.getClass().getMethod(capitalized);
return method.invoke(target);
}
catch (Exception e2) {
return null;
}
}
}
public static Properties copyProperties(final Properties props)
{
Properties copy = new Properties();
for (Map.Entry