001: /*
002: * JBoss, Home of Professional Open Source
003: * Copyright 2006, JBoss Inc., and individual contributors as indicated
004: * by the @authors tag. See the copyright.txt in the distribution for a
005: * full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022:
023: package org.jboss.test.jsf.webapp;
024:
025: import java.util.Map;
026: import javax.annotation.PostConstruct;
027: import javax.annotation.PreDestroy;
028: import javax.annotation.Resource;
029: import javax.el.ELContext;
030: import javax.el.ExpressionFactory;
031: import javax.el.ValueExpression;
032: import javax.faces.context.FacesContext;
033: import javax.naming.InitialContext;
034: import javax.sql.DataSource;
035:
036: /**
037: * Tests resource injection and lifecycle annotations for JSF managed bean.
038: *
039: * @author Stan Silvert
040: */
041: public class InjectionBean {
042:
043: private DataSource defaultDS;
044:
045: private boolean postConstructCalled = false;
046: private boolean datasourceInjected = false;
047:
048: // This bean lives in the HttpSession. Save a reference here.
049: private MySessionBean mySessionBean = null;
050:
051: /** Creates a new instance of InjectionBean */
052: public InjectionBean() {
053: }
054:
055: public String getName() {
056: return "InjectionBean";
057: }
058:
059: public boolean getPostConstructCalled() {
060: return this .postConstructCalled;
061: }
062:
063: public boolean getDatasourceInjected() {
064: return this .datasourceInjected;
065: }
066:
067: // TODO: test this annotation on a private method
068: @PostConstruct
069: private void testPostConstruct() {
070: this .postConstructCalled = true;
071:
072: Object dataSourceFromLookup = null;
073: try {
074: dataSourceFromLookup = new InitialContext()
075: .lookup("java:/DefaultDS");
076: } catch (Exception e) {
077: e.printStackTrace();
078: }
079:
080: if (defaultDS == dataSourceFromLookup) {
081: this .datasourceInjected = true;
082: }
083:
084: // doing this puts an instance of MySesisonBean into the session
085: ValueExpression preDestroyVe = expressionFactory()
086: .createValueExpression(elContext(), "#{mySessionBean}",
087: MySessionBean.class);
088: this .mySessionBean = (MySessionBean) preDestroyVe
089: .getValue(elContext());
090:
091: }
092:
093: private ELContext elContext() {
094: return FacesContext.getCurrentInstance().getELContext();
095: }
096:
097: private ExpressionFactory expressionFactory() {
098: return FacesContext.getCurrentInstance().getApplication()
099: .getExpressionFactory();
100: }
101:
102: @PreDestroy
103: public void testPreDestroy() {
104: this .mySessionBean.setPreDestroyCalled(true);
105: }
106:
107: @Resource(name="java:/DefaultDS")
108: public void setDefaultDS(DataSource dataSource) {
109: this.defaultDS = dataSource;
110: }
111:
112: }
|