📄 proxyinstancefactoryvisitable.java
字号:
/*
* Copyright 2003-2006 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 com.jdon.bussinessproxy.dyncproxy;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;
import com.jdon.aop.AopClient;
import com.jdon.bussinessproxy.TargetMetaDef;
import com.jdon.bussinessproxy.meta.DistributedTargetMetaDef;
import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.visitor.Visitable;
import com.jdon.util.Debug;
/**
* by using Proxy.newProxyInstance, create a DynamicProxyWeaving for
* a target service.
*
* @todo: create a DynamicProxyWeaving for all services.
*
* @author <a href="mailto:banqiao@jdon.com">banq</a>
*
*/
public class ProxyInstanceFactoryVisitable implements Visitable{
private final static String module = ProxyInstanceFactoryVisitable.class.getName();
private AopClient aopClient;
/**
* @param aopClient
*/
public ProxyInstanceFactoryVisitable(AopClient aopClient) {
super();
this.aopClient = aopClient;
}
/**
*
*/
public Object accept(TargetMetaRequest targetMetaRequest) {
Debug.logVerbose("[JdonFramework]enter Proxy.newProxyInstance ", module);
Object dynamicProxy = null;
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
DynamicProxyWeaving dynamicProxyWeaving = new DynamicProxyWeaving(targetMetaRequest, aopClient);
dynamicProxy =
Proxy.newProxyInstance(
classLoader,
getInterfaces(targetMetaRequest.getTargetMetaDef()),
dynamicProxyWeaving);
} catch (Exception ex) {
Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module);
} catch (Throwable ex) {
Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module);
}
return dynamicProxy;
}
/**
* get the interface of target class
* if it is EJB, it is ejb local/remote interface
* if it is pojo, it is a class .
*
*
* @param targetMetaDef
* @return
*/
private Class[] getInterfaces(TargetMetaDef targetMetaDef){
Class[] interfaces = targetMetaDef.getInterfaces();
if (interfaces != null) return interfaces;
try {
if (targetMetaDef.isEJB()){
if (targetMetaDef instanceof EJBTargetMetaDef)
interfaces = getEJB2Interfaces(targetMetaDef);
else if (targetMetaDef instanceof DistributedTargetMetaDef)
interfaces = getEJB3Interfaces(targetMetaDef);
}else{
interfaces = getPOJOInterfaces(targetMetaDef);
}
} catch (Exception ex) {
Debug.logError("[JdonFramework] getInterfaces error:" + ex, module);
} catch (Throwable ex) {
Debug.logError("[JdonFramework] getInterfaces error:" + ex, module);
}
if ((interfaces == null) || (interfaces.length == 0)){
Debug.logError("[JdonFramework] no find any interface for the service:" + targetMetaDef.getClassName(), module);
}else{
targetMetaDef.setInterfaces(interfaces); //cache the result
Debug.logVerbose("[JdonFramework]found the the below interfaces for the service:"+ targetMetaDef.getClassName(), module);
for(int i=0; i<interfaces.length; i++){
Debug.logVerbose(interfaces[i].getName() + ";", module);
}
}
return interfaces;
}
private Class[] getEJB3Interfaces(TargetMetaDef targetMetaDef) throws Exception {
Class[] interfaces = null;
try {
DistributedTargetMetaDef dtargetMetaDef = (DistributedTargetMetaDef)targetMetaDef;
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Debug.logVerbose("[JdonFramework]getEJB3Interfaces interface="+ dtargetMetaDef.getInterfaceClass(), module);
interfaces = new Class[] {classLoader.loadClass(dtargetMetaDef.getInterfaceClass()) };
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return interfaces;
}
private Class[] getEJB2Interfaces(TargetMetaDef targetMetaDef) throws Exception {
Class[] interfaces = null;
Debug.logVerbose("[JdonFramework]getEJB2Interfaces "+ targetMetaDef.getClassName(), module);
try {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class configClasses = classLoader.loadClass(targetMetaDef.getClassName());
EJBTargetMetaDef em = (EJBTargetMetaDef) targetMetaDef;
if ((em.getInterfaceClass() != null)
&& (em.getInterfaceClass().length() != 0)) {
interfaces = new Class[] { configClasses, classLoader.loadClass(em.getInterfaceClass()) };
} else
interfaces = new Class[] { configClasses };
} catch (Exception e) {
e.printStackTrace();
throw new Exception();
}
return interfaces;
}
public Class[] getPOJOInterfaces(TargetMetaDef targetMetaDef) {
Class[] interfaces = null;
Debug.logVerbose("[JdonFramework]getPOJOInterfaces "+ targetMetaDef.getClassName(), module);
try {
List interfacesL = new ArrayList();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
Class pojoClass = classLoader.loadClass(targetMetaDef.getClassName());
while (pojoClass != null) {
for (int i = 0; i < pojoClass.getInterfaces().length; i++) {
Class ifc = pojoClass.getInterfaces()[i];
interfacesL.add(ifc);
}
pojoClass = pojoClass.getSuperclass();
}
if (interfacesL.size() == 0){
throw new Exception();
}
interfaces = (Class[]) interfacesL.toArray(new Class[interfacesL.size()]);
} catch (Exception e) {
Debug.logError("[JdonFramework] not found the interface of your pojoClass=" +
targetMetaDef.getClassName() +", error:" + e, module);
}
return interfaces;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -