KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jsf > webapp > InjectionBean


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2006, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22
23 package org.jboss.test.jsf.webapp;
24
25 import java.util.Map JavaDoc;
26 import javax.annotation.PostConstruct;
27 import javax.annotation.PreDestroy;
28 import javax.annotation.Resource;
29 import javax.el.ELContext;
30 import javax.el.ExpressionFactory;
31 import javax.el.ValueExpression;
32 import javax.faces.context.FacesContext;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.sql.DataSource JavaDoc;
35
36 /**
37  * Tests resource injection and lifecycle annotations for JSF managed bean.
38  *
39  * @author Stan Silvert
40  */

41 public class InjectionBean {
42     
43     private DataSource JavaDoc defaultDS;
44     
45     private boolean postConstructCalled = false;
46     private boolean datasourceInjected = false;
47     
48     // This bean lives in the HttpSession. Save a reference here.
49
private MySessionBean mySessionBean = null;
50     
51     /** Creates a new instance of InjectionBean */
52     public InjectionBean() {
53     }
54     
55     public String JavaDoc getName() {
56         return "InjectionBean";
57     }
58     
59     public boolean getPostConstructCalled() {
60         return this.postConstructCalled;
61     }
62     
63     public boolean getDatasourceInjected() {
64         return this.datasourceInjected;
65     }
66
67     // TODO: test this annotation on a private method
68
@PostConstruct
69     private void testPostConstruct() {
70         this.postConstructCalled = true;
71         
72         Object JavaDoc dataSourceFromLookup = null;
73         try {
74             dataSourceFromLookup = new InitialContext JavaDoc().lookup("java:/DefaultDS");
75         } catch (Exception JavaDoc e) {
76             e.printStackTrace();
77         }
78         
79         if (defaultDS == dataSourceFromLookup) {
80             this.datasourceInjected = true;
81         }
82         
83         // doing this puts an instance of MySesisonBean into the session
84
ValueExpression preDestroyVe = expressionFactory().createValueExpression(elContext(), "#{mySessionBean}", MySessionBean.class);
85         this.mySessionBean = (MySessionBean)preDestroyVe.getValue(elContext());
86         
87     }
88     
89     private ELContext elContext() {
90         return FacesContext.getCurrentInstance().getELContext();
91     }
92     
93     private ExpressionFactory expressionFactory() {
94         return FacesContext.getCurrentInstance().getApplication().getExpressionFactory();
95     }
96     
97     @PreDestroy
98     public void testPreDestroy() {
99         this.mySessionBean.setPreDestroyCalled(true);
100     }
101     
102     @Resource(name="java:/DefaultDS")
103     public void setDefaultDS(DataSource JavaDoc dataSource) {
104         this.defaultDS = dataSource;
105     }
106     
107 }
108
Popular Tags