KickJava   Java API By Example, From Geeks To Geeks.

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


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
18 /**
19  * This is a stateless session bean used to register a new user.
20  *
21  * @author <a HREF="mailto:cecchet@rice.edu">Emmanuel Cecchet</a> and <a HREF="mailto:julie.marguerite@inrialpes.fr">Julie Marguerite</a>
22  * @version 1.1
23  */

24
25 public class SB_RegisterUserBean implements SessionBean JavaDoc
26 {
27   protected SessionContext JavaDoc sessionContext;
28   protected Context JavaDoc initialContext = null;
29   protected DataSource JavaDoc dataSource = null;
30   private UserTransaction JavaDoc utx = null;
31
32   /**
33    * Create a new user.
34    *
35    * @param firstname user's first name
36    * @param lastname user's last name
37    * @param nickname user's nick name
38    * @param email user's email
39    * @param password user's password
40    * @param regionName name of the region where the user live
41    * @return a string in html format
42    * @since 1.1
43    */

44   public String JavaDoc createUser(String JavaDoc firstname, String JavaDoc lastname, String JavaDoc nickname, String JavaDoc email, String JavaDoc password, String JavaDoc regionName) throws RemoteException JavaDoc
45   {
46     String JavaDoc html = "";
47     UserHome uHome;
48     User user;
49     RegionHome regionHome;
50     int regionId;
51     int userId;
52     String JavaDoc creationDate;
53
54       try
55       {
56         // Connecting to region Home thru JNDI
57
regionHome = (RegionHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/Region"), RegionHome.class);
58       }
59       catch (Exception JavaDoc e)
60       {
61         throw new RemoteException JavaDoc("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 JavaDoc e)
69       {
70         throw new RemoteException JavaDoc(" 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 JavaDoc e)
79       {
80         throw new RemoteException JavaDoc("Cannot lookup User: " +e+"<br>");
81       }
82       utx = sessionContext.getUserTransaction();
83       try
84       {
85         user = uHome.findByNickName(nickname);
86         /* If an exception has not be thrown at this point, it means that
87            the nickname already exists. */

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 JavaDoc fe)
92       {
93         try
94         {
95           utx.begin();
96           user = uHome.create(firstname, lastname, nickname, email, password, new Integer JavaDoc(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 JavaDoc e)
107         {
108           try
109           {
110             utx.rollback();
111             throw new RemoteException JavaDoc("User registration failed (got exception: " +e+")<br>");
112           }
113           catch (Exception JavaDoc se)
114           {
115             throw new RemoteException JavaDoc("Transaction rollback failed: " + e +"<br>");
116           }
117         }
118         return html;
119       }
120
121
122   }
123   
124
125
126
127
128   // ======================== EJB related methods ============================
129

130   /**
131    * This method is empty for a stateless session bean
132    */

133   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
134   {
135   }
136
137   /** This method is empty for a stateless session bean */
138   public void ejbActivate() throws RemoteException JavaDoc {}
139   /** This method is empty for a stateless session bean */
140   public void ejbPassivate() throws RemoteException JavaDoc {}
141   /** This method is empty for a stateless session bean */
142   public void ejbRemove() throws RemoteException JavaDoc {}
143
144
145   /**
146    * Sets the associated session context. The container calls this method
147    * after the instance creation. This method is called with no transaction context.
148    * We also retrieve the Home interfaces of all RUBiS's beans.
149    *
150    * @param sessionContext - A SessionContext interface for the instance.
151    * @exception RemoteException - Thrown if the instance could not perform the function
152    * requested by the container because of a system-level error.
153    */

154   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
155   {
156     this.sessionContext = sessionContext;
157     if (dataSource == null)
158     {
159       // Finds DataSource from JNDI
160

161       try
162       {
163         initialContext = new InitialContext JavaDoc();
164         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
165       }
166       catch (Exception JavaDoc e)
167       {
168         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
169       }
170     }
171   }
172
173 }
174
Popular Tags