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