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: }
|