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 public class DefaultAnnotationProviderFactory extends AnnotationProviderFactory
43 {
44 public static final String ANNOTATION_PROVIDER = AnnotationProvider.class.getName();
45
46 public static final String ANNOTATION_PROVIDER_LIST = AnnotationProvider.class.getName()+".LIST";
47
48 public static final String ANNOTATION_PROVIDER_INSTANCE = AnnotationProvider.class.getName()+".INSTANCE";
49
50 private Logger getLogger()
51 {
52 return Logger.getLogger(DefaultAnnotationProviderFactory.class.getName());
53 }
54
55 @Override
56 public AnnotationProvider getAnnotationProvider(ExternalContext externalContext)
57 {
58 AnnotationProvider annotationProvider
59 = (AnnotationProvider) externalContext.getApplicationMap().get(ANNOTATION_PROVIDER_INSTANCE);
60 if (annotationProvider == null)
61 {
62 annotationProvider = createAnnotationProvider(externalContext);
63 externalContext.getApplicationMap().put(ANNOTATION_PROVIDER_INSTANCE, annotationProvider);
64 }
65 return annotationProvider;
66 }
67
68 @Override
69 public AnnotationProvider createAnnotationProvider(
70 ExternalContext externalContext)
71 {
72 AnnotationProvider returnValue = null;
73 final ExternalContext extContext = externalContext;
74 try
75 {
76 if (System.getSecurityManager() != null)
77 {
78 returnValue = AccessController.doPrivileged(new PrivilegedExceptionAction<AnnotationProvider>()
79 {
80 public AnnotationProvider run() throws ClassNotFoundException,
81 NoClassDefFoundError,
82 InstantiationException,
83 IllegalAccessException,
84 InvocationTargetException,
85 PrivilegedActionException
86 {
87 return resolveAnnotationProviderFromService(extContext);
88 }
89 });
90 }
91 else
92 {
93 returnValue = resolveAnnotationProviderFromService(extContext);
94 }
95 }
96 catch (ClassNotFoundException e)
97 {
98
99 }
100 catch (NoClassDefFoundError e)
101 {
102
103 }
104 catch (InstantiationException e)
105 {
106 getLogger().log(Level.SEVERE, "", e);
107 }
108 catch (IllegalAccessException e)
109 {
110 getLogger().log(Level.SEVERE, "", e);
111 }
112 catch (InvocationTargetException e)
113 {
114 getLogger().log(Level.SEVERE, "", e);
115 }
116 catch (PrivilegedActionException e)
117 {
118 throw new FacesException(e);
119 }
120 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 List<String> classList = (List<String>) externalContext.getApplicationMap().get(ANNOTATION_PROVIDER_LIST);
132 if (classList == null)
133 {
134 classList = ServiceProviderFinderFactory.getServiceProviderFinder(externalContext).
135 getServiceProviderList(ANNOTATION_PROVIDER);
136 externalContext.getApplicationMap().put(ANNOTATION_PROVIDER_LIST, classList);
137 }
138 return ClassUtils.buildApplicationObject(AnnotationProvider.class, classList, new DefaultAnnotationProvider());
139 }
140 }