Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
GuiceResolver |
|
| 7.5;7.5 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
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 | * <p> | |
35 | * Register this ELResolver in faces-config.xml. | |
36 | * </p> | |
37 | * | |
38 | * <application> <el-resolver>org.apache.myfaces.el.unified.resolver.GuiceResolver</el-resolver> </application> | |
39 | * | |
40 | * <p> | |
41 | * Implement and configure a ServletContextListener in web.xml . | |
42 | * </p> | |
43 | * | |
44 | * <listener> <listener-class>com.your_company.GuiceServletContextListener</listener-class> </listener> | |
45 | * | |
46 | * <p> | |
47 | * Configure Guice in your ServletContextListener implementation, and place the Injector in application scope. | |
48 | * </p> | |
49 | * | |
50 | * public class GuiceServletContextListener implements ServletContextListener { | |
51 | * | |
52 | * public void contextInitialized(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); //when on | |
53 | * Java6, use ServiceLoader.load(com.google.inject.Module.class); Injector injector = Guice.createInjector(new | |
54 | * YourModule()); ctx.setAttribute(GuiceResolver.KEY, injector); } | |
55 | * | |
56 | * public void contextDestroyed(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); | |
57 | * ctx.removeAttribute(GuiceResolver.KEY); } | |
58 | * | |
59 | *} | |
60 | * | |
61 | * @author Dennis Byrne | |
62 | */ | |
63 | ||
64 | 0 | public class GuiceResolver extends ManagedBeanResolver |
65 | { | |
66 | ||
67 | 0 | 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 | 0 | if (base != null || !(property instanceof String)) |
75 | { | |
76 | 0 | return null; |
77 | } | |
78 | ||
79 | 0 | FacesContext fctx = (FacesContext)ctx.getContext(FacesContext.class); |
80 | ||
81 | 0 | if (fctx == null) |
82 | { | |
83 | 0 | return null; |
84 | } | |
85 | ||
86 | 0 | ExternalContext ectx = fctx.getExternalContext(); |
87 | ||
88 | 0 | if (ectx == null || ectx.getRequestMap().containsKey(property) || ectx.getSessionMap().containsKey(property) |
89 | || ectx.getApplicationMap().containsKey(property)) | |
90 | { | |
91 | 0 | return null; |
92 | } | |
93 | ||
94 | 0 | ManagedBean managedBean = runtimeConfig(ctx).getManagedBean((String)property); |
95 | ||
96 | 0 | return managedBean == null ? null : getValue(ctx, ectx, managedBean.getManagedBeanClass()); |
97 | } | |
98 | ||
99 | private Object getValue(ELContext ctx, ExternalContext ectx, Class<?> managedBeanClass) | |
100 | { | |
101 | ||
102 | 0 | Injector injector = (Injector)ectx.getApplicationMap().get(KEY); |
103 | ||
104 | 0 | if (injector == null) |
105 | { | |
106 | 0 | throw new FacesException("Could not find an instance of " + Injector.class.getName() |
107 | + " in application scope using key '" + KEY + "'"); | |
108 | } | |
109 | ||
110 | 0 | Object value = injector.getInstance(managedBeanClass); |
111 | 0 | ctx.setPropertyResolved(true); |
112 | 0 | return value; |
113 | } | |
114 | ||
115 | } |