📄 methodinvokerutil.java
字号:
/*
* Copyright 2003-2005 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.aop.reflection;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.rmi.Remote;
import javax.ejb.EJBObject;
import javax.rmi.PortableRemoteObject;
import org.aopalliance.aop.AspectException;
import com.jdon.bussinessproxy.target.TargetServiceFactory;
import com.jdon.container.access.TargetMetaRequest;
import com.jdon.container.finder.ComponentKeys;
import com.jdon.container.visitor.ComponentVisitor;
import com.jdon.util.Debug;
/**
* tools for method invoke
* @author <a href="mailto:banqiao@jdon.com">banq </a>
*
*/
public class MethodInvokerUtil {
private final static String module = MethodInvokerUtil.class.getName();
/**
* the service execute by method reflection
*
* @param method
* @param targetObj
* @param p_args
* @return
* @throws Throwable
*/
public Object execute(Method method, Object targetObj, Object[] p_args) throws Throwable {
try {
if ((method == null) || (targetObj == null))
Debug.logError(" no method or target, please check your configure", module);
if (p_args == null)
p_args = new Object[0];
Object result = method.invoke(targetObj, p_args);
Debug.logVerbose(" method invoke successfully ", module);
return result;
} catch (IllegalArgumentException iex) {
Debug.logError(" method invoke IllegalArgumentException: " + iex, module);
throw new Throwable(" check your interceptor : ", iex);
} catch (InvocationTargetException ex) {
String errorInfo = " method "+ method.getName() +" run error: " + ex.getTargetException() + " check your method's code! or use try..catch ";
Debug.logError(errorInfo, module);
throw new Throwable(errorInfo);
} catch (IllegalAccessException ex) {
Debug.logError(" method invoke IllegalAccessException: " + ex, module);
throw new Throwable("access method:" + method + " " + ex, ex);
} catch (Exception ex) {
Debug.logError(" method invoke error: " + ex, module);
throw new Throwable(" method invoke error: " + ex);
}
}
/**
* if target service is ejb object, cache it,
* so this function can active stateful session bean.
*
* @param targetServiceFactory
* @param targetMetaDef
* @return
* @throws Exception
*/
public Object createTargetObject(TargetServiceFactory targetServiceFactory, TargetMetaRequest targetMetaRequest) {
Debug.logVerbose(" now getTargetObject by visitor ", module);
Object targetObjRef = null;
try {
if (targetMetaRequest.getTargetMetaDef().isEJB()) { //cache the ejb object
ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
targetMetaRequest.setVisitableName(ComponentKeys.TARGETSERVICE_FACTORY);
Debug.logVerbose(ComponentKeys.TARGETSERVICE_FACTORY + " in action (cache)", module);
targetObjRef = cm.visit(targetMetaRequest);
} else {
Debug.logVerbose(" not active targer service instance cache !!!!", module);
targetObjRef = targetServiceFactory.create(targetMetaRequest.getTargetMetaDef());
}
} catch (Exception e) {
Debug.logError("createTargetObject error: " + e, module);
}
return targetObjRef;
}
/**
* 如果参数中有remote EJB,需要从远程序列化过来这些参数
*
*/
public Object[] narrowArgs(Object[] p_args) {
if (p_args == null)
return null;
int length = p_args.length;
Object[] result = new Object[length];
for (int i = 0; i < length; i++) {
if (p_args[i] instanceof Remote)
result[i] = PortableRemoteObject.narrow(p_args[i], EJBObject.class);
else
result[i] = p_args[i];
}
return result;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -