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 org.apache.myfaces.config.annotation.DefaultAnnotationProvider; |
22 | |
import org.apache.myfaces.shared.util.ClassUtils; |
23 | |
import org.apache.myfaces.spi.AnnotationProvider; |
24 | |
import org.apache.myfaces.spi.AnnotationProviderFactory; |
25 | |
import org.apache.myfaces.spi.ServiceProviderFinderFactory; |
26 | |
|
27 | |
import javax.faces.FacesException; |
28 | |
import javax.faces.context.ExternalContext; |
29 | |
import java.lang.reflect.InvocationTargetException; |
30 | |
import java.security.AccessController; |
31 | |
import java.security.PrivilegedActionException; |
32 | |
import java.security.PrivilegedExceptionAction; |
33 | |
import java.util.List; |
34 | |
import java.util.logging.Level; |
35 | |
import java.util.logging.Logger; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class DefaultAnnotationProviderFactory extends AnnotationProviderFactory |
43 | |
{ |
44 | 0 | public static final String ANNOTATION_PROVIDER = AnnotationProvider.class.getName(); |
45 | |
|
46 | 0 | public static final String ANNOTATION_PROVIDER_LIST = AnnotationProvider.class.getName()+".LIST"; |
47 | |
|
48 | 0 | public static final String ANNOTATION_PROVIDER_INSTANCE = AnnotationProvider.class.getName()+".INSTANCE"; |
49 | |
|
50 | |
private Logger getLogger() |
51 | |
{ |
52 | 0 | return Logger.getLogger(DefaultAnnotationProviderFactory.class.getName()); |
53 | |
} |
54 | |
|
55 | |
@Override |
56 | |
public AnnotationProvider getAnnotationProvider(ExternalContext externalContext) |
57 | |
{ |
58 | 0 | AnnotationProvider annotationProvider |
59 | |
= (AnnotationProvider) externalContext.getApplicationMap().get(ANNOTATION_PROVIDER_INSTANCE); |
60 | 0 | if (annotationProvider == null) |
61 | |
{ |
62 | 0 | annotationProvider = createAnnotationProvider(externalContext); |
63 | 0 | externalContext.getApplicationMap().put(ANNOTATION_PROVIDER_INSTANCE, annotationProvider); |
64 | |
} |
65 | 0 | return annotationProvider; |
66 | |
} |
67 | |
|
68 | |
@Override |
69 | |
public AnnotationProvider createAnnotationProvider( |
70 | |
ExternalContext externalContext) |
71 | |
{ |
72 | 0 | AnnotationProvider returnValue = null; |
73 | 0 | final ExternalContext extContext = externalContext; |
74 | |
try |
75 | |
{ |
76 | 0 | if (System.getSecurityManager() != null) |
77 | |
{ |
78 | 0 | returnValue = AccessController.doPrivileged(new PrivilegedExceptionAction<AnnotationProvider>() |
79 | 0 | { |
80 | |
public AnnotationProvider run() throws ClassNotFoundException, |
81 | |
NoClassDefFoundError, |
82 | |
InstantiationException, |
83 | |
IllegalAccessException, |
84 | |
InvocationTargetException, |
85 | |
PrivilegedActionException |
86 | |
{ |
87 | 0 | return resolveAnnotationProviderFromService(extContext); |
88 | |
} |
89 | |
}); |
90 | |
} |
91 | |
else |
92 | |
{ |
93 | 0 | returnValue = resolveAnnotationProviderFromService(extContext); |
94 | |
} |
95 | |
} |
96 | 0 | catch (ClassNotFoundException e) |
97 | |
{ |
98 | |
|
99 | |
} |
100 | 0 | catch (NoClassDefFoundError e) |
101 | |
{ |
102 | |
|
103 | |
} |
104 | 0 | catch (InstantiationException e) |
105 | |
{ |
106 | 0 | getLogger().log(Level.SEVERE, "", e); |
107 | |
} |
108 | 0 | catch (IllegalAccessException e) |
109 | |
{ |
110 | 0 | getLogger().log(Level.SEVERE, "", e); |
111 | |
} |
112 | 0 | catch (InvocationTargetException e) |
113 | |
{ |
114 | 0 | getLogger().log(Level.SEVERE, "", e); |
115 | |
} |
116 | 0 | catch (PrivilegedActionException e) |
117 | |
{ |
118 | 0 | throw new FacesException(e); |
119 | 0 | } |
120 | 0 | return returnValue; |
121 | |
} |
122 | |
|
123 | |
private AnnotationProvider resolveAnnotationProviderFromService( |
124 | |
ExternalContext externalContext) throws ClassNotFoundException, |
125 | |
NoClassDefFoundError, |
126 | |
InstantiationException, |
127 | |
IllegalAccessException, |
128 | |
InvocationTargetException, |
129 | |
PrivilegedActionException |
130 | |
{ |
131 | 0 | List<String> classList = (List<String>) externalContext.getApplicationMap().get(ANNOTATION_PROVIDER_LIST); |
132 | 0 | if (classList == null) |
133 | |
{ |
134 | 0 | classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext). |
135 | |
getServiceProviderList(ANNOTATION_PROVIDER); |
136 | 0 | externalContext.getApplicationMap().put(ANNOTATION_PROVIDER_LIST, classList); |
137 | |
} |
138 | 0 | return ClassUtils.buildApplicationObject(AnnotationProvider.class, classList, new DefaultAnnotationProvider()); |
139 | |
} |
140 | |
} |