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

com.alterioncorp.perfjdbc.PerfProxyFactory Maven / Gradle / Ivy

Go to download

Wrapper JDBC driver that monitors time spent inside JDBC code by thread. JDBC driver URL: jdbc:alterion:perf://class=<TARGET_DRIVER_CLASS>|url=<TARGET_JDBC_URL> To get the time in millis spent inside JDBC for thread: StopWatch.getTime()

The newest version!
package com.alterioncorp.perfjdbc;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.WeakHashMap;

import com.alterioncorp.perfjdbc.io.PerfInputStream;
import com.alterioncorp.perfjdbc.io.PerfOutputStream;
import com.alterioncorp.perfjdbc.io.PerfReader;
import com.alterioncorp.perfjdbc.io.PerfWriter;

public class PerfProxyFactory {

	static class PerfInvocationHandler implements InvocationHandler {
	
		private Object target;
	
		public PerfInvocationHandler(Object target) {
			super();
			this.target = target;
		}

		@Override
		public Object invoke(Object proxy, Method method, Object[] args)
				throws Throwable {
			
			Object retValue;

			StopWatch.start();
			try {
				retValue = method.invoke(target, args);
			}
			catch (InvocationTargetException e) {
				throw e.getTargetException();
			}
			finally {
				StopWatch.stop();
			}
			
			retValue = createProxyInternal(method.getReturnType(), retValue);

			return retValue;
		}
	};

	private static WeakHashMap, Constructor> proxyConstructorCache =
			new WeakHashMap, Constructor>(64);

	public static  T createProxy(final Class type, final T target) {
		return createProxyInternal(type, target);
	}
	
	@SuppressWarnings("unchecked")
	private static  T createProxyInternal(final Class type, final Object target) {
		
		T proxy = (T)target;
		
		if (target != null && ! (target instanceof PerfProxy)) {
			if (isJdbcInterface(type)) {
				proxy = createProxyForInterface(type, target);
			}
			else if (type == InputStream.class) {
				proxy = (T)new PerfInputStream((InputStream)target);
			}
			else if (type == OutputStream.class) {
				proxy = (T)new PerfOutputStream((OutputStream)target);
			}
			else if (type == Reader.class) {
				proxy = (T)new PerfReader((Reader)target);
			}
			else if (type == Writer.class) {
				proxy = (T)new PerfWriter((Writer)target);
			}
		}
		
		return proxy;
	}
	
	@SuppressWarnings("unchecked")
	private static  T createProxyForInterface(final Class type, final Object target) {
		
		try {
			Constructor proxyConstructor = proxyConstructorCache.get(type);
			if (proxyConstructor == null) {
				synchronized (proxyConstructorCache) {
					proxyConstructor = proxyConstructorCache.get(type);
					if (proxyConstructor == null) {
						proxyConstructor = Proxy.getProxyClass(
								PerfProxyFactory.class.getClassLoader(),
								new Class[] {type, PerfProxy.class})
							.getConstructor(new Class[] {InvocationHandler.class});
						
						proxyConstructorCache.put(type, proxyConstructor);
					}
				}
			}
			
			return (T)proxyConstructor.newInstance(new Object[] {new PerfInvocationHandler(target)});
		}
		catch (RuntimeException e) {
			throw e;
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}			

	}
	
	private static boolean isJdbcInterface(Class type) {
		return
				type.isInterface() && (
					type.getPackage().getName().startsWith("java.sql") ||
					type.getPackage().getName().startsWith("javax.sql"));
	}
	
	private PerfProxyFactory() {
		super();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy