/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 */
17package org.jboss.as.weld.webtier.jsf;
18
19import org.jboss.weld.el.WeldELContextListener;
20
21import javax.el.ELResolver;
22import javax.el.ExpressionFactory;
23import javax.enterprise.inject.spi.BeanManager;
24import javax.faces.application.Application;
25import javax.naming.InitialContext;
26import javax.naming.NamingException;
27
28/**
29 * @author pmuir
30 */
31public class WeldApplication extends ForwardingApplication {
32
33 private static class AdjustableELResolver extends ForwardingELResolver {
34
35 private ELResolver delegate;
36
37 public void setDelegate(ELResolver delegate) {
38 this.delegate = delegate;
39 }
40
41 @Override
42 protected ELResolver delegate() {
43 return delegate;
44 }
45 }
46
47 private final Application application;
48 private final AdjustableELResolver elResolver;
49
50 private volatile ExpressionFactory expressionFactory;
51 private volatile boolean initialized = false;
52 private volatile BeanManager beanManager;
53
54 public WeldApplication(Application application) {
55 this.application = application;
56 application.addELContextListener(new WeldELContextListener());
57 elResolver = new AdjustableELResolver();
58 elResolver.setDelegate(new DummyELResolver());
59 application.addELResolver(elResolver);
60 }
61
62 private void init() {
63 if (!initialized && beanManager() != null) {
64 synchronized (this) {
65 if(!initialized && beanManager() != null) {
66 elResolver.setDelegate(beanManager().getELResolver());
67 initialized = true;
68 }
69 }
70 }
71 }
72
73 @Override
74 protected Application delegate() {
75 init();
76 return application;
77 }
78
79 @Override
80 public ExpressionFactory getExpressionFactory() {
81 init();
82 // may be improved for thread safety, but right now the only risk is to invoke wrapExpressionFactory
83 // multiple times for concurrent threads. This is ok, as the call is
84 if (expressionFactory == null) {
85 synchronized (this) {
86 if (expressionFactory == null) {
87 BeanManager bm = beanManager();
88 if (bm == null) {
89 expressionFactory = application.getExpressionFactory();
90 } else {
91 expressionFactory = bm.wrapExpressionFactory(application.getExpressionFactory());
92 }
93 }
94 }
95 }
96 return expressionFactory;
97 }
98
99 private BeanManager beanManager() {
100 if (beanManager == null) {
101 synchronized (this) {
102 if (beanManager == null) {
103 try {
104 beanManager = (BeanManager) new InitialContext().lookup("java:comp/BeanManager");
105 } catch (NamingException e) {
106 return null;
107 }
108 }
109 }
110 }
111 return beanManager;
112 }
113
114}