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_RegisterUserBean implements SessionBean 26 { 27 protected SessionContext sessionContext; 28 protected Context initialContext = null; 29 protected DataSource dataSource = null; 30 private UserTransaction utx = null; 31 32 44 public String createUser(String firstname, String lastname, String nickname, String email, String password, String regionName) throws RemoteException 45 { 46 String html = ""; 47 UserHome uHome; 48 User user; 49 RegionHome regionHome; 50 int regionId; 51 int userId; 52 String creationDate; 53 54 try 55 { 56 regionHome = (RegionHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Region"), RegionHome.class); 58 } 59 catch (Exception e) 60 { 61 throw new RemoteException ("Cannot lookup Region: " +e+"<br>\n"); 62 } 63 try 64 { 65 Region region = regionHome.findByName(regionName); 66 regionId = region.getId().intValue(); 67 } 68 catch (Exception e) 69 { 70 throw new RemoteException (" Region "+regionName+" does not exist in the database!<br>(got exception: " +e+")<br>\n"); 71 } 72 73 try 74 { 75 uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), 76 UserHome.class); 77 } 78 catch (Exception e) 79 { 80 throw new RemoteException ("Cannot lookup User: " +e+"<br>"); 81 } 82 utx = sessionContext.getUserTransaction(); 83 try 84 { 85 user = uHome.findByNickName(nickname); 86 88 html = "The nickname you have choosen is already taken by someone else. Please choose a new nickname.<br>"; 89 return html; 90 } 91 catch (Exception fe) 92 { 93 try 94 { 95 utx.begin(); 96 user = uHome.create(firstname, lastname, nickname, email, password, new Integer (regionId)); 97 user = uHome.findByNickName(nickname); 98 userId = user.getId().intValue(); 99 creationDate = user.getCreationDate(); 100 utx.commit(); 101 html = "User id :"+userId+"<br>\n" + 102 "Creation date :"+creationDate+"<br>\n" + 103 "Rating :"+user.getRating()+"<br>\n" + 104 "Balance :"+user.getBalance()+"<br>\n"; 105 } 106 catch (Exception e) 107 { 108 try 109 { 110 utx.rollback(); 111 throw new RemoteException ("User registration failed (got exception: " +e+")<br>"); 112 } 113 catch (Exception se) 114 { 115 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 116 } 117 } 118 return html; 119 } 120 121 122 } 123 124 125 126 127 128 130 133 public void ejbCreate() throws CreateException , RemoteException 134 { 135 } 136 137 138 public void ejbActivate() throws RemoteException {} 139 140 public void ejbPassivate() throws RemoteException {} 141 142 public void ejbRemove() throws RemoteException {} 143 144 145 154 public void setSessionContext(SessionContext sessionContext) throws RemoteException 155 { 156 this.sessionContext = sessionContext; 157 if (dataSource == null) 158 { 159 161 try 162 { 163 initialContext = new InitialContext (); 164 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 165 } 166 catch (Exception e) 167 { 168 throw new RemoteException ("Cannot get JNDI InitialContext"); 169 } 170 } 171 } 172 173 } 174 | Popular Tags |