View Javadoc

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>Register this ELResolver in faces-config.xml.</p>
35   * 
36   *     &ltapplication>
37   *        &ltel-resolver>org.apache.myfaces.el.unified.resolver.GuiceResolver&lt/el-resolver>
38   *    &lt/application>
39   *
40   * <p>Implement and configure a ServletContextListener in web.xml .</p>
41   * 
42   *  &ltlistener>
43   *      <listener-class>com.your_company.GuiceServletContextListener&lt/listener-class>
44   *  &lt/listener>
45   * 
46   * <p>Configure Guice in your ServletContextListener implementation, and place the 
47   * Injector in application scope.</p>
48   * 
49   * public class GuiceServletContextListener implements ServletContextListener {
50   *
51   *    public void contextInitialized(ServletContextEvent event) {
52   *        ServletContext ctx = event.getServletContext();
53   *              //when on Java6, use ServiceLoader.load(com.google.inject.Module.class);
54   *        Injector injector = Guice.createInjector(new YourModule());
55   *        ctx.setAttribute(GuiceResolver.KEY, injector);
56   *    }
57   *
58   *    public void contextDestroyed(ServletContextEvent event) {
59   *        ServletContext ctx = event.getServletContext();
60   *        ctx.removeAttribute(GuiceResolver.KEY);
61   *    }
62   *
63   *}
64   * 
65   * @author Dennis Byrne
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 }