Source Code Cross Referenced for BasicBmp2DataSourcesBean.java in  » J2EE » openejb3 » org » apache » openejb » test » entity » bmp » 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 » J2EE » openejb3 » org.apache.openejb.test.entity.bmp 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */package org.apache.openejb.test.entity.bmp;
017:
018:        import org.apache.openejb.test.object.OperationsPolicy;
019:
020:        import javax.ejb.EJBException;
021:        import javax.ejb.EntityContext;
022:        import javax.ejb.FinderException;
023:        import javax.ejb.RemoveException;
024:        import javax.naming.InitialContext;
025:        import javax.sql.DataSource;
026:        import java.rmi.RemoteException;
027:        import java.sql.Connection;
028:        import java.sql.PreparedStatement;
029:        import java.sql.ResultSet;
030:        import java.util.Hashtable;
031:        import java.util.Properties;
032:        import java.util.StringTokenizer;
033:
034:        /**
035:         * @author <a href="mailto:david.blevins@visi.com">David Blevins</a>
036:         * @author <a href="mailto:Richard@Monson-Haefel.com">Richard Monson-Haefel</a>
037:         */
038:        public class BasicBmp2DataSourcesBean implements  javax.ejb.EntityBean {
039:
040:            public static int primaryKey = 1;
041:            public String firstName;
042:            public String lastName;
043:            public EntityContext ejbContext;
044:            public Hashtable allowedOperationsTable = new Hashtable();
045:
046:            //=============================
047:            // Home interface methods
048:            //    
049:
050:            /**
051:             * Maps to BasicBmp2DataSourcesHome.sum
052:             * <p/>
053:             * Adds x and y and returns the result.
054:             *
055:             * @param one
056:             * @param two
057:             * @return x + y
058:             * @see BasicBmp2DataSourcesHome#sum
059:             */
060:            public int ejbHomeSum(int x, int y) {
061:                testAllowedOperations("ejbHome");
062:                return x + y;
063:            }
064:
065:            /**
066:             * Maps to BasicBmp2DataSourcesHome.findEmptyCollection
067:             *
068:             * @return Collection
069:             * @throws javax.ejb.FinderException
070:             */
071:            public java.util.Collection ejbFindEmptyCollection()
072:                    throws javax.ejb.FinderException, java.rmi.RemoteException {
073:                return new java.util.Vector();
074:            }
075:
076:            /**
077:             * Maps to BasicBmp2DataSourcesHome.findByPrimaryKey
078:             *
079:             * @param primaryKey
080:             * @return Integer
081:             * @throws javax.ejb.FinderException
082:             */
083:            public Integer ejbFindByPrimaryKey(Integer primaryKey)
084:                    throws javax.ejb.FinderException {
085:                boolean found = false;
086:                try {
087:                    InitialContext jndiContext = new InitialContext();
088:                    DataSource ds = (DataSource) jndiContext
089:                            .lookup("java:comp/env/jdbc/basic/entityDatabase");
090:                    Connection con = ds.getConnection();
091:
092:                    try {
093:                        PreparedStatement stmt = con
094:                                .prepareStatement("select * from entity where id = ?");
095:                        try {
096:                            stmt.setInt(1, primaryKey.intValue());
097:                            found = stmt.executeQuery().next();
098:                        } finally {
099:                            stmt.close();
100:                        }
101:                    } finally {
102:                        con.close();
103:                    }
104:                } catch (Exception e) {
105:                    throw new FinderException("FindByPrimaryKey failed");
106:                }
107:
108:                if (found)
109:                    return primaryKey;
110:                else
111:                    throw new javax.ejb.ObjectNotFoundException();
112:            }
113:
114:            /**
115:             * Maps to BasicBmp2DataSourcesHome.create
116:             *
117:             * @param name
118:             * @return Integer
119:             * @throws javax.ejb.CreateException
120:             */
121:            public Integer ejbCreate(String name)
122:                    throws javax.ejb.CreateException {
123:                try {
124:                    StringTokenizer st = new StringTokenizer(name, " ");
125:                    firstName = st.nextToken();
126:                    lastName = st.nextToken();
127:
128:                    InitialContext jndiContext = new InitialContext();
129:
130:                    DataSource ds = (DataSource) jndiContext
131:                            .lookup("java:comp/env/jdbc/basic/entityDatabase");
132:
133:                    Connection con = ds.getConnection();
134:
135:                    PreparedStatement stmt;
136:                    try {
137:                        // Support for Oracle because Oracle doesn't do auto increment
138:                        stmt = con
139:                                .prepareStatement("insert into entity (id, first_name, last_name) values (?,?,?)");
140:                        try {
141:                            stmt.setInt(1, primaryKey++);
142:                            stmt.setString(2, firstName);
143:                            stmt.setString(3, lastName);
144:                            stmt.executeUpdate();
145:                        } finally {
146:                            stmt.close();
147:                        }
148:
149:                        stmt = con
150:                                .prepareStatement("select id from entity where first_name = ? AND last_name = ?");
151:                        try {
152:                            stmt.setString(1, firstName);
153:                            stmt.setString(2, lastName);
154:                            ResultSet set = stmt.executeQuery();
155:                            while (set.next())
156:                                primaryKey = set.getInt("id");
157:                        } finally {
158:                            stmt.close();
159:                        }
160:                    } finally {
161:                        con.close();
162:                    }
163:
164:                    // Do backup
165:                    ds = (DataSource) jndiContext
166:                            .lookup("java:comp/env/jdbc/basic/entityDatabaseBackup");
167:
168:                    con = ds.getConnection();
169:
170:                    try {
171:                        // Support for Oracle because Oracle doesn't do auto increment
172:                        stmt = con
173:                                .prepareStatement("insert into entityBackup (id, first_name, last_name) values (?,?,?)");
174:                        try {
175:                            stmt.setInt(1, primaryKey);
176:                            stmt.setString(2, firstName);
177:                            stmt.setString(3, lastName);
178:                            stmt.executeUpdate();
179:                        } finally {
180:                            stmt.close();
181:                        }
182:                    } finally {
183:                        con.close();
184:                    }
185:
186:                    return new Integer(primaryKey);
187:
188:                } catch (Exception e) {
189:                    e.printStackTrace();
190:                    throw new javax.ejb.CreateException("can't create");
191:                }
192:            }
193:
194:            public void ejbPostCreate(String name)
195:                    throws javax.ejb.CreateException {
196:            }
197:
198:            //    
199:            // Home interface methods
200:            //=============================
201:
202:            //=============================
203:            // Remote interface methods
204:            //    
205:
206:            /**
207:             * Maps to BasicBmp2DataSourcesObject.businessMethod
208:             *
209:             * @return String
210:             */
211:            public String businessMethod(String text) {
212:                testAllowedOperations("businessMethod");
213:                StringBuffer b = new StringBuffer(text);
214:                return b.reverse().toString();
215:            }
216:
217:            /**
218:             * Maps to BasicBmp2DataSourcesObject.getPermissionsReport
219:             * <p/>
220:             * Returns a report of the bean's
221:             * runtime permissions
222:             *
223:             * @return null
224:             */
225:            public Properties getPermissionsReport() {
226:                /* TO DO: */
227:                return null;
228:            }
229:
230:            /**
231:             * Maps to BasicBmp2DataSourcesObject.getAllowedOperationsReport
232:             * <p/>
233:             * Returns a report of the allowed opperations
234:             * for one of the bean's methods.
235:             *
236:             * @param methodName The method for which to get the allowed opperations report
237:             * @return OperationPolicy
238:             */
239:            public OperationsPolicy getAllowedOperationsReport(String methodName) {
240:                return (OperationsPolicy) allowedOperationsTable
241:                        .get(methodName);
242:            }
243:
244:            //    
245:            // Remote interface methods
246:            //=============================
247:
248:            //================================
249:            // EntityBean interface methods
250:            //    
251:
252:            /**
253:             * A container invokes this method to instruct the
254:             * instance to synchronize its state by loading it state from the
255:             * underlying database.
256:             */
257:            public void ejbLoad() throws EJBException, RemoteException {
258:                try {
259:                    InitialContext jndiContext = new InitialContext();
260:                    DataSource ds = (DataSource) jndiContext
261:                            .lookup("java:comp/env/jdbc/basic/entityDatabase");
262:                    Connection con = ds.getConnection();
263:
264:                    try {
265:                        PreparedStatement stmt = con
266:                                .prepareStatement("select * from entity where id = ?");
267:                        try {
268:                            Integer primaryKey = (Integer) ejbContext
269:                                    .getPrimaryKey();
270:                            stmt.setInt(1, primaryKey.intValue());
271:                            ResultSet rs = stmt.executeQuery();
272:                            while (rs.next()) {
273:                                lastName = rs.getString("last_name");
274:                                firstName = rs.getString("first_name");
275:                            }
276:                        } finally {
277:                            stmt.close();
278:                        }
279:                    } finally {
280:                        con.close();
281:                    }
282:
283:                } catch (Exception e) {
284:                    e.printStackTrace();
285:                }
286:            }
287:
288:            /**
289:             * Set the associated entity context. The container invokes this method
290:             * on an instance after the instance has been created.
291:             */
292:            public void setEntityContext(EntityContext ctx)
293:                    throws EJBException, RemoteException {
294:                ejbContext = ctx;
295:                testAllowedOperations("setEntityContext");
296:            }
297:
298:            /**
299:             * Unset the associated entity context. The container calls this method
300:             * before removing the instance.
301:             */
302:            public void unsetEntityContext() throws EJBException,
303:                    RemoteException {
304:                testAllowedOperations("unsetEntityContext");
305:            }
306:
307:            /**
308:             * A container invokes this method to instruct the
309:             * instance to synchronize its state by storing it to the underlying
310:             * database.
311:             */
312:            public void ejbStore() throws EJBException, RemoteException {
313:                try {
314:                    InitialContext jndiContext = new InitialContext();
315:                    DataSource ds = (DataSource) jndiContext
316:                            .lookup("java:comp/env/jdbc/basic/entityDatabase");
317:                    Connection con = ds.getConnection();
318:
319:                    try {
320:                        PreparedStatement stmt = con
321:                                .prepareStatement("update entity set first_name = ?, last_name = ? where EmployeeID = ?");
322:                        try {
323:                            stmt.setString(1, firstName);
324:                            stmt.setString(2, lastName);
325:                            stmt.setInt(3, primaryKey);
326:                            stmt.execute();
327:                        } finally {
328:                            stmt.close();
329:                        }
330:                    } finally {
331:                        con.close();
332:                    }
333:                } catch (Exception e) {
334:                    e.printStackTrace();
335:                }
336:            }
337:
338:            /**
339:             * A container invokes this method before it removes the EJB object
340:             * that is currently associated with the instance. This method
341:             * is invoked when a client invokes a remove operation on the
342:             * enterprise Bean's home interface or the EJB object's remote interface.
343:             * This method transitions the instance from the ready state to the pool
344:             * of available instances.
345:             */
346:            public void ejbRemove() throws RemoveException, EJBException,
347:                    RemoteException {
348:                try {
349:                    InitialContext jndiContext = new InitialContext();
350:                    DataSource ds = (DataSource) jndiContext
351:                            .lookup("java:comp/env/jdbc/basic/entityDatabase");
352:                    Connection con = ds.getConnection();
353:                    try {
354:                        PreparedStatement stmt = con
355:                                .prepareStatement("delete from entity where id = ?");
356:                        try {
357:                            Integer primaryKey = (Integer) ejbContext
358:                                    .getPrimaryKey();
359:                            stmt.setInt(1, primaryKey.intValue());
360:                            stmt.executeUpdate();
361:                        } finally {
362:                            stmt.close();
363:                        }
364:                    } finally {
365:                        con.close();
366:                    }
367:
368:                } catch (Exception e) {
369:                    e.printStackTrace();
370:                    throw new javax.ejb.EJBException(e);
371:                }
372:            }
373:
374:            /**
375:             * A container invokes this method when the instance
376:             * is taken out of the pool of available instances to become associated
377:             * with a specific EJB object. This method transitions the instance to
378:             * the ready state.
379:             */
380:            public void ejbActivate() throws EJBException, RemoteException {
381:                testAllowedOperations("ejbActivate");
382:            }
383:
384:            /**
385:             * A container invokes this method on an instance before the instance
386:             * becomes disassociated with a specific EJB object. After this method
387:             * completes, the container will place the instance into the pool of
388:             * available instances.
389:             */
390:            public void ejbPassivate() throws EJBException, RemoteException {
391:                testAllowedOperations("ejbPassivate");
392:            }
393:
394:            //    
395:            // EntityBean interface methods
396:            //================================
397:
398:            protected void testAllowedOperations(String methodName) {
399:                OperationsPolicy policy = new OperationsPolicy();
400:
401:                /*[1] Test getEJBHome /////////////////*/
402:                try {
403:                    ejbContext.getEJBHome();
404:                    policy.allow(policy.Context_getEJBHome);
405:                } catch (IllegalStateException ise) {
406:                }
407:
408:                /*[2] Test getCallerPrincipal /////////*/
409:                try {
410:                    ejbContext.getCallerPrincipal();
411:                    policy.allow(policy.Context_getCallerPrincipal);
412:                } catch (IllegalStateException ise) {
413:                }
414:
415:                /*[3] Test isCallerInRole /////////////*/
416:                try {
417:                    ejbContext.isCallerInRole("TheMan");
418:                    policy.allow(policy.Context_isCallerInRole);
419:                } catch (IllegalStateException ise) {
420:                }
421:
422:                /*[4] Test getRollbackOnly ////////////*/
423:                try {
424:                    ejbContext.getRollbackOnly();
425:                    policy.allow(policy.Context_getRollbackOnly);
426:                } catch (IllegalStateException ise) {
427:                }
428:
429:                /*[5] Test setRollbackOnly ////////////*/
430:                try {
431:                    ejbContext.setRollbackOnly();
432:                    policy.allow(policy.Context_setRollbackOnly);
433:                } catch (IllegalStateException ise) {
434:                }
435:
436:                /*[6] Test getUserTransaction /////////*/
437:                try {
438:                    ejbContext.getUserTransaction();
439:                    policy.allow(policy.Context_getUserTransaction);
440:                } catch (Exception e) {
441:                }
442:
443:                /*[7] Test getEJBObject ///////////////*/
444:                try {
445:                    ejbContext.getEJBObject();
446:                    policy.allow(policy.Context_getEJBObject);
447:                } catch (IllegalStateException ise) {
448:                }
449:
450:                /*[8] Test getPrimaryKey //////////////*/
451:                try {
452:                    ejbContext.getPrimaryKey();
453:                    policy.allow(policy.Context_getPrimaryKey);
454:                } catch (IllegalStateException ise) {
455:                }
456:
457:                /* TO DO:  
458:                 * Check for policy.Enterprise_bean_access
459:                 * Check for policy.JNDI_access_to_java_comp_env
460:                 * Check for policy.Resource_manager_access
461:                 */
462:                allowedOperationsTable.put(methodName, policy);
463:            }
464:
465:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.