1 | |
package org.kuali.spring.util; |
2 | |
|
3 | |
import net.sf.cglib.proxy.Callback; |
4 | |
import net.sf.cglib.proxy.Enhancer; |
5 | |
import net.sf.cglib.proxy.NoOp; |
6 | |
|
7 | |
import org.springframework.beans.BeanUtils; |
8 | |
import org.springframework.beans.factory.FactoryBean; |
9 | |
import org.springframework.util.Assert; |
10 | |
|
11 | |
public class SimpleProxyFactoryBean implements FactoryBean<Object> { |
12 | 1 | public static final Callback DEFAULT_CALLBACK = NoOp.INSTANCE; |
13 | |
|
14 | 4 | Callback callback = DEFAULT_CALLBACK; |
15 | |
String classname; |
16 | |
Object sourceBean; |
17 | 4 | boolean copySourceBeanProperties = true; |
18 | |
|
19 | |
public SimpleProxyFactoryBean() { |
20 | 4 | this(null, DEFAULT_CALLBACK); |
21 | 4 | } |
22 | |
|
23 | |
public SimpleProxyFactoryBean(String classname) { |
24 | 0 | this(classname, DEFAULT_CALLBACK); |
25 | 0 | } |
26 | |
|
27 | |
public SimpleProxyFactoryBean(String classname, Callback callback) { |
28 | 4 | super(); |
29 | 4 | this.classname = classname; |
30 | 4 | this.callback = callback; |
31 | 4 | } |
32 | |
|
33 | |
@Override |
34 | |
public Object getObject() throws Exception { |
35 | 6 | Assert.isTrue(this.classname != null || this.sourceBean != null); |
36 | 6 | Assert.notNull(this.callback); |
37 | 6 | if (this.copySourceBeanProperties) { |
38 | 6 | Assert.notNull(this.sourceBean); |
39 | |
} |
40 | |
|
41 | 6 | Class<?> targetClass = getTargetClass(); |
42 | 6 | Enhancer enhancer = new Enhancer(); |
43 | 6 | enhancer.setSuperclass(targetClass); |
44 | 6 | enhancer.setCallback(getCallback()); |
45 | 6 | Object proxy = enhancer.create(); |
46 | 6 | if (this.copySourceBeanProperties) { |
47 | 6 | BeanUtils.copyProperties(this.sourceBean, proxy); |
48 | |
} |
49 | 6 | return proxy; |
50 | |
} |
51 | |
|
52 | |
protected Class<?> getTargetClass() throws ClassNotFoundException { |
53 | 6 | if (this.classname != null) { |
54 | 0 | return Class.forName(this.classname); |
55 | |
} else { |
56 | 6 | return this.sourceBean.getClass(); |
57 | |
} |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public Class<?> getObjectType() { |
62 | 8 | return Object.class; |
63 | |
} |
64 | |
|
65 | |
@Override |
66 | |
public boolean isSingleton() { |
67 | 10 | return false; |
68 | |
} |
69 | |
|
70 | |
public Callback getCallback() { |
71 | 6 | return callback; |
72 | |
} |
73 | |
|
74 | |
public void setCallback(Callback callback) { |
75 | 4 | this.callback = callback; |
76 | 4 | } |
77 | |
|
78 | |
public String getClassname() { |
79 | 0 | return classname; |
80 | |
} |
81 | |
|
82 | |
public void setClassname(String classname) { |
83 | 0 | this.classname = classname; |
84 | 0 | } |
85 | |
|
86 | |
public Object getSourceBean() { |
87 | 0 | return sourceBean; |
88 | |
} |
89 | |
|
90 | |
public void setSourceBean(Object source) { |
91 | 4 | this.sourceBean = source; |
92 | 4 | } |
93 | |
|
94 | |
} |