1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.myfaces.config.annotation; |
20 | |
|
21 | |
import java.lang.reflect.InvocationTargetException; |
22 | |
import java.util.Map; |
23 | |
import java.util.WeakHashMap; |
24 | |
import java.util.logging.Level; |
25 | |
import java.util.logging.Logger; |
26 | |
|
27 | |
import javax.faces.FacesException; |
28 | |
import javax.faces.context.ExternalContext; |
29 | |
import javax.faces.context.FacesContext; |
30 | |
import javax.naming.NamingException; |
31 | |
|
32 | |
import org.apache.myfaces.shared.util.ClassUtils; |
33 | |
import org.apache.tomcat.InstanceManager; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
public class Tomcat7AnnotationLifecycleProvider implements |
39 | |
DiscoverableLifecycleProvider, LifecycleProvider2 |
40 | |
{ |
41 | |
|
42 | 0 | private static Logger log = Logger.getLogger(TomcatAnnotationLifecycleProvider.class.getName()); |
43 | |
|
44 | 0 | private WeakHashMap<ClassLoader, InstanceManager> instanceManagers = null; |
45 | |
|
46 | |
public Tomcat7AnnotationLifecycleProvider(ExternalContext externalContext) |
47 | 0 | { |
48 | 0 | instanceManagers = new WeakHashMap<ClassLoader, InstanceManager>(); |
49 | 0 | } |
50 | |
|
51 | |
public Object newInstance(String className) |
52 | |
throws ClassNotFoundException, IllegalAccessException, |
53 | |
InstantiationException, NamingException, InvocationTargetException |
54 | |
{ |
55 | 0 | Class<?> clazz = ClassUtils.classForName(className); |
56 | 0 | if (log.isLoggable(Level.FINEST)) |
57 | |
{ |
58 | 0 | log.finest("Creating instance of " + className); |
59 | |
} |
60 | 0 | Object object = clazz.newInstance(); |
61 | |
|
62 | 0 | return object; |
63 | |
} |
64 | |
|
65 | |
public void destroyInstance(Object instance) |
66 | |
throws IllegalAccessException, InvocationTargetException |
67 | |
{ |
68 | 0 | InstanceManager manager = instanceManagers |
69 | |
.get(ClassUtils.getContextClassLoader()); |
70 | |
|
71 | 0 | if (manager != null) |
72 | |
{ |
73 | 0 | manager.destroyInstance(instance); |
74 | |
} |
75 | 0 | } |
76 | |
|
77 | |
public void postConstruct(Object instance) |
78 | |
throws IllegalAccessException, InvocationTargetException |
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 (NamingException e) |
97 | |
{ |
98 | 0 | throw new FacesException(e); |
99 | 0 | } |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | |
public boolean isAvailable() |
104 | |
{ |
105 | |
try |
106 | |
{ |
107 | 0 | Class.forName("org.apache.tomcat.InstanceManager", |
108 | |
true, ClassUtils.getContextClassLoader()); |
109 | 0 | return true; |
110 | |
} |
111 | 0 | catch (Exception e) |
112 | |
{ |
113 | |
|
114 | |
} |
115 | |
|
116 | 0 | return false; |
117 | |
} |
118 | |
|
119 | |
private InstanceManager initManager() |
120 | |
{ |
121 | 0 | FacesContext context = FacesContext.getCurrentInstance(); |
122 | 0 | if (context == null) |
123 | |
{ |
124 | 0 | return null; |
125 | |
} |
126 | |
|
127 | 0 | ExternalContext extCtx = context.getExternalContext(); |
128 | 0 | if (extCtx == null) |
129 | |
{ |
130 | 0 | return null; |
131 | |
} |
132 | |
|
133 | |
|
134 | 0 | Map<String, Object> applicationMap = extCtx.getApplicationMap(); |
135 | |
|
136 | 0 | InstanceManager instanceManager = (InstanceManager) |
137 | |
applicationMap.get(InstanceManager.class.getName()); |
138 | 0 | if (instanceManager != null) |
139 | |
{ |
140 | 0 | instanceManagers.put(ClassUtils.getContextClassLoader(), |
141 | |
instanceManager); |
142 | |
} |
143 | |
|
144 | 0 | return instanceManager; |
145 | |
} |
146 | |
|
147 | |
} |