PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/jboss-as-7.1.1.Final/weld/src/main/java/org/jboss/as/weld/webtier/jsf/WeldApplication.java

#
Java | 114 lines | 78 code | 15 blank | 21 comment | 17 complexity | 2fc25cda6ce246e2c5b2e99952eda7e7 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2008, Red Hat Middleware LLC, and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jboss.as.weld.webtier.jsf;
  18. import org.jboss.weld.el.WeldELContextListener;
  19. import javax.el.ELResolver;
  20. import javax.el.ExpressionFactory;
  21. import javax.enterprise.inject.spi.BeanManager;
  22. import javax.faces.application.Application;
  23. import javax.naming.InitialContext;
  24. import javax.naming.NamingException;
  25. /**
  26. * @author pmuir
  27. */
  28. public class WeldApplication extends ForwardingApplication {
  29. private static class AdjustableELResolver extends ForwardingELResolver {
  30. private ELResolver delegate;
  31. public void setDelegate(ELResolver delegate) {
  32. this.delegate = delegate;
  33. }
  34. @Override
  35. protected ELResolver delegate() {
  36. return delegate;
  37. }
  38. }
  39. private final Application application;
  40. private final AdjustableELResolver elResolver;
  41. private volatile ExpressionFactory expressionFactory;
  42. private volatile boolean initialized = false;
  43. private volatile BeanManager beanManager;
  44. public WeldApplication(Application application) {
  45. this.application = application;
  46. application.addELContextListener(new WeldELContextListener());
  47. elResolver = new AdjustableELResolver();
  48. elResolver.setDelegate(new DummyELResolver());
  49. application.addELResolver(elResolver);
  50. }
  51. private void init() {
  52. if (!initialized && beanManager() != null) {
  53. synchronized (this) {
  54. if(!initialized && beanManager() != null) {
  55. elResolver.setDelegate(beanManager().getELResolver());
  56. initialized = true;
  57. }
  58. }
  59. }
  60. }
  61. @Override
  62. protected Application delegate() {
  63. init();
  64. return application;
  65. }
  66. @Override
  67. public ExpressionFactory getExpressionFactory() {
  68. init();
  69. // may be improved for thread safety, but right now the only risk is to invoke wrapExpressionFactory
  70. // multiple times for concurrent threads. This is ok, as the call is
  71. if (expressionFactory == null) {
  72. synchronized (this) {
  73. if (expressionFactory == null) {
  74. BeanManager bm = beanManager();
  75. if (bm == null) {
  76. expressionFactory = application.getExpressionFactory();
  77. } else {
  78. expressionFactory = bm.wrapExpressionFactory(application.getExpressionFactory());
  79. }
  80. }
  81. }
  82. }
  83. return expressionFactory;
  84. }
  85. private BeanManager beanManager() {
  86. if (beanManager == null) {
  87. synchronized (this) {
  88. if (beanManager == null) {
  89. try {
  90. beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
  91. } catch (NamingException e) {
  92. return null;
  93. }
  94. }
  95. }
  96. }
  97. return beanManager;
  98. }
  99. }