java反射机制实现Log出力,事务处理

来源:互联网 发布:paint it black 知乎 编辑:程序博客网 时间:2024/05/09 14:21

ServiceProxyFactory.java
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;
import java.util.HashMap;
import java.util.Map;

import jp.co.mg.mgstruts.exception.ProcessingException;

public class ServiceProxyFactory {
 
 private static final int NON_TRANSACTION = 6;
 private static final int TRANSACTION = 3;
 
 private Class targetClass;
 private static Map cache = new HashMap();
 
 public ServiceProxyFactory(Class targetClass) {
  this.targetClass = targetClass;
 }

 public synchronized Object getProxy(int transactionType)
   throws ProcessingException {
  Object target = cache.get(targetClass);
  if (target == null) {
   try {
    target = targetClass.newInstance();
   } catch (InstantiationException e) {
    throw new ProcessingException("CICORESE085");
   } catch (IllegalAccessException e) {
    throw new ProcessingException("CICORESE085");
   }
   cache.put(targetClass, target);
  }
  InvocationHandler handler = new ServiceProxyHandler(target, transactionType);
  return Proxy.newProxyInstance(target.getClass().getClassLoader(),
    target.getClass().getInterfaces(), handler);
 }

 public Object getTransactionalProxy() throws ProcessingException {
  return getProxy(TRANSACTION);
 }

 public Object getNonTransactionalProxy() throws ProcessingException {
  return getProxy(NON_TRANSACTION);
 }
}

 

ServiceProxyHandler.java
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;

public class ServiceProxyHandler implements InvocationHandler {

 Logger log = Logger.getLogger(ServiceProxyHandler.class.getName());
 
 private final Object impl;
 private int transactionType;
 private Map methodNameCache;

 public ServiceProxyHandler(Object impl, int transactionType) {
  methodNameCache = new HashMap();
  this.impl = impl;
  this.transactionType = transactionType;
 }

 public Object invoke(Object proxy, Method method, Object[] args)
   throws Throwable {
  
  // Log Start.
  logStart(method, args);
  // TODO Transaction Start.
  System.out.println("Transaction Start!");
  Object obj;
  try {
   obj = method.invoke(impl, args);
  } catch (Throwable t) {
   
   // TODO Transaction Error.
   System.out.println("Transaction Error!");
   // TODO Log Error.
   System.out.println("Log Error!");
   log.error("Log Start!");
   throw t;
  } finally {

  }
  // TODO Transaction End.
  System.out.println("Transaction End!");
  // Log End.
  logEnd(method, args);
  return obj;
 }
 
 private void logStart(Method method, Object[] args) {
  log.info("METHOD [" + method.getName() + "] START");
  if (!log.isDebugEnabled() || args == null) {
   return;
  }
 
  log.debug("METHOD [" + method.getName() + "] PARAM:");
 
  StringBuffer buff = new StringBuffer();

  for (Object obj : args) {
   LogHelper.dumpData(obj, buff);
  }
  log.debug(buff.toString());
 }
 
 private void logEnd(Method method, Object args[]) {
  log.info("METHOD [" + method.getName() + "] END");
 }
}