Source Code Cross Referenced for MockEjbContext.java in  » Testing » MockEJB » org » mockejb » 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 » MockEJB » org.mockejb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.mockejb;
002:
003:        import java.io.Serializable;
004:        import java.security.Identity;
005:        import java.security.Principal;
006:
007:        import java.util.Properties;
008:
009:        import javax.ejb.*;
010:        import javax.transaction.Status;
011:        import javax.transaction.SystemException;
012:        import javax.transaction.UserTransaction;
013:
014:        /**
015:         * Provides implementation of <code>javax.ejb.SessionContext</code>,
016:         * <code>javax.ejb.MessageDrivenContext</code> and <code>javax.ejb.EntityContext</code>
017:         * as well as some extra convenience methods.
018:         * 
019:         * @author Alexander Ananiev
020:         */
021:        public class MockEjbContext implements  SessionContext,
022:                MessageDrivenContext, EntityContext, Serializable {
023:
024:            private Object homeProxy;
025:            private Object ejbObjectProxy;
026:            private Object primaryKey;
027:
028:            MockEjbContext(final Object homeProxy) {
029:                this .homeProxy = homeProxy;
030:            }
031:
032:            void setEjbObjectProxy(final Object ejbObjectProxy) {
033:                this .ejbObjectProxy = ejbObjectProxy;
034:            }
035:
036:            /**
037:             * Tests if the business interface for this context's bean is remote interface 
038:             * meaning that it extends <code>EJBObject</code>.
039:             * <br>This is useful for exception handling and in other places where local/remote
040:             * interfaces behave differently. 
041:             * @return true if the business interface of this context's EJB implements remote 
042:             * interface. Always returns false for message-driven beans.
043:             */
044:            public boolean isRemote() {
045:
046:                if (ejbObjectProxy == null)
047:                    throw new IllegalStateException(
048:                            "Can't determine the type. Most likely this EJB has not bean created yet");
049:
050:                return (EJBObject.class.isAssignableFrom(ejbObjectProxy
051:                        .getClass()));
052:            }
053:
054:            /** 
055:             * @see javax.ejb.EJBContext#getEJBHome()
056:             */
057:            public EJBHome getEJBHome() {
058:                if (homeProxy == null)
059:                    throw new IllegalStateException(
060:                            "This EJB does not have Home interface");
061:
062:                return (EJBHome) homeProxy;
063:            }
064:
065:            /**
066:             *
067:             * @see javax.ejb.EJBContext#getEJBLocalHome()
068:             */
069:            public EJBLocalHome getEJBLocalHome() {
070:
071:                if (homeProxy == null)
072:                    throw new IllegalStateException(
073:                            "This EJB does not have Local Home interface");
074:
075:                return (EJBLocalHome) homeProxy;
076:            }
077:
078:            /**
079:             * Always returns empty Properties object. Bean-scoped environment 
080:             * is not supported directly.
081:             * @see javax.ejb.EJBContext#getEnvironment()
082:             */
083:            public Properties getEnvironment() {
084:
085:                return new Properties();
086:            }
087:
088:            /**
089:             * This method is not supported
090:             * @see javax.ejb.EJBContext#getCallerIdentity()
091:             */
092:            public Identity getCallerIdentity() {
093:                throwMethodNotImplemented("getCallerIdentity");
094:                return null;
095:            }
096:
097:            /**
098:             * Returns the principal that was logged in using 
099:             * MockContainer.login. Returns the anonymous principal if 
100:             * login was not called. 
101:             *  
102:             * @return principal of the logged in user or anonymous principal
103:             * 
104:             * @see javax.ejb.EJBContext#getCallerPrincipal()
105:             */
106:            public Principal getCallerPrincipal() {
107:                return MockContainer.getUser();
108:            }
109:
110:            /** 
111:             * This method is not supported
112:             * @see javax.ejb.EJBContext#isCallerInRole(java.security.Identity)
113:             */
114:            public boolean isCallerInRole(Identity arg0) {
115:                throwMethodNotImplemented("isCallerInRole");
116:                return false;
117:            }
118:
119:            /**
120:             * @see javax.ejb.EJBContext#isCallerInRole(java.lang.String)
121:             */
122:            public boolean isCallerInRole(String role) {
123:                return MockContainer.getUser().hasRole(role);
124:            }
125:
126:            /**
127:             * Calls {@link TransactionManager} to get the
128:             * <code>javax.transaction.UserTransaction</code> object.
129:             * @return <code>javax.transaction.UserTransaction</code> object  
130:             * @see javax.ejb.EJBContext#getUserTransaction()
131:             */
132:            public UserTransaction getUserTransaction()
133:                    throws IllegalStateException {
134:
135:                UserTransaction tran = TransactionManager.getUserTransaction();
136:
137:                return tran;
138:            }
139:
140:            /**
141:             * @see javax.ejb.EJBContext#setRollbackOnly()
142:             */
143:            public void setRollbackOnly() throws IllegalStateException {
144:                UserTransaction tran = getUserTransaction();
145:                try {
146:                    tran.setRollbackOnly();
147:                } catch (SystemException se) {
148:                    throw new EJBException(
149:                            "Error trying to call setRollbackOnly on transaction",
150:                            se);
151:                }
152:
153:            }
154:
155:            /**
156:             * @see javax.ejb.EJBContext#getRollbackOnly()
157:             */
158:            public boolean getRollbackOnly() throws IllegalStateException {
159:
160:                int status = Status.STATUS_UNKNOWN;
161:                UserTransaction tran = getUserTransaction();
162:                try {
163:                    status = tran.getStatus();
164:                } catch (SystemException se) {
165:                    throw new EJBException(
166:                            "Error trying to call getStatus on transaction", se);
167:                }
168:
169:                return (status == Status.STATUS_MARKED_ROLLBACK);
170:            }
171:
172:            /**
173:             * Obtains a reference to the EJB local object that is currently associated with the instance.
174:             * @return the EJB object currently associated with the instance
175:             */
176:            public EJBLocalObject getEJBLocalObject()
177:                    throws IllegalStateException {
178:                return (EJBLocalObject) ejbObjectProxy;
179:            }
180:
181:            /**
182:             * Obtains a reference to the EJB object that is currently associated with the instance.
183:             * @return the EJB object currently associated with the instance
184:             */
185:            public EJBObject getEJBObject() throws IllegalStateException {
186:                return (EJBObject) ejbObjectProxy;
187:            }
188:
189:            // EJB 2.1 methods for the future 
190:            /*
191:            public javax.xml.rpc.handler.MessageContext getMessageContext() throws IllegalStateException{
192:                throwMethodNotImplemented( "getMessageContext");
193:                return null;
194:            }
195:            
196:            public TimerService getTimerService() throws IllegalStateException {    
197:                throwMethodNotImplemented( "getMessageContext");
198:                return null;
199:            }
200:             */
201:
202:            /**
203:             * Helper method to throw NotImplementedException for this class
204:             * @param methodName
205:             */
206:            private void throwMethodNotImplemented(String methodName) {
207:
208:                throw new MethodNotImplementedException(methodName, this 
209:                        .getClass().getName());
210:
211:            }
212:
213:            /**
214:             * Returns the primary key for entity beans. If the context is assotiated with 
215:             * Session bean or MDB, returns null. 
216:             * MockEJB does not automatically handles primary keys for 
217:             * entity beans. Since "create" returns null, you need to intercept "create" methods
218:             * of your entity bean and return the real PK. 
219:             * 
220:             * @see javax.ejb.EntityContext#getPrimaryKey()
221:             */
222:            public Object getPrimaryKey() throws IllegalStateException {
223:                return primaryKey;
224:            }
225:
226:            public void setPrimaryKey(Object primaryKey) {
227:                this.primaryKey = primaryKey;
228:            }
229:
230:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.