1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.el.unified.resolver;
21
22 import javax.el.ELContext;
23 import javax.el.ELException;
24 import javax.el.PropertyNotFoundException;
25 import javax.faces.FacesException;
26 import javax.faces.context.ExternalContext;
27 import javax.faces.context.FacesContext;
28
29 import org.apache.myfaces.config.element.ManagedBean;
30
31 import com.google.inject.Injector;
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64 public class GuiceResolver extends ManagedBeanResolver
65 {
66
67 public static final String KEY = "oam." + Injector.class.getName();
68
69 @Override
70 public Object getValue(ELContext ctx, Object base, Object property) throws NullPointerException,
71 PropertyNotFoundException, ELException
72 {
73
74 if (base != null || !(property instanceof String))
75 {
76 return null;
77 }
78
79 FacesContext fctx = (FacesContext)ctx.getContext(FacesContext.class);
80
81 if (fctx == null)
82 {
83 return null;
84 }
85
86 ExternalContext ectx = fctx.getExternalContext();
87
88 if (ectx == null || ectx.getRequestMap().containsKey(property) || ectx.getSessionMap().containsKey(property)
89 || ectx.getApplicationMap().containsKey(property))
90 {
91 return null;
92 }
93
94 ManagedBean managedBean = runtimeConfig(ctx).getManagedBean((String)property);
95
96 return managedBean == null ? null : getValue(ctx, ectx, managedBean.getManagedBeanClass());
97 }
98
99 private Object getValue(ELContext ctx, ExternalContext ectx, Class<?> managedBeanClass)
100 {
101
102 Injector injector = (Injector)ectx.getApplicationMap().get(KEY);
103
104 if (injector == null)
105 {
106 throw new FacesException("Could not find an instance of " + Injector.class.getName()
107 + " in application scope using key '" + KEY + "'");
108 }
109
110 Object value = injector.getInstance(managedBeanClass);
111 ctx.setPropertyResolved(true);
112 return value;
113 }
114
115 }