KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > yan > ProxyComponent


1 /*****************************************************************************
2  * Copyright (C) Codehaus.org. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Apr 5, 2005
10  *
11  * Author Ben Yu
12  * ZBS
13  */

14 package jfun.yan;
15
16 import jfun.util.Misc;
17 import jfun.util.StringUtils;
18 import jfun.yan.factory.Factory;
19 import jfun.yan.util.ReflectionUtil;
20
21 /**
22  * Codehaus.org.
23  *
24  * @author Ben Yu
25  *
26  */

27 final class ProxyComponent extends DelegatingComponent {
28   private final Class JavaDoc[] itfs;
29   private final ClassLoader JavaDoc cl;
30   private transient Class JavaDoc proxyType;
31   ProxyComponent(Component cc, final Class JavaDoc[] itfs,
32       final ClassLoader JavaDoc cl) {
33     super(cc);
34     this.itfs = itfs;
35     this.cl = cl;
36     this.proxyType = InstanceProxy.getProxyClass(cl, itfs);
37   }
38   private void readObject(java.io.ObjectInputStream JavaDoc in)
39   throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc{
40     in.defaultReadObject();
41     this.proxyType = InstanceProxy.getProxyClass(this.cl, this.itfs);
42   }
43   public boolean isConcrete(){
44     return true;
45   }
46   public Class JavaDoc getType(){
47     return proxyType;
48   }
49   public String JavaDoc toString(){
50     return "proxy <" + getDelegateTarget() +">";
51   }
52   public Object JavaDoc create(final Dependency dep){
53     return InstanceProxy.instance(cl, itfs, new Factory(){
54       public Object JavaDoc create(){
55         return getDelegateTarget().create(dep);
56       }
57     });
58   }
59   public Class JavaDoc verify(final Dependency dep){
60     //laziness requires that the verification cannot be cascaded.
61
//otherwise, circular dependency can still cause problem.
62
return getType();
63   }
64   public Component singleton(){
65     return Components.singleton(this);
66     /*
67     return new Component(){
68       private Object cached = null;
69       public Class getType() {
70         return proxyType;
71       }
72       public synchronized Object create(final Dependency dep){
73         if(cached!=null){
74           InstanceProxy.clear(cached);
75         }
76         else{
77           cached = ProxyComponent.this.creat(dep);
78         }
79         return cached;
80       }
81       public boolean isConcrete() {
82         return true;
83       }
84
85       public boolean isSingleton() {
86         return true;
87       }
88       public Component singleton(){
89         return this;
90       }
91       public Class verify(Dependency dep) throws IrresolveableArgumentException, ParameterTypeMismatchException, AmbiguousComponentResolutionException, YanException {
92         getDelegateTarget().verify(dep);
93         return proxyType;
94       }
95       public String toString(){
96         return ProxyComponent.this.toString();
97       }
98     };*/

99   }
100   /*
101   public Component singleton(final Pool scope){
102     //return Components.singleton(this);
103     return new Component(){
104       public Class getType() {
105         return proxyType;
106       }
107       public Object create(final Dependency dep){
108         final boolean[] evaluated ={false};
109         final Object proxy = scope.getInstance(new ObjectReference(){
110           public Object get(){
111             evaluated[0] = true;
112             return ProxyComponent.this.create(dep);
113           }
114         });
115         if(!evaluated[0]){
116           InstanceProxy.clear(proxy);
117         }
118         return proxy;
119       }
120       public boolean isConcrete() {
121         return true;
122       }
123
124       public boolean isSingleton() {
125         return false;
126       }
127       public Class verify(Dependency dep) throws IrresolveableArgumentException, ParameterTypeMismatchException, AmbiguousComponentResolutionException, YanException {
128         getDelegateTarget().verify(dep);
129         return proxyType;
130       }
131       public String toString(){
132         return ProxyComponent.this.toString();
133       }
134     };
135   }*/

136   /*
137   protected Component decorate(final Component c){
138     return Components.proxy(c, itfs);
139   }*/

140   public Component proxy(){
141     return this;
142   }
143   public Component proxy(Class JavaDoc itf){
144     return proxy(new Class JavaDoc[]{itf});
145   }
146   public Component proxy(Class JavaDoc[] itfs2){
147     boolean act = false;
148     for(int i=0; i<itfs2.length; i++){
149       final Class JavaDoc itf2 = itfs2[i];
150       final int among = contains(itfs, itf2);
151       if(among < 0){//mismatch
152
throw new IllegalArgumentException JavaDoc("type "
153             + Misc.getTypeName(itf2)
154             + " is not a super type of any of "
155             + StringUtils.listString("[", ",", "]", itfs));
156       }
157       if(among > 0) //if any one is a super type, proxy is needed.
158
act = true;
159     }
160     //x.proxy(A.class).proxy(A.class) is equivalent to x.proxy(A.class)
161
return act?Components.proxy(this, itfs2): this;
162   }
163
164   private int contains(Class JavaDoc[] types, Class JavaDoc type){
165     for(int i=0; i<types.length; i++){
166       if(type.equals(types[i])){
167         return 0;
168       }
169       if(ReflectionUtil.isAssignableFrom(type, types[i])){
170         return 1;
171       }
172     }
173     return -1;
174   }
175 }
176
Popular Tags