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

net.sf.hajdbc.sql.AbstractInvocationHandler Maven / Gradle / Ivy

There is a newer version: 3.6.61
Show newest version
/*
 * HA-JDBC: High-Availability JDBC
 * Copyright (C) 2013  Paul Ferraro
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 */
package net.sf.hajdbc.sql;

import net.sf.hajdbc.Database;
import net.sf.hajdbc.DatabaseCluster;
import net.sf.hajdbc.ExceptionFactory;
import net.sf.hajdbc.Messages;
import net.sf.hajdbc.invocation.*;
import net.sf.hajdbc.logging.Level;
import net.sf.hajdbc.logging.Logger;
import net.sf.hajdbc.logging.LoggerFactory;
import net.sf.hajdbc.sql.serial.SerialLocatorFactories;
import net.sf.hajdbc.sql.serial.SerialLocatorFactory;
import net.sf.hajdbc.state.health.ClusterHealth;
import net.sf.hajdbc.util.StopWatch;
import net.sf.hajdbc.util.Tracer;
import net.sf.hajdbc.util.reflect.Methods;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.SQLException;
import java.sql.Wrapper;
import java.util.*;

/**
 * 
 * @author Paul Ferraro
 */
public class AbstractInvocationHandler, T, E extends Exception, F extends ProxyFactory> implements InvocationHandler
{
	private static final Method equalsMethod = Methods.getMethod(Object.class, "equals", Object.class);
	private static final Method hashCodeMethod = Methods.getMethod(Object.class, "hashCode");
	private static final Method toStringMethod = Methods.getMethod(Object.class, "toString");
	private static final Set wrapperMethods = Methods.findMethods(Wrapper.class, "isWrapperFor", "unwrap");
	protected final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final Class proxyClass;
	private final F proxyFactory;
	
	protected AbstractInvocationHandler(Class targetClass, F proxyFactory)
	{
		this.proxyClass = targetClass;
		this.proxyFactory = proxyFactory;
	}
	
	@Override
	public F getProxyFactory()
	{
		return this.proxyFactory;
	}
	
	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
	{
		DatabaseCluster cluster = this.proxyFactory.getDatabaseCluster();

		if (!cluster.isActive())
		{
			throw new SQLException(Messages.CLUSTER_NOT_ACTIVE.getMessage(cluster));
		}
		
		return this.invokeOnProxy(this.proxyClass.cast(proxy), method, args);
	}

	private  R invokeOnProxy(T proxy, Method method, Object... parameters) throws E
	{
		InvocationStrategy strategy = this.getInvocationStrategy(proxy, method, parameters);

		Invoker invoker = this.getInvoker(proxy, method, parameters);

		//this.logger.log(Level.INFO, "Invoking {0} using {1}", method, strategy);
		StopWatch stopWatch = StopWatch.createStarted();
		SortedMap results = strategy.invoke(this.proxyFactory, invoker);

		if(Tracer.invoke.isTrace()) {
			List ps = new ArrayList<>();
			if(parameters!=null){
				for (Object parameter : parameters) {
					ps.add(""+parameter);
				}
			}
			this.logger.log(Level.INFO, "cost {0} Invoked {1} using {2} ps={3} results={4}", stopWatch, method, strategy, ps, results);
		}
		this.postInvoke(invoker, proxy, method, parameters);
		
		@SuppressWarnings("unchecked")
		ProxyFactoryFactory factory = (ProxyFactoryFactory) this.getProxyFactoryFactory(proxy, method, parameters);
		InvocationResultFactory resultFactory = (factory != null) ? new ProxyInvocationResultFactory(factory, proxy, this.getProxyFactory(), invoker) : new SimpleInvocationResultFactory();
		if(isAllInvoke(strategy)){
			DatabaseCluster cluster = this.proxyFactory.getDatabaseCluster();
			ClusterHealth clusterHealth = cluster.getClusterHealth();
			if(clusterHealth!=null&&clusterHealth.isHost()){
				clusterHealth.incrementToken();
			}
		}

		return this.createResult(resultFactory, results);
	}

	private boolean isAllInvoke(InvocationStrategy strategy) {
		return //InvocationStrategies.INVOKE_ON_ALL.equals(strategy)
				//||InvocationStrategies.TRANSACTION_INVOKE_ON_ALL.equals(strategy)
				InvocationStrategies.END_TRANSACTION_INVOKE_ON_ALL.equals(strategy);
	}

