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 import java.util.Collection ; 18 import java.util.Iterator ; 19 import java.net.URLEncoder ; 20 21 27 28 public class SB_BrowseRegionsBean implements SessionBean 29 { 30 protected SessionContext sessionContext; 31 protected Context initialContext = null; 32 protected DataSource dataSource = null; 33 private UserTransaction utx = null; 34 35 36 42 public String getRegions() throws RemoteException 43 { 44 45 Collection list; 46 RegionHome home = null; 47 Region reg; 48 String html = ""; 49 50 try 52 { 53 home = (RegionHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Region"), RegionHome.class); 54 } 55 catch (Exception e) 56 { 57 throw new RemoteException ("Cannot lookup Region: " +e); 58 } 59 60 utx = sessionContext.getUserTransaction(); 61 62 try 63 { 64 utx.begin(); 65 list = home.findAllRegions(); 66 Iterator it = list.iterator(); 67 while (it.hasNext()) 68 { 69 reg = (Region)it.next(); 70 html = html + printRegion(reg); 71 } 72 utx.commit(); 73 } 74 catch (Exception e) 75 { 76 try 77 { 78 utx.rollback(); 79 throw new RemoteException ("Exception getting region list: " + e); 80 } 81 catch (Exception se) 82 { 83 throw new RemoteException ("Transaction rollback failed: " + e); 84 } 85 } 86 return html; 87 } 88 89 90 97 98 public String printRegion(Region region) throws RemoteException 99 { 100 String html; 101 try 102 { 103 String name = region.getName(); 104 html = "<a HREF=\""+BeanConfig.context+"/servlet/edu.rice.rubis.beans.servlets.BrowseCategories?region="+URLEncoder.encode(name)+"\">"+name+"</a><br>\n"; 105 } 106 catch (RemoteException re) 107 { 108 throw new RemoteException ("Unable to print Region (exception: "+re+")"); 109 } 110 return html; 111 } 112 113 114 116 119 public void ejbCreate() throws CreateException , RemoteException 120 { 121 } 122 123 124 public void ejbActivate() throws RemoteException {} 125 126 public void ejbPassivate() throws RemoteException {} 127 128 public void ejbRemove() throws RemoteException {} 129 130 131 140 public void setSessionContext(SessionContext sessionContext) throws RemoteException 141 { 142 this.sessionContext = sessionContext; 143 if (dataSource == null) 144 { 145 147 try 148 { 149 initialContext = new InitialContext (); 150 dataSource = (DataSource )initialContext.lookup("java:comp/env/jdbc/rubis"); 151 } 152 catch (Exception e) 153 { 154 throw new RemoteException ("Cannot get JNDI InitialContext"); 155 } 156 } 157 } 158 159 } 160 | Popular Tags |