View Javadoc
1   package ejava.examples.jndidemo.ejb;
2   
3   import javax.annotation.PostConstruct;
4   
5   import javax.annotation.Resource;
6   
7   import javax.ejb.SessionContext;
8   import javax.ejb.Stateless;
9   import javax.enterprise.inject.Any;
10  import javax.enterprise.inject.Instance;
11  import javax.inject.Inject;
12  import javax.persistence.EntityManager;
13  import javax.sql.DataSource;
14  
15  import ejava.examples.jndidemo.Cook;
16  import ejava.examples.jndidemo.JndiDemo;
17  import ejava.examples.jndidemo.CookQualifier;
18  import ejava.examples.jndidemo.Scheduler;
19  import ejava.examples.jndidemo.dao.JPASchedulerDAOImpl;
20  import ejava.examples.jndidemo.dao.SchedulerDAO;
21  
22  @Stateless
23  public class TrainSchedulerEJB 
24      extends SchedulerBase implements TrainSchedulerRemote {
25  
26      public String getName() { return TrainSchedulerEJB.class.getSimpleName(); }
27  
28      /**
29       * This will inject a persistence context using a textual name qualifier 
30       */
31      @Inject @JndiDemo
32      private EntityManager em;
33      
34      /**
35       * This will inject a javax.sql.DataSource
36       */
37      @Inject @JndiDemo
38      private DataSource ds;
39      
40      /**
41       * This will inject a DAO by interface type
42       */
43      @Inject
44      protected SchedulerDAO schedulerDAO;
45      
46      /**
47       * This will inject a DAO by class type
48       */
49      @Inject
50      protected JPASchedulerDAOImpl jpaSchedulerDAOImpl;
51  
52      /**
53       * This will inject an EJB based on an Annotation qualifier
54       */
55      @Inject @Cook
56      protected Scheduler cook; 
57  
58      /**
59       * This will inject the ability to get an Scheduler when the qualifier is 
60       * known at runtime.
61       */
62      @Inject @Any
63      protected Instance<Scheduler> anyCook;
64      
65      /**
66       * This will be looked up at runtime during the @PostConstruct
67       */
68      protected Scheduler cook2; 
69  
70      /**
71       * This will inject a String based on an annotation qualifier
72       */
73      @Inject @JndiDemo
74      String message;
75  
76      @Resource
77      protected void setSessionContext(SessionContext ctx) {
78          super.ctx = ctx;
79      }
80  
81      @PostConstruct
82      public void init() {        
83          log.info("******************* TrainScheduler Created ******************");
84          log.debug("ctx=" + ctx);
85          log.debug("em=" + em);
86          log.debug("ds=" + ds);
87          log.debug("message=" + message);
88          log.debug("cook=" + cook);
89          cook2 = anyCook.select(new CookQualifier()).get();
90          log.debug("cook2=" + cook2);
91          log.debug("schedulerDAO=" + schedulerDAO);
92          log.debug("jpaSchedulerDAOImpl=" + jpaSchedulerDAOImpl);
93      }
94  }