1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.config; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.util.Map; |
23 | |
import java.util.Set; |
24 | |
import java.util.logging.Level; |
25 | |
import java.util.logging.Logger; |
26 | |
|
27 | |
import javax.faces.component.UIViewRoot; |
28 | |
import javax.faces.event.PreDestroyCustomScopeEvent; |
29 | |
import javax.faces.event.PreDestroyViewMapEvent; |
30 | |
import javax.faces.event.ScopeContext; |
31 | |
import javax.faces.event.SystemEvent; |
32 | |
import javax.faces.event.SystemEventListener; |
33 | |
|
34 | |
import org.apache.myfaces.config.annotation.LifecycleProvider; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
public class ManagedBeanDestroyer implements SystemEventListener |
44 | |
{ |
45 | |
|
46 | 0 | private static Logger log = Logger.getLogger(ManagedBeanDestroyer.class.getName()); |
47 | |
|
48 | |
private RuntimeConfig _runtimeConfig; |
49 | |
private LifecycleProvider _lifecycleProvider; |
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public ManagedBeanDestroyer(LifecycleProvider lifecycleProvider, |
59 | |
RuntimeConfig runtimeConfig) |
60 | 0 | { |
61 | 0 | _lifecycleProvider = lifecycleProvider; |
62 | 0 | _runtimeConfig = runtimeConfig; |
63 | 0 | } |
64 | |
|
65 | |
public boolean isListenerForSource(Object source) |
66 | |
{ |
67 | |
|
68 | |
|
69 | 0 | return (source instanceof ScopeContext) || (source instanceof UIViewRoot); |
70 | |
} |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
public void processEvent(SystemEvent event) |
77 | |
{ |
78 | 0 | Map<String, Object> scope = null; |
79 | |
|
80 | 0 | if (event instanceof PreDestroyViewMapEvent) |
81 | |
{ |
82 | 0 | UIViewRoot viewRoot = (UIViewRoot) ((PreDestroyViewMapEvent) event).getComponent(); |
83 | 0 | scope = viewRoot.getViewMap(false); |
84 | 0 | if (scope == null) |
85 | |
{ |
86 | |
|
87 | 0 | return; |
88 | |
} |
89 | 0 | } |
90 | 0 | else if (event instanceof PreDestroyCustomScopeEvent) |
91 | |
{ |
92 | 0 | ScopeContext scopeContext = ((PreDestroyCustomScopeEvent) event).getContext(); |
93 | 0 | scope = scopeContext.getScope(); |
94 | 0 | } |
95 | |
else |
96 | |
{ |
97 | |
|
98 | 0 | return; |
99 | |
} |
100 | |
|
101 | 0 | if (!scope.isEmpty()) |
102 | |
{ |
103 | 0 | Set<String> keySet = scope.keySet(); |
104 | 0 | String[] keys = keySet.toArray(new String[keySet.size()]); |
105 | |
|
106 | 0 | for (String key : keys) |
107 | |
{ |
108 | 0 | Object value = scope.get(key); |
109 | 0 | this.destroy(key, value); |
110 | |
} |
111 | |
} |
112 | 0 | } |
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
public boolean isManagedBean(String name) |
120 | |
{ |
121 | 0 | return (_runtimeConfig.getManagedBean(name) != null); |
122 | |
} |
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
public void destroy(String name, Object instance) |
130 | |
{ |
131 | 0 | if (instance != null && isManagedBean(name)) |
132 | |
{ |
133 | |
try |
134 | |
{ |
135 | 0 | _lifecycleProvider.destroyInstance(instance); |
136 | |
} |
137 | 0 | catch (IllegalAccessException e) |
138 | |
{ |
139 | 0 | log.log(Level.SEVERE, "Could not access @PreDestroy method of managed bean " + name, e); |
140 | |
} |
141 | 0 | catch (InvocationTargetException e) |
142 | |
{ |
143 | 0 | log.log(Level.SEVERE, "An Exception occured while invoking " + |
144 | |
"@PreDestroy method of managed bean " + name, e); |
145 | 0 | } |
146 | |
} |
147 | 0 | } |
148 | |
|
149 | |
} |