	/**
	 * @throws E 
	 */
	protected ProxyFactoryFactory getProxyFactoryFactory(T object, Method method, Object... parameters) throws E
	{
		return null;
	}
	
	/**
	 * Returns the appropriate {@link InvocationStrategy} for the specified method.
	 * This implementation detects {@link java.sql.Wrapper} methods; and {@link Object#equals}, {@link Object#hashCode()}, and {@link Object#toString()}.
	 * Default invocation strategy is {@link AllResultsCollector}. 
	 * @param object the proxied object
	 * @param method the method to invoke
	 * @param parameters the method invocation parameters
	 * @return an invocation strategy
	 * @throws E
	 */
	protected InvocationStrategy getInvocationStrategy(T object, Method method, Object... parameters) throws E
	{
		if (equalsMethod.equals(method) || hashCodeMethod.equals(method) || toStringMethod.equals(method) || wrapperMethods.contains(method))
		{
			return InvocationStrategies.INVOKE_ON_ANY;
		}

		return InvocationStrategies.INVOKE_ON_ALL;
	}
	
	/**
	 * Return the appropriate invoker for the specified method.
	 * @param proxy
	 * @param method
	 * @param parameters
	 * @return an invoker
	 * @throws Exception
	 */
	protected  Invoker getInvoker(T proxy, Method method, Object... parameters) throws E
	{
		return this.getInvoker(method, parameters);
	}

	/**
	 * @throws E  
	 */
	private  Invoker getInvoker(Method method, Object... parameters) throws E
	{
		return new SimpleInvoker(method, parameters, this.proxyFactory.getExceptionFactory());
	}
	
	protected  Invoker getInvoker(Class parameterClass, final int parameterIndex, T proxy, final Method method, final Object... parameters) throws E
	{
		if (parameterClass.equals(method.getParameterTypes()[parameterIndex]) && !parameterClass.isPrimitive())
		{
			X parameter = parameterClass.cast(parameters[parameterIndex]);
			
			if (parameter != null)
			{
				final ExceptionFactory exceptionFactory = this.getProxyFactory().getExceptionFactory();
				
				// Handle proxy parameter
				if (Proxy.isProxyClass(parameter.getClass()) && (Proxy.getInvocationHandler(parameter) instanceof InvocationHandler))
				{
					final InvocationHandler> handler = (InvocationHandler>) Proxy.getInvocationHandler(parameter);
					
					return new Invoker()
					{
						@Override
						public R invoke(D database, T object) throws E
						{
							List parameterList = new ArrayList(Arrays.asList(parameters));
							
							parameterList.set(parameterIndex, handler.getProxyFactory().get(database));
							
							return Methods.invoke(method, exceptionFactory, object, parameterList.toArray());
						}
					};
				}
				
				SerialLocatorFactory factory = SerialLocatorFactories.find(parameterClass);
				if (factory != null)
				{
					try
					{
						// Create a serial form of the parameter, so it can be used by each database
						parameters[parameterIndex] = factory.createSerial(parameter);
					}
					catch (SQLException e)
					{
						throw exceptionFactory.createException(e);
					}
				}
			}
		}
		
		return this.getInvoker(method, parameters);
	}
	
	private  R createResult(InvocationResultFactory factory, SortedMap resultMap) throws E
	{
		DatabaseCluster cluster = this.proxyFactory.getDatabaseCluster();
		
		if (resultMap.isEmpty())
		{
			throw this.proxyFactory.getExceptionFactory().createException(Messages.NO_ACTIVE_DATABASES.getMessage(cluster));
		}
		
		Iterator> results = resultMap.entrySet().iterator();
		R primaryResult = results.next().getValue();
		
		while (results.hasNext())
		{
			Map.Entry entry = results.next();
			R result = entry.getValue();
			
			if (factory.differs(primaryResult, result))
			{
				results.remove();
				D database = entry.getKey();
				
				if (cluster.deactivate(database, cluster.getStateManager()))
				{
					this.logger.log(Level.ERROR, Messages.DATABASE_INCONSISTENT.getMessage(), database, cluster, primaryResult, result);
				}
			}
		}
		
		return (primaryResult != null) ? factory.createResult(resultMap) : null;
	}

	protected  void postInvoke(Invoker invoker, T proxy, Method method, Object... parameters)
	{
		// Do nothing
	}
}