~ubuntu-branches/ubuntu/oneiric/libspin-java/oneiric

« back to all changes in this revision

Viewing changes to src/spin/CGLibProxyFactory.java

  • Committer: Bazaar Package Importer
  • Author(s): gregor herrmann
  • Date: 2007-01-15 16:29:48 UTC
  • Revision ID: james.westby@ubuntu.com-20070115162948-olisrjfshcvqa7qu
Tags: upstream-1.4
ImportĀ upstreamĀ versionĀ 1.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * Spin - transparent threading solution for non-freezing Swing applications.
 
3
 * Copyright (C) 2002 Sven Meier
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 *
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 *
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 */
 
19
package spin;
 
20
 
 
21
import java.lang.reflect.Method;
 
22
 
 
23
import net.sf.cglib.proxy.Enhancer;
 
24
import net.sf.cglib.proxy.Factory;
 
25
import net.sf.cglib.proxy.MethodInterceptor;
 
26
import net.sf.cglib.proxy.MethodProxy;
 
27
 
 
28
/**
 
29
 * A factory of proxies utilizing CGLib.
 
30
 */
 
31
public class CGLibProxyFactory extends ProxyFactory {
 
32
 
 
33
    public Object createProxy(Object object, Evaluator evaluator) {
 
34
        return Enhancer.create(object.getClass(), new SpinMethodInterceptor(object, evaluator));
 
35
    }
 
36
 
 
37
    public boolean isProxy(Object object) {
 
38
      
 
39
        if (object == null) {
 
40
            return false;
 
41
        }
 
42
        
 
43
        if (!(object instanceof Factory)) {
 
44
            return false;
 
45
        }
 
46
 
 
47
        Factory factory = (Factory)object;
 
48
        return (factory.getCallback(0) instanceof SpinMethodInterceptor);
 
49
    }    
 
50
    
 
51
    protected boolean areProxyEqual(Object proxy1, Object proxy2) {
 
52
        SpinMethodInterceptor methodInterceptor1 = (SpinMethodInterceptor)((Factory)proxy1).getCallback(0);
 
53
        SpinMethodInterceptor methodInterceptor2 = (SpinMethodInterceptor)((Factory)proxy2).getCallback(0);
 
54
        
 
55
        return methodInterceptor1.object == methodInterceptor2.object;
 
56
    }
 
57
    
 
58
    /**
 
59
     * Method interceptor for the <em>Spin</em> proxy.
 
60
     */
 
61
    private class SpinMethodInterceptor implements MethodInterceptor {
 
62
 
 
63
      private Object object;
 
64
      private Evaluator evaluator;
 
65
      
 
66
      public SpinMethodInterceptor(Object object, Evaluator evaluator) {
 
67
          this.object  = object;
 
68
          this.evaluator = evaluator;
 
69
      }
 
70
 
 
71
      /**
 
72
       * Handle the invocation of a method on the <em>Spin</em> proxy.
 
73
       *
 
74
       * @param proxy       the proxy instance
 
75
       * @param method      the method to invoke
 
76
       * @param args        the arguments for the method
 
77
       * @param methodProxy non-intercepted proxy for the method
 
78
       * @return            the result of the invocation on the wrapped object
 
79
       * @throws Throwable if the wrapped method throws a <code>Throwable</code>
 
80
       */
 
81
      public Object intercept(Object proxy, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
 
82
          return evaluteInvocation(evaluator, proxy, new Invocation(this.object, method, args));
 
83
      }      
 
84
    } 
 
85
}
 
 
b'\\ No newline at end of file'