View Javadoc
1   package ejava.examples.asyncmarket.web;
2   
3   import java.util.Enumeration;
4   import java.util.Properties;
5   
6   import javax.naming.Context;
7   import javax.naming.InitialContext;
8   import javax.naming.NamingException;
9   import javax.servlet.ServletContext;
10  
11  import org.slf4j.Logger;
12  import org.slf4j.LoggerFactory;
13  
14  import ejava.examples.asyncmarket.AuctionMgmt;
15  import ejava.examples.asyncmarket.Buyer;
16  import ejava.examples.asyncmarket.Seller;
17  import ejava.examples.asyncmarket.UserMgmt;
18  import ejava.examples.asyncmarket.ejb.AuctionMgmtLocal;
19  import ejava.examples.asyncmarket.ejb.AuctionMgmtRemote;
20  import ejava.examples.asyncmarket.ejb.BuyerLocal;
21  import ejava.examples.asyncmarket.ejb.BuyerRemote;
22  import ejava.examples.asyncmarket.ejb.SellerLocal;
23  import ejava.examples.asyncmarket.ejb.SellerRemote;
24  import ejava.examples.asyncmarket.ejb.UserMgmtLocal;
25  import ejava.examples.asyncmarket.ejb.UserMgmtRemote;
26  import ejava.util.ejb.EJBClient;
27  
28  /**
29   * This is a helper class used to help locate EJBs when running in a 
30   * remote web container -- such as Jetty.
31   *  
32   * TODO: factor out JNDI properties into a jndi.properties file
33   */
34  public class JNDIHelper {
35      private static final Logger log = LoggerFactory.getLogger(JNDIHelper.class);
36      public static final String AUCTION_MGMT_REMOTE_JNDI=
37      	EJBClient.getRemoteLookupName("asyncMarketEAR", "asyncMarketEJB", 
38  			"AuctionMgmtEJB", AuctionMgmtRemote.class.getName());    
39  	public static final String USER_MGMT_REMOTE_JNDI =
40  		EJBClient.getRemoteLookupName("asyncMarketEAR", "asyncMarketEJB", 
41  			"UserMgmtEJB", UserMgmtRemote.class.getName());
42  	public static final String SELLER_REMOTE_JNDI = 
43  		EJBClient.getRemoteLookupName("asyncMarketEAR", "asyncMarketEJB", 
44  			"SellerEJB", SellerRemote.class.getName());	
45  	public static final String BUYER_REMOTE_JNDI = 
46  		EJBClient.getRemoteLookupName("asyncMarketEAR", "asyncMarketEJB", 
47  			"BuyerEJB", BuyerRemote.class.getName());
48  
49      private Context jndi;
50      
51      public JNDIHelper() throws NamingException {
52      	    jndi=new InitialContext();
53      }
54      
55      public void close() {
56          	try {
57          		jndi.close();
58          	} catch (NamingException ex) {
59          		throw new RuntimeException("unexpected error during JNDI.close()", ex);
60          	}
61      }
62      
63      public AuctionMgmt getAuctionMgmt() throws NamingException {
64      	    return lookup(AuctionMgmt.class, jndi, AUCTION_MGMT_REMOTE_JNDI);
65      }
66      
67      public UserMgmt getUserMgmt() throws NamingException {
68      	    return lookup(UserMgmt.class, jndi, USER_MGMT_REMOTE_JNDI);
69      }
70  
71      public Seller getSeller() throws NamingException {
72          return lookup(Seller.class, jndi, SELLER_REMOTE_JNDI);
73      }
74      
75      public Buyer getBuyer() throws NamingException {
76      	    return lookup(Buyer.class, jndi, BUYER_REMOTE_JNDI);
77      }
78  
79      
80  //    private Context getInitialContext(ServletContext context) 
81  //        throws NamingException {
82  //        
83  //        //build an InitialContext from Servlet.init properties in web.xml
84  //        Properties jndiProperties = new Properties();
85  //        for(Enumeration<?> e=context.getInitParameterNames();
86  //            e.hasMoreElements(); ) {
87  //            String key = (String)e.nextElement();
88  //            String value=(String)context.getInitParameter(key);
89  //            if (key.startsWith("java.naming")) {
90  //                jndiProperties.put(key, value);
91  //            }                    
92  //        }
93  //        log.debug("jndiProperties=" + jndiProperties);
94  //        InitialContext jndi = new InitialContext(jndiProperties);
95  //        log.debug("jndi=" + jndiProperties);
96  //        return jndi;
97  //    }
98  
99      @SuppressWarnings("unchecked")
100 	private <T> T lookup(Class<T> lazz, Context jndi, String remoteJNDI) 
101     		throws NamingException { 
102         T object = null;            
103         object = (T)jndi.lookup(remoteJNDI);
104         log.debug("object=" + object);
105         return object;
106     }
107 }