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
30
31
32
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 }