001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027:
028: package enroller;
029:
030: import java.sql.*;
031: import javax.sql.*;
032: import java.util.*;
033: import javax.ejb.*;
034: import javax.naming.*;
035: import javax.rmi.PortableRemoteObject;
036:
037: public class StudentBean implements EntityBean, StudentRemoteBusiness {
038: private static final String dbName = "jdbc/pointbase";
039: private String studentId;
040: private String name;
041: private ArrayList courseIds;
042: private Connection con;
043: private EntityContext context;
044: private EnrollerRemoteHome enrollerHome;
045:
046: public ArrayList getCourseIds() {
047: System.out.println("in getCourseIds");
048:
049: return courseIds;
050: }
051:
052: public String getName() {
053: return name;
054: }
055:
056: public void setName(String name) {
057: this .name = name;
058: }
059:
060: public String ejbCreate(String studentId, String name)
061: throws CreateException {
062: System.out.println("in ejbCreate");
063:
064: try {
065: insertStudent(studentId, name);
066: } catch (Exception ex) {
067: throw new EJBException("ejbCreate: " + ex.getMessage());
068: }
069:
070: this .studentId = studentId;
071: this .name = name;
072: System.out.println("about to leave ejbCreate");
073:
074: return studentId;
075: }
076:
077: public String ejbFindByPrimaryKey(String primaryKey)
078: throws FinderException {
079: boolean result;
080:
081: try {
082: result = selectByPrimaryKey(primaryKey);
083: } catch (Exception ex) {
084: throw new EJBException("ejbFindByPrimaryKey: "
085: + ex.getMessage());
086: }
087:
088: if (result) {
089: return primaryKey;
090: } else {
091: throw new ObjectNotFoundException("Row for id "
092: + primaryKey + " not found.");
093: }
094: }
095:
096: public void ejbRemove() {
097: try {
098: deleteStudent(studentId);
099: } catch (Exception ex) {
100: throw new EJBException("ejbRemove: " + ex.getMessage());
101: }
102: }
103:
104: public void setEntityContext(EntityContext context) {
105: System.out.println("in setEntityContext");
106: this .context = context;
107: courseIds = new ArrayList();
108:
109: try {
110:
111: enrollerHome = lookupEnrollerBean();
112: } catch (Exception ex) {
113: throw new EJBException("setEntityContext: "
114: + ex.getMessage());
115: }
116: }
117:
118: public void unsetEntityContext() {
119: }
120:
121: public void ejbActivate() {
122: studentId = (String) context.getPrimaryKey();
123: }
124:
125: public void ejbPassivate() {
126: studentId = null;
127: }
128:
129: public void ejbLoad() {
130: System.out.println("in ejbLoad");
131:
132: try {
133: loadStudent();
134: loadCourseIds();
135: } catch (Exception ex) {
136: throw new EJBException("ejbLoad: " + ex.getMessage());
137: }
138:
139: System.out.println("leaving ejbLoad");
140: }
141:
142: private void loadCourseIds() {
143: System.out.println("in loadCourseIds");
144: courseIds.clear();
145: System.out.println("in loadCourseIds, about to try");
146:
147: try {
148: EnrollerRemote enroller = enrollerHome.create();
149: ArrayList a = enroller.getCourseIds(studentId);
150:
151: courseIds.addAll(a);
152: } catch (Exception ex) {
153: throw new EJBException("Exception in loadCourseIds: "
154: + ex.getMessage());
155: }
156:
157: System.out.println("leaving loadCourseIds");
158: }
159:
160: public void ejbStore() {
161: System.out.println("in ejbStore");
162:
163: try {
164: storeStudent();
165: } catch (Exception ex) {
166: throw new EJBException("ejbStore: " + ex.getMessage());
167: }
168:
169: System.out.println("leaving ejbStore");
170: }
171:
172: public void ejbPostCreate(String studentId, String name) {
173: }
174:
175: /*********************** Database Routines *************************/
176: private void makeConnection() {
177: try {
178: InitialContext ic = new InitialContext();
179: DataSource ds = (DataSource) ic.lookup(dbName);
180:
181: con = ds.getConnection();
182: } catch (Exception ex) {
183: throw new EJBException("Unable to connect to database. "
184: + ex.getMessage());
185: }
186: }
187:
188: private void releaseConnection() {
189: try {
190: con.close();
191: } catch (SQLException ex) {
192: throw new EJBException("releaseConnection: "
193: + ex.getMessage());
194: }
195: }
196:
197: private void insertStudent(String studentId, String name)
198: throws SQLException {
199: makeConnection();
200:
201: String insertStatement = "insert into student values ( ? , ? )";
202: PreparedStatement prepStmt = con
203: .prepareStatement(insertStatement);
204:
205: prepStmt.setString(1, studentId);
206: prepStmt.setString(2, name);
207:
208: prepStmt.executeUpdate();
209: prepStmt.close();
210: releaseConnection();
211: }
212:
213: private boolean selectByPrimaryKey(String primaryKey)
214: throws SQLException {
215: makeConnection();
216:
217: String selectStatement = "select studentid "
218: + "from student where studentid = ? ";
219: PreparedStatement prepStmt = con
220: .prepareStatement(selectStatement);
221:
222: prepStmt.setString(1, primaryKey);
223:
224: ResultSet rs = prepStmt.executeQuery();
225: boolean result = rs.next();
226:
227: prepStmt.close();
228: releaseConnection();
229:
230: return result;
231: }
232:
233: private void deleteStudent(String studentId) throws SQLException {
234: makeConnection();
235:
236: String deleteStatement = "delete from student "
237: + "where studentid = ?";
238: PreparedStatement prepStmt = con
239: .prepareStatement(deleteStatement);
240:
241: prepStmt.setString(1, studentId);
242: prepStmt.executeUpdate();
243: prepStmt.close();
244: releaseConnection();
245: }
246:
247: private void loadStudent() throws SQLException {
248: makeConnection();
249:
250: String selectStatement = "select name "
251: + "from student where studentid = ? ";
252: PreparedStatement prepStmt = con
253: .prepareStatement(selectStatement);
254:
255: prepStmt.setString(1, studentId);
256:
257: ResultSet rs = prepStmt.executeQuery();
258:
259: if (rs.next()) {
260: name = rs.getString(1);
261: prepStmt.close();
262: } else {
263: prepStmt.close();
264: throw new NoSuchEntityException("Row for studentId "
265: + studentId + " not found in database.");
266: }
267:
268: releaseConnection();
269: }
270:
271: private void storeStudent() throws SQLException {
272: makeConnection();
273:
274: String updateStatement = "update student set name = ? "
275: + "where studentid = ?";
276: PreparedStatement prepStmt = con
277: .prepareStatement(updateStatement);
278:
279: prepStmt.setString(1, name);
280: prepStmt.setString(2, studentId);
281:
282: int rowCount = prepStmt.executeUpdate();
283:
284: prepStmt.close();
285:
286: if (rowCount == 0) {
287: throw new EJBException("Storing row for studentId "
288: + studentId + " failed.");
289: }
290:
291: releaseConnection();
292: }
293:
294: private enroller.EnrollerRemoteHome lookupEnrollerBean() {
295: try {
296: javax.naming.Context c = new javax.naming.InitialContext();
297: Object remote = c.lookup("ejb/SimpleEnroller");
298: enroller.EnrollerRemoteHome rv = (enroller.EnrollerRemoteHome) javax.rmi.PortableRemoteObject
299: .narrow(remote, enroller.EnrollerRemoteHome.class);
300: return rv;
301: } catch (javax.naming.NamingException ne) {
302: java.util.logging.Logger.getLogger(getClass().getName())
303: .log(java.util.logging.Level.SEVERE,
304: "exception caught", ne);
305: throw new RuntimeException(ne);
306: }
307:
308: }
309: }
310: // StudentBean
|