Source Code Cross Referenced for UserEntityBean.java in  » Testing » mockrunner-0.4 » com » mockrunner » example » ejb » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Testing » mockrunner 0.4 » com.mockrunner.example.ejb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.