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
65
66
67
68 public class GuiceResolver extends ManagedBeanResolver
69 {
70
71 public static final String KEY = "oam." + Injector.class.getName();
72
73 @Override
74 public Object getValue(ELContext ctx, Object base, Object property) throws NullPointerException,
75 PropertyNotFoundException, ELException
76 {
77
78 if (base != null || !(property instanceof String))
79 return null;
80
81 if (property == null)
82 throw new PropertyNotFoundException();
83
84 FacesContext fctx = (FacesContext)ctx.getContext(FacesContext.class);
85
86 if (fctx == null)
87 return null;
88
89 ExternalContext ectx = fctx.getExternalContext();
90
91 if (ectx == null || ectx.getRequestMap().containsKey(property) || ectx.getSessionMap().containsKey(property)
92 || ectx.getApplicationMap().containsKey(property))
93 return null;
94
95 ManagedBean managedBean = runtimeConfig(ctx).getManagedBean((String)property);
96
97 return managedBean == null ? null : getValue(ctx, ectx, managedBean.getManagedBeanClass());
98 }
99
100 private Object getValue(ELContext ctx, ExternalContext ectx, Class<?> managedBeanClass)
101 {
102
103 Injector injector = (Injector)ectx.getApplicationMap().get(KEY);
104
105 if (injector == null)
106 throw new FacesException("Could not find an instance of " + Injector.class.getName()
107 + " in application scope using key '" + KEY + "'");
108
109 Object value = injector.getInstance(managedBeanClass);
110 ctx.setPropertyResolved(true);
111 return value;
112 }
113
114 }