Advertisement
Petronom

EJBProvider - WELD

Aug 19th, 2016
1,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.99 KB | None | 0 0
  1. import cz.pfreiberg.test.ejb.session.TestSessionEJBLocal;
  2.  
  3. import javax.enterprise.inject.Produces;
  4.  
  5. import javax.naming.InitialContext;
  6. import javax.naming.NamingException;
  7.  
  8. public class EJBProvider {
  9.  
  10.     public EJBProvider() {
  11.     }
  12.  
  13.     /**
  14.      * Now you can use @Inject for this EJB across your application.
  15.      * Example: @Inject TestSessionEJBLocal testEJB;
  16.      */
  17.     @Produces
  18.     public TestSessionEJBLocal getTestSessionEJBLocal() {
  19.         return jndiLookup("java:global/TEST/TestSessionEJB!cz.pfreiberg.test.ejb.session.TestSessionEJBLocal",
  20.                           TestSessionEJBLocal.class);
  21.     }
  22.  
  23.     private <T> T jndiLookup(String name, Class<T> type) {
  24.         try {
  25.             InitialContext ctx = new InitialContext();
  26.             return type.cast(ctx.lookup(name));
  27.         } catch (NamingException e) {
  28.             String errorMessage = "Error during JNDI lookup for " + name;
  29.             throw new RuntimeException(errorMessage, e);
  30.         }
  31.     }
  32.  
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement