KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > bussinessproxy > dyncproxy > ProxyInstanceFactoryVisitable


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.bussinessproxy.dyncproxy;
17 import java.lang.reflect.Proxy JavaDoc;
18 import java.util.ArrayList JavaDoc;
19 import java.util.List JavaDoc;
20
21 import com.jdon.aop.AopClient;
22 import com.jdon.bussinessproxy.TargetMetaDef;
23 import com.jdon.bussinessproxy.meta.DistributedTargetMetaDef;
24 import com.jdon.bussinessproxy.meta.EJBTargetMetaDef;
25 import com.jdon.container.access.TargetMetaRequest;
26 import com.jdon.container.visitor.Visitable;
27 import com.jdon.util.Debug;
28
29 /**
30  * by using Proxy.newProxyInstance, create a DynamicProxyWeaving for
31  * a target service.
32  *
33  * @todo: create a DynamicProxyWeaving for all services.
34  *
35  * @author <a HREF="mailto:banqiao@jdon.com">banq</a>
36  *
37  */

38 public class ProxyInstanceFactoryVisitable implements Visitable{
39     private final static String JavaDoc module = ProxyInstanceFactoryVisitable.class.getName();
40     private AopClient aopClient;
41     
42     /**
43      * @param aopClient
44      */

45     public ProxyInstanceFactoryVisitable(AopClient aopClient) {
46         super();
47         this.aopClient = aopClient;
48     }
49     
50     /**
51      *
52      */

53     public Object JavaDoc accept(TargetMetaRequest targetMetaRequest) {
54         Debug.logVerbose("[JdonFramework]enter Proxy.newProxyInstance ", module);
55         Object JavaDoc dynamicProxy = null;
56         try {
57           ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
58      
59           DynamicProxyWeaving dynamicProxyWeaving = new DynamicProxyWeaving(targetMetaRequest, aopClient);
60           dynamicProxy =
61               Proxy.newProxyInstance(
62               classLoader,
63               getInterfaces(targetMetaRequest.getTargetMetaDef()),
64               dynamicProxyWeaving);
65         } catch (Exception JavaDoc ex) {
66           Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module);
67         } catch (Throwable JavaDoc ex) {
68           Debug.logError("[JdonFramework] Proxy.newProxyInstance error:" + ex, module);
69         }
70         return dynamicProxy;
71     }
72
73     /**
74      * get the interface of target class
75      * if it is EJB, it is ejb local/remote interface
76      * if it is pojo, it is a class .
77      *
78      *
79      * @param targetMetaDef
80      * @return
81      */

82     private Class JavaDoc[] getInterfaces(TargetMetaDef targetMetaDef){
83         Class JavaDoc[] interfaces = targetMetaDef.getInterfaces();
84         if (interfaces != null) return interfaces;
85         try {
86             if (targetMetaDef.isEJB()){
87                 if (targetMetaDef instanceof EJBTargetMetaDef)
88                     interfaces = getEJB2Interfaces(targetMetaDef);
89                 else if (targetMetaDef instanceof DistributedTargetMetaDef)
90                     interfaces = getEJB3Interfaces(targetMetaDef);
91             }else{
92                 interfaces = getPOJOInterfaces(targetMetaDef);
93             }
94           } catch (Exception JavaDoc ex) {
95             Debug.logError("[JdonFramework] getInterfaces error:" + ex, module);
96           } catch (Throwable JavaDoc ex) {
97             Debug.logError("[JdonFramework] getInterfaces error:" + ex, module);
98           }
99        if ((interfaces == null) || (interfaces.length == 0)){
100            Debug.logError("[JdonFramework] no find any interface for the service:" + targetMetaDef.getClassName(), module);
101        }else{
102            targetMetaDef.setInterfaces(interfaces); //cache the result
103
Debug.logVerbose("[JdonFramework]found the the below interfaces for the service:"+ targetMetaDef.getClassName(), module);
104            for(int i=0; i<interfaces.length; i++){
105                Debug.logVerbose(interfaces[i].getName() + ";", module);
106            }
107        }
108        return interfaces;
109     }
110     
111     private Class JavaDoc[] getEJB3Interfaces(TargetMetaDef targetMetaDef) throws Exception JavaDoc {
112         Class JavaDoc[] interfaces = null;
113         try {
114             DistributedTargetMetaDef dtargetMetaDef = (DistributedTargetMetaDef)targetMetaDef;
115             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
116             Debug.logVerbose("[JdonFramework]getEJB3Interfaces interface="+ dtargetMetaDef.getInterfaceClass(), module);
117             interfaces = new Class JavaDoc[] {classLoader.loadClass(dtargetMetaDef.getInterfaceClass()) };
118         } catch (Exception JavaDoc e) {
119             e.printStackTrace();
120             throw new Exception JavaDoc();
121         }
122         return interfaces;
123     }
124
125     
126     private Class JavaDoc[] getEJB2Interfaces(TargetMetaDef targetMetaDef) throws Exception JavaDoc {
127         Class JavaDoc[] interfaces = null;
128         Debug.logVerbose("[JdonFramework]getEJB2Interfaces "+ targetMetaDef.getClassName(), module);
129         try {
130             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
131             Class JavaDoc configClasses = classLoader.loadClass(targetMetaDef.getClassName());
132             EJBTargetMetaDef em = (EJBTargetMetaDef) targetMetaDef;
133             if ((em.getInterfaceClass() != null)
134                     && (em.getInterfaceClass().length() != 0)) {
135                 interfaces = new Class JavaDoc[] { configClasses, classLoader.loadClass(em.getInterfaceClass()) };
136             } else
137                 interfaces = new Class JavaDoc[] { configClasses };
138         } catch (Exception JavaDoc e) {
139             e.printStackTrace();
140             throw new Exception JavaDoc();
141         }
142         return interfaces;
143     }
144
145     
146     public Class JavaDoc[] getPOJOInterfaces(TargetMetaDef targetMetaDef) {
147         Class JavaDoc[] interfaces = null;
148         Debug.logVerbose("[JdonFramework]getPOJOInterfaces "+ targetMetaDef.getClassName(), module);
149         try {
150             List JavaDoc interfacesL = new ArrayList JavaDoc();
151             ClassLoader JavaDoc classLoader = Thread.currentThread().getContextClassLoader();
152             Class JavaDoc pojoClass = classLoader.loadClass(targetMetaDef.getClassName());
153             while (pojoClass != null) {
154                 for (int i = 0; i < pojoClass.getInterfaces().length; i++) {
155                     Class JavaDoc ifc = pojoClass.getInterfaces()[i];
156                     interfacesL.add(ifc);
157                 }
158                 pojoClass = pojoClass.getSuperclass();
159             }
160             if (interfacesL.size() == 0){
161                 throw new Exception JavaDoc();
162             }
163             interfaces = (Class JavaDoc[]) interfacesL.toArray(new Class JavaDoc[interfacesL.size()]);
164         } catch (Exception JavaDoc e) {
165             Debug.logError("[JdonFramework] not found the interface of your pojoClass=" +
166                     targetMetaDef.getClassName() +", error:" + e, module);
167         }
168         return interfaces;
169     }
170
171
172
173 }
174
Popular Tags