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 private static Logger log = Logger.getLogger(Tomcat7AnnotationLifecycleProvider.class.getName());
43
44 private WeakHashMap<ClassLoader, InstanceManager> instanceManagers = null;
45
46 public Tomcat7AnnotationLifecycleProvider(ExternalContext externalContext)
47 {
48 instanceManagers = new WeakHashMap<ClassLoader, InstanceManager>();
49 }
50
51 public Object newInstance(String className)
52 throws ClassNotFoundException, IllegalAccessException,
53 InstantiationException, NamingException, InvocationTargetException
54 {
55 Class<?> clazz = ClassUtils.classForName(className);
56 if (log.isLoggable(Level.FINEST))
57 {
58 log.finest("Creating instance of " + className);
59 }
60 Object object = clazz.newInstance();
61
62 return object;
63 }
64
65 public void destroyInstance(Object instance)
66 throws IllegalAccessException, InvocationTargetException
67 {
68 InstanceManager manager = instanceManagers
69 .get(ClassUtils.getContextClassLoader());
70
71 if (manager != null)
72 {
73 manager.destroyInstance(instance);
74 }
75 }
76
77 public void postConstruct(Object instance)
78 throws IllegalAccessException, InvocationTargetException
79 {
80 InstanceManager manager = instanceManagers
81 .get(ClassUtils.getContextClassLoader());
82 if (manager == null)
83 {
84
85 manager = initManager();
86 }
87
88
89 if (manager != null)
90 {
91
92 try
93 {
94 manager.newInstance(instance);
95 }
96 catch (NamingException e)
97 {
98 throw new FacesException(e);
99 }
100 }
101 }
102
103 public boolean isAvailable()
104 {
105 try
106 {
107 Class.forName("org.apache.tomcat.InstanceManager",
108 true, ClassUtils.getContextClassLoader());
109 return true;
110 }
111 catch (Exception e)
112 {
113
114 }
115
116 return false;
117 }
118
119 private InstanceManager initManager()
120 {
121 FacesContext context = FacesContext.getCurrentInstance();
122 if (context == null)
123 {
124 return null;
125 }
126
127 ExternalContext extCtx = context.getExternalContext();
128 if (extCtx == null)
129 {
130 return null;
131 }
132
133
134 Map<String, Object> applicationMap = extCtx.getApplicationMap();
135
136 InstanceManager instanceManager = (InstanceManager)
137 applicationMap.get(InstanceManager.class.getName());
138 if (instanceManager != null)
139 {
140 instanceManagers.put(ClassUtils.getContextClassLoader(),
141 instanceManager);
142 }
143
144 return instanceManager;
145 }
146
147 }