View Javadoc
1   package ejava.examples.jndidemo.ejb;
2   
3   import javax.annotation.PostConstruct;import javax.annotation.Resource;
4   
5   import javax.ejb.EJB;
6   import javax.ejb.SessionContext;
7   import javax.ejb.Stateful;
8   import javax.naming.InitialContext;
9   import javax.naming.NamingException;
10  import javax.persistence.EntityManager;
11  import javax.persistence.PersistenceContext;
12  import javax.persistence.PersistenceContextType;
13  import javax.sql.DataSource;
14  
15  import ejava.util.jndi.JNDIUtil;
16  
17  /**
18   * This class is primarily an example of configuring an EJB through 
19   * @Annotations. There is no external ejb-jar.xml deployment descriptor 
20   * entries for this EJB.
21   * 
22   * @author jcstaff
23   *
24   */
25  @Stateful(name="BakeScheduler")
26  /**
27   * The PersistenceContext placed at this level is not common because from the
28   * Java class -- we can directly inject into the variables we want. However,
29   * this does simulate what is beiong done from ejb-jar.xml by injecting the 
30   * persistence context into the ENC so that it can be looked up within the 
31   * component using JNDI.
32   */
33  @PersistenceContext(unitName="jndidemo",
34  		name="persistence/jndidemo",
35  		type=PersistenceContextType.EXTENDED)
36  public class BakeSchedulerEJB 
37      extends SchedulerBase implements BakeSchedulerRemote {
38  
39      public String getName() { return "BakeSchedulerEJB"; }
40      
41      /*
42       * This injects a enity manager into the class variable. Since this 
43       * happens to be a stateless session bean -- an EXTENDED context is 
44       * chosen over a TX-scoped.
45       */
46      @PersistenceContext(
47      		unitName="jndidemo",    		
48      		type=PersistenceContextType.EXTENDED)
49      private EntityManager em;
50      
51      /**
52       * This instance of an entity manager is being taken out of the JNDI
53       * tree configured at the top of the class. This would be made available
54       * to any POJO called by the EJB. 
55       */
56      @Resource(name="persistence/jndidemo")
57      private EntityManager em2;
58  
59      /*
60       * This declaration obtains a reference to the SQL DataSource in the '
61       * global JNDI tree and initializes ds to that value
62       */
63      @Resource(mappedName="java:jboss/datasources/ExampleDS")
64      private DataSource ds;
65      
66      /*
67       * This declaration will cause the container to inject a SessionContext
68       * into the EJB at startup.
69       */
70      @Resource
71      protected void setSessionContext(SessionContext ctx) {
72          super.ctx = ctx;
73      }
74      
75      /* 
76       * We will manually assign this value using a JNDI lookup inside the 
77       * @PostConstruct method
78       */
79      protected CookLocal cook; 
80  
81      /*
82       * This reference will be injected by the container based on the data
83       * type declared. The resolved EJB also gets placed in the ejb/cook ENC
84       * name so that it can be looked up by code using the JNDI tree directly.
85       */
86      @EJB(name="ejb/cook")
87      protected CookLocal cook2; 
88  
89      /*
90       * This won't resolve to anything since this example does not use an 
91       * external deployment descriptor to give us a value.
92       */
93      @Resource(name="vals/message")
94      String message;
95  
96      @PostConstruct
97      public void init() {        
98          log.info("******************* BakeScheduler Created ******************");
99          log.debug("ctx=" + ctx);
100         log.debug("em=" + em);
101         log.debug("em2=" + em2);
102         log.debug("ds=" + ds);
103         //log.debug("persistence/jndidemo=" + ctx.lookup("persistence/jndidemo"));
104         log.debug("message=" + message);
105         log.debug("cook=" + cook);  //this will be null at this point
106         log.debug("cook2=" + cook2);
107         log.debug("ejb/cook=" + ctx.lookup("ejb/cook"));
108         cook = (CookLocal)ctx.lookup("ejb/cook");
109         try { 
110         	log.debug(new JNDIUtil().dump(new InitialContext(), "java:comp/env"));
111         } catch (NamingException ex) { log.fatal("" + ex); }
112     }
113 }