1 22 23 package org.jboss.test.jsf.webapp; 24 25 import java.util.Map ; 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 ; 34 import javax.sql.DataSource ; 35 36 41 public class InjectionBean { 42 43 private DataSource defaultDS; 44 45 private boolean postConstructCalled = false; 46 private boolean datasourceInjected = false; 47 48 private MySessionBean mySessionBean = null; 50 51 52 public InjectionBean() { 53 } 54 55 public String 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 @PostConstruct 69 private void testPostConstruct() { 70 this.postConstructCalled = true; 71 72 Object dataSourceFromLookup = null; 73 try { 74 dataSourceFromLookup = new InitialContext ().lookup("java:/DefaultDS"); 75 } catch (Exception e) { 76 e.printStackTrace(); 77 } 78 79 if (defaultDS == dataSourceFromLookup) { 80 this.datasourceInjected = true; 81 } 82 83 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 dataSource) { 104 this.defaultDS = dataSource; 105 } 106 107 } 108 | Popular Tags |