1 package edu.rice.rubis.beans; 2 3 import java.rmi.RemoteException ; 4 import javax.ejb.SessionBean ; 5 import javax.ejb.SessionContext ; 6 import javax.ejb.FinderException ; 7 import javax.ejb.ObjectNotFoundException ; 8 import javax.ejb.CreateException ; 9 import javax.ejb.RemoveException ; 10 import javax.ejb.EJBException ; 11 import javax.naming.Context ; 12 import javax.naming.InitialContext ; 13 import javax.rmi.PortableRemoteObject ; 14 import javax.sql.DataSource ; 15 import java.io.Serializable ; 16 import javax.transaction.UserTransaction ; 17 18 24 25 public class SB_BuyNowBean implements SessionBean 26 { 27 protected SessionContext sessionContext; 28 protected Context initialContext = null; 29 protected DataSource dataSource = null; 30 32 33 39 public String getBuyNowForm(Integer itemId, String username, String password) throws RemoteException 40 { 41 int userId = -1; 42 String html = ""; 43 44 if ((username != null && !username.equals("")) || (password != null && !password.equals(""))) 46 { 47 SB_AuthHome authHome = null; 48 SB_Auth auth = null; 49 try 50 { 51 authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class); 52 auth = authHome.create(); 53 } 54 catch (Exception e) 55 { 56 throw new RemoteException ("Cannot lookup SB_Auth: " +e); 57 } 58 try 59 { 60 userId = auth.authenticate(username, password); 61 } 62 catch (Exception e) 63 { 64 throw new RemoteException ("Authentication failed: " +e); 65 } 66 if (userId == -1) 67 { 68 html = (" You don't have an account on RUBiS!<br>You have to register first.<br>"); 69 return html; 70 } 71 } 72 ItemHome itemHome; 74 try 75 { 76 itemHome = (ItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Item"), 77 ItemHome.class); 78 } 79 catch (Exception e) 80 { 81 throw new RemoteException ("Cannot lookup Item: " +e+"<br>"); 82 } 83 try 84 { 85 Item item = itemHome.findByPrimaryKey(new ItemPK(itemId)); 86 87 html = printItemDescriptionToBuyNow(item, userId); 89 } 90 catch (Exception e) 91 { 92 throw new RemoteException ("Exception getting the item information: "+ e +"<br>"); 93 } 94 95 return html; 96 } 97 98 104 public String printItemDescriptionToBuyNow(Item item, int userId) throws RemoteException 105 { 106 String html = ""; 107 try 108 { 109 String itemName = item.getName(); 110 html = html + "<TABLE width=\"100%\" bgcolor=\"#CCCCFF\">\n<TR><TD align=\"center\" width=\"100%\"><FONT size=\"4\" color=\"#000000\"><B>You are ready to buy this item: "+itemName+"</B></FONT></TD></TR>\n</TABLE><p>\n" + item.printItemDescriptionToBuyNow(userId); 111 ; 112 } 113 catch (RemoteException re) 114 { 115 throw new RemoteException ("Unable to print Item description (exception: "+re+")<br>\n"); 116 } 117 return html; 118 } 119 120 121 122 123 125 128 public void ejbCreate() throws CreateException , RemoteException 129 { 130 } 131 132 133 public void ejbActivate() throws RemoteException {} 134 135 public void ejbPassivate() throws RemoteException {} 136 137 public void ejbRemove() throws RemoteException {} 138 139 140 149 public void setSessionContext(SessionContext sessionContext) throws RemoteException 150 { 151 this.sessionContext = sessionContext; 152 if (dataSource == null) 153 { 154 156 try 157 { 158 initialContext = new InitialContext (); 159 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 160 } 161 catch (Exception e) 162 { 163 throw new RemoteException ("Cannot get JNDI InitialContext"); 164 } 165 } 166 } 167 168 } 169 | Popular Tags |