001: package com.mockrunner.example.ejb;
002:
003: import java.rmi.RemoteException;
004: import java.sql.Connection;
005: import java.sql.PreparedStatement;
006: import java.sql.ResultSet;
007: import java.sql.SQLException;
008: import java.sql.Statement;
009: import java.util.ArrayList;
010: import java.util.Collection;
011: import java.util.List;
012:
013: import javax.ejb.CreateException;
014: import javax.ejb.EJBException;
015: import javax.ejb.EntityBean;
016: import javax.ejb.EntityContext;
017: import javax.ejb.FinderException;
018: import javax.ejb.NoSuchEntityException;
019: import javax.ejb.ObjectNotFoundException;
020: import javax.ejb.RemoveException;
021: import javax.naming.InitialContext;
022: import javax.sql.DataSource;
023:
024: /*
025: * @ejb:bean name="UserEntity"
026: * display-name="UserEntity"
027: * type="BMP"
028: * jndi-name="com/mockrunner/example/UserEntity"
029: *
030: * @ejb:pk class="java.lang.String"
031: *
032: * @ejb:transaction type="Required"
033: *
034: * @ejb:resource-ref res-ref-name="jdbc/MySQLDB"
035: * res-type="javax.sql.DataSource"
036: * res-auth="Container"
037: * res-sharing-scope="Shareable"
038: *
039: * @jboss:resource-manager res-man-name="jdbc/MySQLDB" res-man-jndi-name="java:/MySQLDB"
040: **/
041: /**
042: * Implementation of a BMP entity bean representing
043: * a user with a username and a password.
044: */
045: public class UserEntityBean implements EntityBean {
046: private EntityContext entityContext;
047: private DataSource dataSource;
048:
049: private String username;
050: private String password;
051:
052: /*
053: * @ejb:interface-method
054: **/
055: public String getPassword() {
056: return password;
057: }
058:
059: /*
060: * @ejb:interface-method
061: **/
062: public void setPassword(String password) {
063: this .password = password;
064: }
065:
066: /*
067: * @ejb:interface-method
068: **/
069: public String getUsername() {
070: return username;
071: }
072:
073: public void setUsername(String username) {
074: this .username = username;
075: }
076:
077: /*
078: * @ejb:create-method
079: **/
080: public String ejbCreate(String username, String password)
081: throws CreateException {
082: Connection connection = null;
083: PreparedStatement statement = null;
084: try {
085: connection = dataSource.getConnection();
086: statement = connection
087: .prepareStatement("insert into usertable values(?, ?)");
088: statement.setString(1, username);
089: statement.setString(2, password);
090: statement.executeUpdate();
091: this .username = username;
092: this .password = password;
093: return username;
094: } catch (SQLException exc) {
095: throw new CreateException(exc.getMessage());
096: } finally {
097: try {
098: if (null != statement)
099: statement.close();
100: if (null != connection)
101: connection.close();
102: } catch (SQLException exc) {
103:
104: }
105: }
106: }
107:
108: public void ejbPostCreate(String username, String password)
109: throws CreateException {
110:
111: }
112:
113: public String ejbFindByPrimaryKey(String username)
114: throws FinderException {
115: Connection connection = null;
116: PreparedStatement statement = null;
117: ResultSet result = null;
118: try {
119: connection = dataSource.getConnection();
120: statement = connection
121: .prepareStatement("select username from usertable where username=?");
122: statement.setString(1, username);
123: result = statement.executeQuery();
124: if (!result.next()) {
125: throw new ObjectNotFoundException(
126: "No user with username " + username + " found");
127: }
128: return result.getString(1);
129: } catch (SQLException exc) {
130: throw new EJBException(exc);
131: } finally {
132: try {
133: if (null != result)
134: result.close();
135: if (null != statement)
136: statement.close();
137: if (null != connection)
138: connection.close();
139: } catch (SQLException exc) {
140:
141: }
142: }
143: }
144:
145: public Collection ejbFindAll() throws FinderException {
146: Connection connection = null;
147: Statement statement = null;
148: ResultSet result = null;
149: try {
150: connection = dataSource.getConnection();
151: List foundKeys = new ArrayList();
152: statement = connection.createStatement();
153: result = statement
154: .executeQuery("select username from usertable");
155: while (result.next()) {
156: foundKeys.add(result.getString(1));
157: }
158: return foundKeys;
159: } catch (SQLException exc) {
160: throw new EJBException(exc);
161: } finally {
162: try {
163: if (null != result)
164: result.close();
165: if (null != statement)
166: statement.close();
167: if (null != connection)
168: connection.close();
169: } catch (SQLException exc) {
170:
171: }
172: }
173: }
174:
175: public void ejbLoad() throws EJBException, RemoteException {
176: Connection connection = null;
177: PreparedStatement statement = null;
178: ResultSet result = null;
179: try {
180: connection = dataSource.getConnection();
181: statement = connection
182: .prepareStatement("select * from usertable where username=?");
183: statement.setString(1, (String) entityContext
184: .getPrimaryKey());
185: result = statement.executeQuery();
186: if (result.next()) {
187: this .username = result.getString(1);
188: this .password = result.getString(2);
189: } else {
190: throw new NoSuchEntityException("Entity for key "
191: + entityContext.getPrimaryKey() + " not found");
192: }
193: } catch (SQLException exc) {
194: throw new EJBException(exc);
195: } finally {
196: try {
197: if (null != result)
198: result.close();
199: if (null != statement)
200: statement.close();
201: if (null != connection)
202: connection.close();
203: } catch (SQLException exc) {
204:
205: }
206: }
207: }
208:
209: public void ejbRemove() throws RemoveException, EJBException,
210: RemoteException {
211: Connection connection = null;
212: PreparedStatement statement = null;
213: try {
214: connection = dataSource.getConnection();
215: statement = connection
216: .prepareStatement("delete from usertable where username=?");
217: statement.setString(1, (String) entityContext
218: .getPrimaryKey());
219: int updateCount = statement.executeUpdate();
220: if (updateCount < 1) {
221: throw new RemoveException("Delete error for key "
222: + entityContext.getPrimaryKey());
223: }
224: } catch (SQLException exc) {
225: throw new EJBException(exc);
226: } finally {
227: try {
228: if (null != statement)
229: statement.close();
230: if (null != connection)
231: connection.close();
232: } catch (SQLException exc) {
233:
234: }
235: }
236: }
237:
238: public void ejbStore() throws EJBException, RemoteException {
239: Connection connection = null;
240: PreparedStatement statement = null;
241: try {
242: connection = dataSource.getConnection();
243: statement = connection
244: .prepareStatement("update usertable set password=? where username=?");
245: statement.setString(1, this .password);
246: statement.setString(2, this .username);
247: int updateCount = statement.executeUpdate();
248: if (updateCount < 1) {
249: throw new NoSuchEntityException("Entity for key "
250: + username + " not found");
251: }
252: } catch (SQLException exc) {
253: throw new EJBException(exc);
254: } finally {
255: try {
256: if (null != statement)
257: statement.close();
258: if (null != connection)
259: connection.close();
260: } catch (SQLException exc) {
261:
262: }
263: }
264: }
265:
266: public void ejbActivate() throws EJBException, RemoteException {
267:
268: }
269:
270: public void ejbPassivate() throws EJBException, RemoteException {
271:
272: }
273:
274: public void setEntityContext(EntityContext entityContext)
275: throws EJBException, RemoteException {
276: this .entityContext = entityContext;
277: try {
278: InitialContext context = new InitialContext();
279: dataSource = (DataSource) context
280: .lookup("java:comp/env/jdbc/MySQLDB");
281: } catch (Exception exc) {
282: throw new EJBException(exc);
283: }
284: }
285:
286: public void unsetEntityContext() throws EJBException,
287: RemoteException {
288: entityContext = null;
289: dataSource = null;
290: }
291: }
|