1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.spi.impl; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.lang.reflect.Method; |
23 | |
import java.util.Map; |
24 | |
import java.util.WeakHashMap; |
25 | |
import javax.faces.context.ExternalContext; |
26 | |
import javax.faces.context.FacesContext; |
27 | |
import javax.naming.NamingException; |
28 | |
|
29 | |
import org.apache.myfaces.shared.util.ClassUtils; |
30 | |
import org.apache.myfaces.spi.InjectionProvider; |
31 | |
import org.apache.myfaces.spi.InjectionProviderException; |
32 | |
import org.apache.myfaces.util.ExternalSpecifications; |
33 | |
import org.apache.tomcat.InstanceManager; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class Tomcat7AnnotationInjectionProvider extends InjectionProvider |
39 | |
{ |
40 | |
|
41 | 0 | private WeakHashMap<ClassLoader, InstanceManager> instanceManagers = null; |
42 | |
|
43 | |
public Tomcat7AnnotationInjectionProvider(ExternalContext externalContext) |
44 | 0 | { |
45 | 0 | instanceManagers = new WeakHashMap<ClassLoader, InstanceManager>(); |
46 | 0 | } |
47 | |
|
48 | |
@Override |
49 | |
public Object inject(Object instance) throws InjectionProviderException |
50 | |
{ |
51 | 0 | return null; |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public void preDestroy(Object instance, Object creationMetaData) throws InjectionProviderException |
56 | |
{ |
57 | 0 | InstanceManager manager = instanceManagers |
58 | |
.get(ClassUtils.getContextClassLoader()); |
59 | |
|
60 | 0 | if (manager != null) |
61 | |
{ |
62 | |
try |
63 | |
{ |
64 | 0 | manager.destroyInstance(instance); |
65 | |
} |
66 | 0 | catch (IllegalAccessException ex) |
67 | |
{ |
68 | 0 | throw new InjectionProviderException(ex); |
69 | |
} |
70 | 0 | catch (InvocationTargetException ex) |
71 | |
{ |
72 | 0 | throw new InjectionProviderException(ex); |
73 | 0 | } |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
@Override |
78 | |
public void postConstruct(Object instance, Object creationMetaData) throws InjectionProviderException |
79 | |
{ |
80 | 0 | InstanceManager manager = instanceManagers |
81 | |
.get(ClassUtils.getContextClassLoader()); |
82 | 0 | if (manager == null) |
83 | |
{ |
84 | |
|
85 | 0 | manager = initManager(); |
86 | |
} |
87 | |
|
88 | |
|
89 | 0 | if (manager != null) |
90 | |
{ |
91 | |
|
92 | |
try |
93 | |
{ |
94 | 0 | manager.newInstance(instance); |
95 | |
} |
96 | 0 | catch (IllegalAccessException ex) |
97 | |
{ |
98 | 0 | throw new InjectionProviderException(ex); |
99 | |
} |
100 | 0 | catch (InvocationTargetException ex) |
101 | |
{ |
102 | 0 | throw new InjectionProviderException(ex); |
103 | |
} |
104 | 0 | catch (NamingException e) |
105 | |
{ |
106 | 0 | throw new InjectionProviderException(e); |
107 | 0 | } |
108 | |
} |
109 | 0 | } |
110 | |
|
111 | |
@Override |
112 | |
public boolean isAvailable() |
113 | |
{ |
114 | |
try |
115 | |
{ |
116 | 0 | Class c = Class.forName("org.apache.tomcat.InstanceManager", |
117 | |
true, ClassUtils.getContextClassLoader()); |
118 | 0 | if (c != null) |
119 | |
{ |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | 0 | FacesContext facesContext = FacesContext.getCurrentInstance(); |
126 | 0 | if (ExternalSpecifications.isCDIAvailable(facesContext.getExternalContext())) |
127 | |
{ |
128 | |
|
129 | 0 | ExternalContext extCtx = facesContext.getExternalContext(); |
130 | 0 | Map<String, Object> applicationMap = extCtx.getApplicationMap(); |
131 | 0 | InstanceManager instanceManager = (InstanceManager) |
132 | |
applicationMap.get(InstanceManager.class.getName()); |
133 | |
|
134 | 0 | Class clazz = ClassUtils.classForName( |
135 | |
"org.apache.myfaces.cdi.checkenv.DummyInjectableBean"); |
136 | 0 | Object dummyInjectableBean = clazz.newInstance(); |
137 | |
|
138 | 0 | instanceManager.newInstance(dummyInjectableBean); |
139 | |
|
140 | 0 | Method m = clazz.getDeclaredMethod("isDummyBeanInjected"); |
141 | 0 | Object value = m.invoke(dummyInjectableBean); |
142 | 0 | if (Boolean.TRUE.equals(value)) |
143 | |
{ |
144 | |
|
145 | 0 | return true; |
146 | |
} |
147 | |
else |
148 | |
{ |
149 | |
|
150 | |
|
151 | |
|
152 | 0 | return false; |
153 | |
} |
154 | |
} |
155 | |
} |
156 | 0 | return true; |
157 | |
} |
158 | 0 | catch (Exception e) |
159 | |
{ |
160 | |
|
161 | |
} |
162 | 0 | return false; |
163 | |
} |
164 | |
|
165 | |
private InstanceManager initManager() |
166 | |
{ |
167 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
168 | 0 | if (context == null) |
169 | |
{ |
170 | 0 | return null; |
171 | |
} |
172 | |
|
173 | 0 | ExternalContext extCtx = context.getExternalContext(); |
174 | 0 | if (extCtx == null) |
175 | |
{ |
176 | 0 | return null; |
177 | |
} |
178 | |
|
179 | |
|
180 | 0 | Map<String, Object> applicationMap = extCtx.getApplicationMap(); |
181 | |
|
182 | 0 | InstanceManager instanceManager = (InstanceManager) |
183 | |
applicationMap.get(InstanceManager.class.getName()); |
184 | 0 | if (instanceManager != null) |
185 | |
{ |
186 | 0 | instanceManagers.put(ClassUtils.getContextClassLoader(), |
187 | |
instanceManager); |
188 | |
} |
189 | |
|
190 | 0 | return instanceManager; |
191 | |
} |
192 | |
|
193 | |
} |