1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.spi;
20
21 import javax.faces.FacesException;
22 import javax.faces.context.ExternalContext;
23 import javax.servlet.ServletContext;
24
25 import org.apache.myfaces.buildtools.maven2.plugin.builder.annotation.JSFWebConfigParam;
26 import org.apache.myfaces.shared.util.ClassUtils;
27 import org.apache.myfaces.spi.impl.DefaultServiceProviderFinder;
28
29
30
31
32
33
34
35
36
37 public class ServiceProviderFinderFactory
38 {
39
40 private final static String SERVICE_PROVIDER_KEY = "org.apache.myfaces.spi.ServiceProviderFinder";
41
42
43
44
45
46
47
48
49 @JSFWebConfigParam(since = "2.0.3", desc = "Class name of a custom ServiceProviderFinder implementation.")
50 private static final String SERVICE_PROVIDER_FINDER_PARAM = "org.apache.myfaces.SERVICE_PROVIDER_FINDER";
51
52
53
54
55
56
57
58 public static ServiceProviderFinder getServiceProviderFinder(ExternalContext ectx)
59 {
60 ServiceProviderFinder slp = (ServiceProviderFinder) ectx.getApplicationMap().get(SERVICE_PROVIDER_KEY);
61 if (slp == null)
62 {
63 slp = _getServiceProviderFinderFromInitParam(ectx);
64
65 if (slp == null)
66 {
67 slp = new DefaultServiceProviderFinder();
68 }
69
70
71 setServiceProviderFinder(ectx, slp);
72 }
73 return slp;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88 public static void setServiceProviderFinder(ExternalContext ectx, ServiceProviderFinder slp)
89 {
90 ectx.getApplicationMap().put(SERVICE_PROVIDER_KEY, slp);
91 }
92
93 public static void setServiceProviderFinder(ServletContext ctx, ServiceProviderFinder slp)
94 {
95 ctx.setAttribute(SERVICE_PROVIDER_KEY, slp);
96 }
97
98
99
100
101
102
103 private static ServiceProviderFinder _getServiceProviderFinderFromInitParam(ExternalContext context)
104 {
105 String initializerClassName = context.getInitParameter(SERVICE_PROVIDER_FINDER_PARAM);
106 if (initializerClassName != null)
107 {
108 try
109 {
110
111 Class<?> clazz = ClassUtils.classForName(initializerClassName);
112 if (!ServiceProviderFinder.class.isAssignableFrom(clazz))
113 {
114 throw new FacesException("Class " + clazz
115 + " does not implement ServiceProviderFinder");
116 }
117
118
119 return (ServiceProviderFinder) ClassUtils.newInstance(clazz);
120 }
121 catch (ClassNotFoundException cnfe)
122 {
123 throw new FacesException("Could not find class of specified ServiceProviderFinder", cnfe);
124 }
125 }
126 return null;
127 }
128
129 }