KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > rubis > beans > SB_BrowseRegionsBean


1 package edu.rice.rubis.beans;
2
3 import java.rmi.RemoteException JavaDoc;
4 import javax.ejb.SessionBean JavaDoc;
5 import javax.ejb.SessionContext JavaDoc;
6 import javax.ejb.FinderException JavaDoc;
7 import javax.ejb.ObjectNotFoundException JavaDoc;
8 import javax.ejb.CreateException JavaDoc;
9 import javax.ejb.RemoveException JavaDoc;
10 import javax.ejb.EJBException JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.InitialContext JavaDoc;
13 import javax.rmi.PortableRemoteObject JavaDoc;
14 import javax.sql.DataSource JavaDoc;
15 import java.io.Serializable JavaDoc;
16 import javax.transaction.UserTransaction JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.net.URLEncoder JavaDoc;
20
21 /**
22  * This is a stateless session bean used to get the list of regions
23  * from the database.
24  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
25  * @version 1.1
26  */

27
28 public class SB_BrowseRegionsBean implements SessionBean JavaDoc
29 {
30   protected SessionContext JavaDoc sessionContext;
31   protected Context JavaDoc initialContext = null;
32   protected DataSource JavaDoc dataSource = null;
33   private UserTransaction JavaDoc utx = null;
34
35
36   /**
37    * Get all the regions.
38    *
39    * @return a string that is the list of regions in html format
40    * @since 1.1
41    */

42   public String JavaDoc getRegions() throws RemoteException JavaDoc
43   {
44     
45     Collection JavaDoc list;
46     RegionHome home = null;
47     Region reg;
48     String JavaDoc html = "";
49
50     // Connecting to Region Home
51
try
52     {
53       home = (RegionHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Region"), RegionHome.class);
54     }
55     catch (Exception JavaDoc e)
56     {
57       throw new RemoteException JavaDoc("Cannot lookup Region: " +e);
58     }
59
60     utx = sessionContext.getUserTransaction();
61
62     try
63     {
64       utx.begin();
65       list = home.findAllRegions();
66       Iterator JavaDoc 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 JavaDoc e)
75     {
76       try
77       {
78         utx.rollback();
79         throw new RemoteException JavaDoc("Exception getting region list: " + e);
80       }
81       catch (Exception JavaDoc se)
82       {
83         throw new RemoteException JavaDoc("Transaction rollback failed: " + e);
84       }
85     }
86     return html;
87   }
88   
89
90   /**
91    * Region related printed functions
92    *
93    * @param region the region to display
94    * @return a string in html format
95    * @since 1.1
96    */

97
98   public String JavaDoc printRegion(Region region) throws RemoteException JavaDoc
99   {
100     String JavaDoc html;
101     try
102     {
103       String JavaDoc 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 JavaDoc re)
107     {
108       throw new RemoteException JavaDoc("Unable to print Region (exception: "+re+")");
109     }
110     return html;
111   }
112
113
114   // ======================== EJB related methods ============================
115

116   /**
117    * This method is empty for a stateless session bean
118    */

119   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
120   {
121   }
122
123   /** This method is empty for a stateless session bean */
124   public void ejbActivate() throws RemoteException JavaDoc {}
125   /** This method is empty for a stateless session bean */
126   public void ejbPassivate() throws RemoteException JavaDoc {}
127   /** This method is empty for a stateless session bean */
128   public void ejbRemove() throws RemoteException JavaDoc {}
129
130
131   /**
132    * Sets the associated session context. The container calls this method
133    * after the instance creation. This method is called with no transaction context.
134    * We also retrieve the Home interfaces of all RUBiS's beans.
135    *
136    * @param sessionContext - A SessionContext interface for the instance.
137    * @exception RemoteException - Thrown if the instance could not perform the function
138    * requested by the container because of a system-level error.
139    */

140   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
141   {
142     this.sessionContext = sessionContext;
143     if (dataSource == null)
144     {
145       // Finds DataSource from JNDI
146

147       try
148       {
149         initialContext = new InitialContext JavaDoc();
150         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
151       }
152       catch (Exception JavaDoc e)
153       {
154         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
155       }
156     }
157   }
158
159 }
160
Popular Tags