KickJava   Java API By Example, From Geeks To Geeks.

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


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 build the html form to put a bid.
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_PutBidBean implements SessionBean JavaDoc
26 {
27   protected SessionContext JavaDoc sessionContext;
28   protected Context JavaDoc initialContext = null;
29   protected DataSource JavaDoc dataSource = null;
30   //private UserTransaction utx = null;
31

32
33   /**
34    * Authenticate the user and get the information to build the html form.
35    *
36    * @return a string in html format
37    * @since 1.1
38    */

39   public String JavaDoc getBiddingForm(Integer JavaDoc itemId, String JavaDoc username, String JavaDoc password) throws RemoteException JavaDoc
40   {
41     int userId = -1;
42     String JavaDoc html = "";
43
44     // Authenticate the user who want to comment
45
if ((username != null && !username.equals("")) || (password != null && !password.equals("")))
46       {
47         SB_AuthHome authHome = null;
48         SB_Auth auth = null;
49         try
50         {
51           authHome = (SB_AuthHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_Auth"), SB_AuthHome.class);
52           auth = authHome.create();
53         }
54         catch (Exception JavaDoc e)
55         {
56           throw new RemoteException JavaDoc("Cannot lookup SB_Auth: " +e);
57         }
58         try
59         {
60           userId = auth.authenticate(username, password);
61         }
62         catch (Exception JavaDoc e)
63         {
64           throw new RemoteException JavaDoc("Authentication failed: " +e);
65         }
66         if (userId == -1)
67         {
68            html = (" You don't have an account on RUBiS!<br>You have to register first.<br>");
69            return html;
70         }
71       }
72
73       SB_ViewItemHome itemHome;
74       SB_ViewItem viewItem;
75       try
76       {
77         itemHome = (SB_ViewItemHome)PortableRemoteObject.narrow(initialContext.lookup("java:comp/env/ejb/SB_ViewItem"),
78                                                          SB_ViewItemHome.class);
79         viewItem = itemHome.create();
80       }
81       catch (Exception JavaDoc e)
82       {
83         throw new RemoteException JavaDoc("Cannot lookup SB_ViewItem: " +e+"<br>");
84       }
85       try
86       {
87         html = viewItem.getItemDescription(itemId, userId);
88       }
89       catch (Exception JavaDoc e)
90       {
91         throw new RemoteException JavaDoc("Exception getting the item information: "+ e +"<br>");
92       }
93  
94     return html;
95   }
96                    
97
98
99   // ======================== EJB related methods ============================
100

101   /**
102    * This method is empty for a stateless session bean
103    */

104   public void ejbCreate() throws CreateException JavaDoc, RemoteException JavaDoc
105   {
106   }
107
108   /** This method is empty for a stateless session bean */
109   public void ejbActivate() throws RemoteException JavaDoc {}
110   /** This method is empty for a stateless session bean */
111   public void ejbPassivate() throws RemoteException JavaDoc {}
112   /** This method is empty for a stateless session bean */
113   public void ejbRemove() throws RemoteException JavaDoc {}
114
115
116   /**
117    * Sets the associated session context. The container calls this method
118    * after the instance creation. This method is called with no transaction context.
119    * We also retrieve the Home interfaces of all RUBiS's beans.
120    *
121    * @param sessionContext - A SessionContext interface for the instance.
122    * @exception RemoteException - Thrown if the instance could not perform the function
123    * requested by the container because of a system-level error.
124    */

125   public void setSessionContext(SessionContext JavaDoc sessionContext) throws RemoteException JavaDoc
126   {
127     this.sessionContext = sessionContext;
128     if (dataSource == null)
129     {
130       // Finds DataSource from JNDI
131

132       try
133       {
134         initialContext = new InitialContext JavaDoc();
135         dataSource = (DataSource JavaDoc)initialContext.lookup("java:comp/env/jdbc/rubis");
136       }
137       catch (Exception JavaDoc e)
138       {
139         throw new RemoteException JavaDoc("Cannot get JNDI InitialContext");
140       }
141     }
142   }
143
144 }
145
Popular Tags