PageRenderTime 122ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/myfaces_core-2.1.10/src/myfaces-core-module-2.1.10/impl/src/main/java/org/apache/myfaces/el/unified/resolver/GuiceResolver.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 115 lines | 47 code | 20 blank | 48 comment | 13 complexity | a3054607b3398e277dcee2e13e2ae618 MD5 | raw file
  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. package org.apache.myfaces.el.unified.resolver;
  20. import javax.el.ELContext;
  21. import javax.el.ELException;
  22. import javax.el.PropertyNotFoundException;
  23. import javax.faces.FacesException;
  24. import javax.faces.context.ExternalContext;
  25. import javax.faces.context.FacesContext;
  26. import org.apache.myfaces.config.element.ManagedBean;
  27. import com.google.inject.Injector;
  28. /**
  29. * <p>
  30. * Register this ELResolver in faces-config.xml.
  31. * </p>
  32. *
  33. * &ltapplication> &ltel-resolver>org.apache.myfaces.el.unified.resolver.GuiceResolver&lt/el-resolver> &lt/application>
  34. *
  35. * <p>
  36. * Implement and configure a ServletContextListener in web.xml .
  37. * </p>
  38. *
  39. * &ltlistener> <listener-class>com.your_company.GuiceServletContextListener&lt/listener-class> &lt/listener>
  40. *
  41. * <p>
  42. * Configure Guice in your ServletContextListener implementation, and place the Injector in application scope.
  43. * </p>
  44. *
  45. * public class GuiceServletContextListener implements ServletContextListener {
  46. *
  47. * public void contextInitialized(ServletContextEvent event) { ServletContext ctx = event.getServletContext(); //when on
  48. * Java6, use ServiceLoader.load(com.google.inject.Module.class); Injector injector = Guice.createInjector(new
  49. * YourModule()); ctx.setAttribute(GuiceResolver.KEY, injector); }
  50. *
  51. * public void contextDestroyed(ServletContextEvent event) { ServletContext ctx = event.getServletContext();
  52. * ctx.removeAttribute(GuiceResolver.KEY); }
  53. *
  54. *}
  55. *
  56. * @author Dennis Byrne
  57. */
  58. public class GuiceResolver extends ManagedBeanResolver
  59. {
  60. public static final String KEY = "oam." + Injector.class.getName();
  61. @Override
  62. public Object getValue(ELContext ctx, Object base, Object property) throws NullPointerException,
  63. PropertyNotFoundException, ELException
  64. {
  65. if (base != null || !(property instanceof String))
  66. {
  67. return null;
  68. }
  69. FacesContext fctx = (FacesContext)ctx.getContext(FacesContext.class);
  70. if (fctx == null)
  71. {
  72. return null;
  73. }
  74. ExternalContext ectx = fctx.getExternalContext();
  75. if (ectx == null || ectx.getRequestMap().containsKey(property) || ectx.getSessionMap().containsKey(property)
  76. || ectx.getApplicationMap().containsKey(property))
  77. {
  78. return null;
  79. }
  80. ManagedBean managedBean = runtimeConfig(ctx).getManagedBean((String)property);
  81. return managedBean == null ? null : getValue(ctx, ectx, managedBean.getManagedBeanClass());
  82. }
  83. private Object getValue(ELContext ctx, ExternalContext ectx, Class<?> managedBeanClass)
  84. {
  85. Injector injector = (Injector)ectx.getApplicationMap().get(KEY);
  86. if (injector == null)
  87. {
  88. throw new FacesException("Could not find an instance of " + Injector.class.getName()
  89. + " in application scope using key '" + KEY + "'");
  90. }
  91. Object value = injector.getInstance(managedBeanClass);
  92. ctx.setPropertyResolved(true);
  93. return value;
  94. }
  95. }