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.core.entity;
017:
018: import java.security.Principal;
019: import java.util.ArrayList;
020: import javax.ejb.EJBLocalObject;
021: import javax.ejb.EJBObject;
022: import javax.ejb.TimerService;
023: import javax.transaction.TransactionManager;
024: import javax.transaction.UserTransaction;
025:
026: import org.apache.openejb.DeploymentInfo;
027: import org.apache.openejb.InterfaceType;
028: import org.apache.openejb.InternalErrorException;
029: import org.apache.openejb.RpcContainer;
030: import org.apache.openejb.core.BaseContext;
031: import org.apache.openejb.core.Operation;
032: import org.apache.openejb.core.ThreadContext;
033: import org.apache.openejb.core.ivm.EjbObjectProxyHandler;
034: import org.apache.openejb.core.ivm.IntraVmProxy;
035: import org.apache.openejb.spi.SecurityService;
036: import org.apache.openejb.util.proxy.ProxyManager;
037:
038: /**
039: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
040: */
041: public class EntityContext extends BaseContext implements
042: javax.ejb.EntityContext {
043:
044: protected final static State[] states = new State[Operation
045: .values().length];
046:
047: public static State[] getStates() {
048: return states;
049: }
050:
051: public EntityContext(TransactionManager transactionManager,
052: SecurityService securityService) {
053: super (transactionManager, securityService);
054: }
055:
056: protected EntityContext(TransactionManager transactionManager,
057: SecurityService securityService,
058: UserTransaction userTransaction) {
059: super (transactionManager, securityService, userTransaction);
060: }
061:
062: protected State getState() {
063: Operation operation = ThreadContext.getThreadContext()
064: .getCurrentOperation();
065: State state = states[operation.ordinal()];
066:
067: if (state == null)
068: throw new IllegalArgumentException("Invalid operation "
069: + operation + " for this context");
070:
071: return state;
072: }
073:
074: public EJBLocalObject getEJBLocalObject()
075: throws IllegalStateException {
076: return ((EntityState) getState()).getEJBLocalObject();
077: }
078:
079: public EJBObject getEJBObject() throws IllegalStateException {
080: return ((EntityState) getState()).getEJBObject();
081: }
082:
083: public Object getPrimaryKey() throws IllegalStateException {
084: return ((EntityState) getState()).getPrimaryKey();
085: }
086:
087: private static class EntityState extends State {
088:
089: public EJBLocalObject getEJBLocalObject()
090: throws IllegalStateException {
091: ThreadContext threadContext = ThreadContext
092: .getThreadContext();
093: DeploymentInfo di = threadContext.getDeploymentInfo();
094:
095: if (di.getLocalInterface() == null) {
096: throw new IllegalStateException("EJB "
097: + di.getDeploymentID()
098: + " does not have a local interface");
099: }
100:
101: EjbObjectProxyHandler handler = new EntityEjbObjectHandler(
102: di, threadContext.getPrimaryKey(),
103: InterfaceType.EJB_LOCAL, new ArrayList<Class>());
104:
105: try {
106: Class[] interfaces = new Class[] {
107: di.getLocalInterface(), IntraVmProxy.class };
108: return (EJBLocalObject) ProxyManager.newProxyInstance(
109: interfaces, handler);
110: } catch (IllegalAccessException iae) {
111: throw new InternalErrorException(
112: "Could not create IVM proxy for "
113: + di.getLocalInterface() + " interface",
114: iae);
115: }
116: }
117:
118: public EJBObject getEJBObject() throws IllegalStateException {
119: ThreadContext threadContext = ThreadContext
120: .getThreadContext();
121: DeploymentInfo di = threadContext.getDeploymentInfo();
122:
123: if (di.getRemoteInterface() == null) {
124: throw new IllegalStateException("EJB "
125: + di.getDeploymentID()
126: + " does not have a remote interface");
127: }
128:
129: EjbObjectProxyHandler handler = new EntityEjbObjectHandler(
130: ((RpcContainer) di.getContainer())
131: .getDeploymentInfo(di.getDeploymentID()),
132: threadContext.getPrimaryKey(),
133: InterfaceType.EJB_OBJECT, new ArrayList<Class>());
134: try {
135: Class[] interfaces = new Class[] {
136: di.getRemoteInterface(), IntraVmProxy.class };
137: return (EJBObject) ProxyManager.newProxyInstance(
138: interfaces, handler);
139: } catch (IllegalAccessException iae) {
140: throw new InternalErrorException(
141: "Could not create IVM proxy for "
142: + di.getRemoteInterface()
143: + " interface", iae);
144: }
145: }
146:
147: public Object getPrimaryKey() throws IllegalStateException {
148: ThreadContext threadContext = ThreadContext
149: .getThreadContext();
150: return threadContext.getPrimaryKey();
151: }
152:
153: public boolean isTimerMethodAllowed() {
154: return false;
155: }
156: }
157:
158: protected static class ContextEntityState extends EntityState {
159:
160: public EJBLocalObject getEJBLocalObject()
161: throws IllegalStateException {
162: throw new IllegalStateException();
163: }
164:
165: public EJBObject getEJBObject() throws IllegalStateException {
166: throw new IllegalStateException();
167: }
168:
169: public Object getPrimaryKey() throws IllegalStateException {
170: throw new IllegalStateException();
171: }
172:
173: public Principal getCallerPrincipal(
174: SecurityService securityService) {
175: throw new IllegalStateException();
176: }
177:
178: public boolean isCallerInRole(SecurityService securityService,
179: String roleName) {
180: throw new IllegalStateException();
181: }
182:
183: public UserTransaction getUserTransaction(
184: UserTransaction userTransaction)
185: throws IllegalStateException {
186: throw new IllegalStateException();
187: }
188:
189: public void setRollbackOnly(
190: TransactionManager transactionManager)
191: throws IllegalStateException {
192: throw new IllegalStateException();
193: }
194:
195: public boolean getRollbackOnly(
196: TransactionManager transactionManager)
197: throws IllegalStateException {
198: throw new IllegalStateException();
199: }
200:
201: public TimerService getTimerService()
202: throws IllegalStateException {
203: throw new IllegalStateException();
204: }
205:
206: public boolean isUserTransactionAccessAllowed() {
207: return false;
208: }
209:
210: public boolean isMessageContextAccessAllowed() {
211: return false;
212: }
213:
214: public boolean isEntityManagerFactoryAccessAllowed() {
215: return false;
216: }
217:
218: public boolean isEntityManagerAccessAllowed() {
219: return false;
220: }
221:
222: public boolean isTimerAccessAllowed() {
223: return false;
224: }
225: }
226:
227: protected static class CreateEntityState extends EntityState {
228:
229: public EJBLocalObject getEJBLocalObject()
230: throws IllegalStateException {
231: throw new IllegalStateException();
232: }
233:
234: public EJBObject getEJBObject() throws IllegalStateException {
235: throw new IllegalStateException();
236: }
237:
238: public Object getPrimaryKey() throws IllegalStateException {
239: throw new IllegalStateException();
240: }
241:
242: public UserTransaction getUserTransaction(
243: UserTransaction userTransaction)
244: throws IllegalStateException {
245: throw new IllegalStateException();
246: }
247:
248: public boolean isUserTransactionAccessAllowed() {
249: return false;
250: }
251:
252: public boolean isMessageContextAccessAllowed() {
253: return false;
254: }
255:
256: public boolean isTimerAccessAllowed() {
257: return false;
258: }
259: }
260:
261: protected static class LifecycleBusinessTimeoutEntityState extends
262: EntityState {
263:
264: public UserTransaction getUserTransaction(
265: UserTransaction userTransaction)
266: throws IllegalStateException {
267: throw new IllegalStateException();
268: }
269:
270: public boolean isUserTransactionAccessAllowed() {
271: return false;
272: }
273:
274: public boolean isMessageContextAccessAllowed() {
275: return false;
276: }
277:
278: public boolean isTimerMethodAllowed() {
279: return true;
280: }
281: }
282:
283: protected static class FindEntityState extends EntityState {
284:
285: public EJBLocalObject getEJBLocalObject()
286: throws IllegalStateException {
287: throw new IllegalStateException();
288: }
289:
290: public EJBObject getEJBObject() throws IllegalStateException {
291: throw new IllegalStateException();
292: }
293:
294: public Object getPrimaryKey() throws IllegalStateException {
295: throw new IllegalStateException();
296: }
297:
298: public UserTransaction getUserTransaction(
299: UserTransaction userTransaction)
300: throws IllegalStateException {
301: throw new IllegalStateException();
302: }
303:
304: public TimerService getTimerService()
305: throws IllegalStateException {
306: throw new IllegalStateException();
307: }
308:
309: public boolean isUserTransactionAccessAllowed() {
310: return false;
311: }
312:
313: public boolean isMessageContextAccessAllowed() {
314: return false;
315: }
316:
317: public boolean isTimerAccessAllowed() {
318: return false;
319: }
320: }
321:
322: protected static class HomeEntityState extends EntityState {
323:
324: public EJBLocalObject getEJBLocalObject()
325: throws IllegalStateException {
326: throw new IllegalStateException();
327: }
328:
329: public EJBObject getEJBObject() throws IllegalStateException {
330: throw new IllegalStateException();
331: }
332:
333: public Object getPrimaryKey() throws IllegalStateException {
334: throw new IllegalStateException();
335: }
336:
337: public UserTransaction getUserTransaction(
338: UserTransaction userTransaction)
339: throws IllegalStateException {
340: throw new IllegalStateException();
341: }
342:
343: public boolean isUserTransactionAccessAllowed() {
344: return false;
345: }
346:
347: public boolean isMessageContextAccessAllowed() {
348: return false;
349: }
350:
351: public boolean isTimerAccessAllowed() {
352: return false;
353: }
354: }
355:
356: protected static class ActivatePassivateEntityState extends
357: EntityState {
358:
359: public Principal getCallerPrincipal(
360: SecurityService securityService) {
361: throw new IllegalStateException();
362: }
363:
364: public boolean isCallerInRole(SecurityService securityService,
365: String roleName) {
366: throw new IllegalStateException();
367: }
368:
369: public UserTransaction getUserTransaction(
370: UserTransaction userTransaction)
371: throws IllegalStateException {
372: throw new IllegalStateException();
373: }
374:
375: public void setRollbackOnly(
376: TransactionManager transactionManager)
377: throws IllegalStateException {
378: throw new IllegalStateException();
379: }
380:
381: public boolean getRollbackOnly(
382: TransactionManager transactionManager)
383: throws IllegalStateException {
384: throw new IllegalStateException();
385: }
386:
387: public boolean isUserTransactionAccessAllowed() {
388: return false;
389: }
390:
391: public boolean isMessageContextAccessAllowed() {
392: return false;
393: }
394:
395: public boolean isEntityManagerFactoryAccessAllowed() {
396: return false;
397: }
398:
399: public boolean isEntityManagerAccessAllowed() {
400: return false;
401: }
402:
403: public boolean isTimerAccessAllowed() {
404: return false;
405: }
406: }
407:
408: static {
409: states[Operation.SET_CONTEXT.ordinal()] = new ContextEntityState();
410: states[Operation.UNSET_CONTEXT.ordinal()] = new ContextEntityState();
411: states[Operation.CREATE.ordinal()] = new CreateEntityState();
412: states[Operation.POST_CREATE.ordinal()] = new LifecycleBusinessTimeoutEntityState();
413: states[Operation.REMOVE.ordinal()] = new LifecycleBusinessTimeoutEntityState();
414: states[Operation.FIND.ordinal()] = new FindEntityState();
415: states[Operation.HOME.ordinal()] = new HomeEntityState();
416: states[Operation.ACTIVATE.ordinal()] = new ActivatePassivateEntityState();
417: states[Operation.PASSIVATE.ordinal()] = new ActivatePassivateEntityState();
418: states[Operation.LOAD.ordinal()] = new LifecycleBusinessTimeoutEntityState();
419: states[Operation.STORE.ordinal()] = new LifecycleBusinessTimeoutEntityState();
420: states[Operation.BUSINESS.ordinal()] = new LifecycleBusinessTimeoutEntityState();
421: states[Operation.TIMEOUT.ordinal()] = new LifecycleBusinessTimeoutEntityState();
422: }
423: }
|