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