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