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