📄 processfactory.java
字号:
package cn.myapps.util;
import java.lang.reflect.Method;
import java.security.AccessControlException;
import javax.security.auth.Subject;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import cn.myapps.base.dao.IBaseDAO;
import cn.myapps.base.dao.PersistenceUtils;
import cn.myapps.base.dao.SessionSignal;
import cn.myapps.base.ejb.BaseProcess;
import cn.myapps.util.cache.CacheKey;
import cn.myapps.util.cache.ICacheProvider;
import cn.myapps.util.cache.IMyCache;
import cn.myapps.util.cache.IMyElement;
import cn.myapps.util.cache.MyCacheManager;
public class ProcessFactory implements MethodInterceptor {
private static ProcessFactory _application;
private static Log logger = LogFactory.getLog(ProcessFactory.class);
private Enhancer enhancer = new Enhancer();
private Subject subject = null;
private ProcessFactory() {
}
public static ProcessFactory getInstance() {
if (_application == null) {
_application = new ProcessFactory();
}
return _application;
}
/**
* Create the relate process
*
* @param subject
* The subject
* @param iProcessClazz
* The process interface class
* @return The process object.
* @throws ClassNotFoundException
*/
public static BaseProcess createProcess(Subject subject, Class iProcessClazz)
throws ClassNotFoundException {
ProcessFactory factory = new ProcessFactory();
factory.subject = subject;
Class[] iclzs = new Class[] { iProcessClazz };
String cn = iProcessClazz.getName();
cn += cn + "Bean";
Class clz = Class.forName(cn);
BaseProcess process = (BaseProcess) factory.getInstrumentedClass(iclzs,
clz);
return process;
}
/**
* Create the relate process
*
* @param iProcessClazz
* The process interface class
* @return The process object.
* @throws ClassNotFoundException
*/
public static BaseProcess createProcess(Class iProcessClazz)
throws ClassNotFoundException {
ProcessFactory factory = new ProcessFactory();
Class[] iclzs = new Class[] { iProcessClazz };
String cn = iProcessClazz.getName();
cn += "Bean";
Class clz = Class.forName(cn);
BaseProcess process = (BaseProcess) factory.getInstrumentedClass(iclzs,
clz);
return process;
}
public static BaseProcess createProcess(String clazzName)
throws ClassNotFoundException {
ProcessFactory factory = new ProcessFactory();
Class iProcessClazz = Class.forName(clazzName);
Class[] iclzs = new Class[] { iProcessClazz };
String cn = iProcessClazz.getName();
cn += "Bean";
Class clz = Class.forName(cn);
BaseProcess process = (BaseProcess) factory.getInstrumentedClass(iclzs,
clz);
return process;
}
/**
* Get the instrumented class.
*
* @param iclzs
* The interfaces array.
* @param clz
* The super class
* @return The instrumented class.
*/
public Object getInstrumentedClass(Class[] iclzs, Class clz) {
enhancer.setInterfaces(iclzs);
enhancer.setSuperclass(clz);
enhancer.setCallback((Callback) this);
return enhancer.create();
}
/*
* (non-Javadoc)
*
* @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object,
* java.lang.reflect.Method, java.lang.Object[],
* net.sf.cglib.proxy.MethodProxy)
*/
public Object intercept(Object o, Method method, Object[] methodParameters,
MethodProxy methodProxy) throws Throwable {
if (method.getName().equals("finalize")) {
return methodProxy.invokeSuper(o, methodParameters);
}
// SessionSignal signal = PersistenceUtils.getSessionSignal();
boolean chechedMethod = false;
Object result = null;
try {
// signal.sessionSignal++;
Class methodClazz = method.getClass();
if (!methodClazz.equals(Object.class)) {
String cacheName = MyCacheManager.buildCacheKeyString(method);
ICacheProvider provider = MyCacheManager.getProviderInstance();
if (provider != null) {
IMyCache cache = provider.getCache(cacheName);
if (cache != null) {
chechedMethod = true;
CacheKey cacheKey = new CacheKey(o, method,
methodParameters);
IMyElement cachedElement = (IMyElement) cache
.get(cacheKey);
if (cachedElement != null
&& cachedElement.getValue() != null) {
System.out
.println("@@CACHED-METHOD-->>" + cacheKey);
result = cachedElement.getValue();
} else {
result = methodProxy.invokeSuper(o,
methodParameters);
cache.put(cacheKey, result);
}
}
}
}
if (!chechedMethod) {
result = methodProxy.invokeSuper(o, methodParameters);
}
return result;
} catch (Throwable t) {
throw t;
} finally {
// signal.sessionSignal--;
// if (signal.sessionSignal <= 0) {
// PersistenceUtils.closeSession();
// }
}
}
/**
* Check the permission.
*
* @throws AccessControlException
*/
private void checkPermission() throws AccessControlException {
throw new AccessControlException(null);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -