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.apache.commons.logging.Log;
12  import org.apache.commons.logging.LogFactory;
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 Log log = LogFactory.getLog(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(ServletContext context) throws NamingException {
52      	jndi=getInitialContext(context);
53      }
54      public void close() {
55      	try {
56      		jndi.close();
57      	} catch (NamingException ex) {
58      		throw new RuntimeException("unexpected error during JNDI.close()", ex);
59      	}
60      }
61      
62      public AuctionMgmt getAuctionMgmt() throws NamingException {
63      	return lookup(AuctionMgmt.class, jndi, AUCTION_MGMT_REMOTE_JNDI);
64      }
65      
66      public UserMgmt getUserMgmt() throws NamingException {
67      	return lookup(UserMgmt.class, jndi, USER_MGMT_REMOTE_JNDI);
68      }
69  
70      public Seller getSeller() throws NamingException {
71      	return lookup(Seller.class, jndi, SELLER_REMOTE_JNDI);
72      }
73      
74      public Buyer getBuyer() throws NamingException {
75      	return lookup(Buyer.class, jndi, BUYER_REMOTE_JNDI);
76      }
77  
78      
79      private Context getInitialContext(ServletContext context) 
80          throws NamingException {
81          
82          //build an InitialContext from Servlet.init properties in web.xml
83          Properties jndiProperties = new Properties();
84          for(Enumeration<?> e=context.getInitParameterNames();
85              e.hasMoreElements(); ) {
86              String key = (String)e.nextElement();
87              String value=(String)context.getInitParameter(key);
88              if (key.startsWith("java.naming")) {
89                  jndiProperties.put(key, value);
90              }                    
91          }
92          log.debug("jndiProperties=" + jndiProperties);
93          InitialContext jndi = new InitialContext(jndiProperties);
94          log.debug("jndi=" + jndiProperties);
95          return jndi;
96      }
97  
98      @SuppressWarnings("unchecked")
99  	private <T> T lookup(Class<T> lazz, Context jndi, String remoteJNDI) 
100     		throws NamingException { 
101         T object = null;            
102         object = (T)jndi.lookup(remoteJNDI);
103         log.debug("object=" + object);
104         return object;
105     }
106 }