All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.cattleframework.db.datasource.reflect.invocation.DataSourceInvocationHandler Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2018 the original author or authors.
 *
 * 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 org.cattleframework.db.datasource.reflect.invocation;

import java.lang.reflect.Method;
import java.sql.Connection;

import javax.sql.DataSource;

import org.cattleframework.db.datasource.DataSourceConstants;
import org.cattleframework.db.datasource.reflect.ConnectionProxy;
import org.cattleframework.utils.reflect.ReflectUtils;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.reflect.AbstractInvocationHandler;

/**
 * 数据库数据源调用处理
 * 
 * @author orange
 *
 */
public class DataSourceInvocationHandler extends AbstractInvocationHandler {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static final String METHOD_NAME_GET_TARGET_DATA_SOURCE = "getTargetDataSource";

    private static final String METHOD_NAME_GET_CONNECTION = "getConnection";

    private final DataSource target;

    private boolean logDebug = false;

    private boolean logInfo = false;

    public DataSourceInvocationHandler(DataSource target) {
	this.target = target;
	if (logger.isDebugEnabled()) {
	    logDebug = true;
	} else if (logger.isInfoEnabled()) {
	    logInfo = true;
	}
    }

    private void log(String format, Object... arguments) {
	if (logDebug) {
	    logger.debug(format, arguments);
	} else if (logInfo) {
	    logger.info(format, arguments);
	}
    }

    @Override
    protected Object handleInvocation(Object proxy, Method method, @Nullable Object[] args) throws Throwable {
	String methodName = method.getName();
	if (DataSourceConstants.METHOD_NAME_UNWRAP.equals(methodName)) {
	    if (((Class) args[0]).isInstance(proxy)) {
		return proxy;
	    }
	} else if (DataSourceConstants.METHOD_NAME_IS_WRAPPER_FOR.equals(methodName)) {
	    if (((Class) args[0]).isInstance(proxy)) {
		return true;
	    }
	} else if (METHOD_NAME_GET_TARGET_DATA_SOURCE.equals(methodName)) {
	    return target;
	} else if (METHOD_NAME_GET_CONNECTION.equals(methodName)) {
	    return getConnection(method, args);
	}
	return ReflectUtils.invokeObjectMethod(method, target, args);
    }

    private Connection getConnection(Method method, Object[] args) throws Throwable {
	long startTime = System.currentTimeMillis();
	Connection conn = ConnectionProxy
		.getConnectionProxy((Connection) ReflectUtils.invokeObjectMethod(method, target, args));
	long endTime = System.currentTimeMillis();
	log("获得数据库连接,执行时间:{}毫秒", (endTime - startTime));
	return conn;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy