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_StoreCommentBean implements SessionBean 26 { 27 protected SessionContext sessionContext; 28 protected Context initialContext = null; 29 protected DataSource dataSource = null; 30 private UserTransaction utx = null; 31 32 42 public void createComment(Integer fromId, Integer toId, Integer itemId, int rating, String comment) throws RemoteException 43 { 44 User to; 46 try 47 { 48 UserHome uHome = (UserHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/User"), 49 UserHome.class); 50 to = uHome.findByPrimaryKey(new UserPK(toId)); 51 } 52 catch (Exception e) 53 { 54 throw new RemoteException ("Cannot lookup User ("+toId+"): " +e+"<br>"); 55 } 56 CommentHome cHome; 57 try 58 { 59 cHome = (CommentHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Comment"), 60 CommentHome.class); 61 } 62 catch (Exception e) 63 { 64 throw new RemoteException ("Cannot lookup Comment: " +e+"<br>"); 65 } 66 utx = sessionContext.getUserTransaction(); 67 try 68 { 69 utx.begin(); 70 Comment c = cHome.create(fromId, toId, itemId, rating, comment); 71 to.updateRating(rating); 72 utx.commit(); 73 } 74 catch (Exception e) 75 { 76 try 77 { 78 utx.rollback(); 79 throw new RemoteException ("Error while storing the comment (got exception: " +e+")<br>"); 80 } 81 catch (Exception se) 82 { 83 throw new RemoteException ("Transaction rollback failed: " + e +"<br>"); 84 } 85 } 86 87 } 88 89 90 91 93 96 public void ejbCreate() throws CreateException , RemoteException 97 { 98 } 99 100 101 public void ejbActivate() throws RemoteException {} 102 103 public void ejbPassivate() throws RemoteException {} 104 105 public void ejbRemove() throws RemoteException {} 106 107 108 117 public void setSessionContext(SessionContext sessionContext) throws RemoteException 118 { 119 this.sessionContext = sessionContext; 120 if (dataSource == null) 121 { 122 124 try 125 { 126 initialContext = new InitialContext (); 127 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 128 } 129 catch (Exception e) 130 { 131 throw new RemoteException ("Cannot get JNDI InitialContext"); 132 } 133 } 134 } 135 136 } 137 | Popular Tags |