KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > aop > reflection > MethodInvokerUtil


1 /*
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */

16 package com.jdon.aop.reflection;
17
18 import java.lang.reflect.InvocationTargetException JavaDoc;
19 import java.lang.reflect.Method JavaDoc;
20 import java.rmi.Remote JavaDoc;
21
22 import javax.ejb.EJBObject JavaDoc;
23 import javax.rmi.PortableRemoteObject JavaDoc;
24
25 import com.jdon.bussinessproxy.target.TargetServiceFactory;
26 import com.jdon.container.access.TargetMetaRequest;
27 import com.jdon.container.finder.ComponentKeys;
28 import com.jdon.container.visitor.ComponentVisitor;
29 import com.jdon.util.Debug;
30
31 /**
32  * tools for method invoke
33  * @author <a HREF="mailto:banqiao@jdon.com">banq </a>
34  *
35  */

36 public class MethodInvokerUtil {
37     private final static String JavaDoc module = MethodInvokerUtil.class.getName();
38
39     /**
40      * the service execute by method reflection
41      *
42      * @param method
43      * @param targetObj
44      * @param p_args
45      * @return
46      * @throws Throwable
47      */

48     public Object JavaDoc execute(Method JavaDoc method, Object JavaDoc targetObj, Object JavaDoc[] p_args) throws Throwable JavaDoc {
49         try {
50             if ((method == null) || (targetObj == null))
51                 Debug.logError("[JdonFramework] no method or target, please check your configure", module);
52             if (p_args == null) p_args = new Object JavaDoc[0];
53             Debug.logVerbose("[JdonFramework] method invoke: " + targetObj.getClass().getName() + " method=" + method.getName(), module);
54             Object JavaDoc result = method.invoke(targetObj, p_args);
55             Debug.logVerbose("[JdonFramework] method invoke successfully ", module);
56             return result;
57         } catch (IllegalArgumentException JavaDoc iex) {
58             String JavaDoc errorInfo = "Errors happened in your method:["+ targetObj.getClass().getName() + "." + method.getName() +"]";
59             Debug.logError(errorInfo, module);
60             errorInfo = "[JdonFramework] method invoke IllegalArgumentException: " + iex
61                               + " method argument type :["+ method.getParameterTypes() +"], but method arguments value p_args type:"+ p_args.getClass().getName() ;
62             Debug.logError(errorInfo, module);
63             throw new Throwable JavaDoc(errorInfo, iex);
64         } catch (InvocationTargetException JavaDoc ex) {
65             String JavaDoc errorInfo = "Errors happened in your method:["+ targetObj.getClass().getName() + "." + method.getName() +"]";
66             Debug.logError(errorInfo, module);
67             throw new Throwable JavaDoc(errorInfo);
68         } catch (IllegalAccessException JavaDoc ex) {
69             String JavaDoc errorInfo = "Errors happened in your method:["+ targetObj.getClass().getName() + "." + method.getName() +"]";
70             Debug.logError(errorInfo, module);
71             Debug.logError("[JdonFramework] method invoke IllegalAccessException: " + ex, module);
72             throw new Throwable JavaDoc("access method:" + method + " " + ex, ex);
73         } catch (Exception JavaDoc ex) {
74             String JavaDoc errorInfo = "Errors happened in your method:["+ targetObj.getClass().getName() + "." + method.getName() +"]";
75             Debug.logError(errorInfo, module);
76             Debug.logError("[JdonFramework] method invoke error: " + ex, module);
77             throw new Throwable JavaDoc(" method invoke error: " + ex);
78         }
79
80     }
81
82     /**
83      * if target service is ejb object, cache it,
84      * so this function can active stateful session bean.
85      *
86      * @param targetServiceFactory
87      * @param targetMetaDef
88      * @return
89      * @throws Exception
90      */

91     public Object JavaDoc createTargetObject(TargetServiceFactory targetServiceFactory, TargetMetaRequest targetMetaRequest) {
92         Debug.logVerbose("[JdonFramework] now getTargetObject by visitor ", module);
93         Object JavaDoc targetObjRef = null;
94         try {
95             if (targetMetaRequest.getTargetMetaDef().isEJB()) { //cache the ejb object
96
ComponentVisitor cm = targetMetaRequest.getComponentVisitor();
97                 targetMetaRequest.setVisitableName(ComponentKeys.TARGETSERVICE_FACTORY);
98                 Debug.logVerbose(ComponentKeys.TARGETSERVICE_FACTORY + " in action (cache)", module);
99                 targetObjRef = cm.visit(targetMetaRequest);
100             } else {
101                 Debug.logVerbose("[JdonFramework] not active targer service instance cache !!!!", module);
102                 targetObjRef = targetServiceFactory.create(targetMetaRequest.getTargetMetaDef());
103             }
104         } catch (Exception JavaDoc e) {
105             Debug.logError("[JdonFramework]createTargetObject error: " + e, module);
106         }
107         return targetObjRef;
108     }
109
110     /**
111      * 如果å?‚数中有remote EJB,需è¦?从远程åº?列化过æ?¥è¿™äº›å?‚æ•°
112      *
113      */

114     public Object JavaDoc[] narrowArgs(Object JavaDoc[] p_args) {
115         if (p_args == null)
116             return null;
117         int length = p_args.length;
118         Object JavaDoc[] result = new Object JavaDoc[length];
119         for (int i = 0; i < length; i++) {
120             if (p_args[i] instanceof Remote JavaDoc)
121                 result[i] = PortableRemoteObject.narrow(p_args[i], EJBObject JavaDoc.class);
122             else
123                 result[i] = p_args[i];
124         }
125         return result;
126     }
127
128 }
129
Popular Tags