1 | |
package org.jtheque.core.spring.factory; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
import org.jtheque.core.managers.Managers; |
20 | |
import org.jtheque.core.managers.beans.IBeansManager; |
21 | |
import org.jtheque.core.managers.log.ILoggingManager; |
22 | |
|
23 | |
import javax.swing.SwingUtilities; |
24 | |
import java.lang.reflect.InvocationHandler; |
25 | |
import java.lang.reflect.InvocationTargetException; |
26 | |
import java.lang.reflect.Method; |
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 0 | final class LazyProxyHandler implements InvocationHandler { |
34 | |
private final String beanName; |
35 | |
private final boolean swing; |
36 | |
|
37 | |
private Object instance; |
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
LazyProxyHandler(String beanName, boolean swing) { |
46 | 0 | super(); |
47 | |
|
48 | 0 | this.beanName = beanName; |
49 | 0 | this.swing = swing; |
50 | 0 | } |
51 | |
|
52 | |
@Override |
53 | |
public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException { |
54 | 0 | if (instance == null) { |
55 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).debug("Init {} due to call to {}", beanName, method.toGenericString()); |
56 | |
|
57 | 0 | if(swing && !SwingUtilities.isEventDispatchThread()){ |
58 | |
try { |
59 | 0 | SwingUtilities.invokeAndWait(new Runnable(){ |
60 | |
@Override |
61 | |
public void run() { |
62 | 0 | instance = Managers.getManager(IBeansManager.class).getBean(beanName); |
63 | 0 | } |
64 | |
}); |
65 | 0 | } catch (InterruptedException e) { |
66 | 0 | Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e); |
67 | 0 | } |
68 | |
} else { |
69 | 0 | instance = Managers.getManager(IBeansManager.class).getBean(beanName); |
70 | |
} |
71 | |
} |
72 | |
|
73 | 0 | return method.invoke(instance, args); |
74 | |
} |
75 | |
} |