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.mdb;
017:
018: import java.security.Principal;
019: import javax.ejb.EJBHome;
020: import javax.ejb.EJBLocalHome;
021: import javax.ejb.MessageDrivenContext;
022: import javax.ejb.TimerService;
023: import javax.transaction.TransactionManager;
024: import javax.transaction.UserTransaction;
025:
026: import org.apache.openejb.core.BaseContext;
027: import org.apache.openejb.core.Operation;
028: import org.apache.openejb.core.ThreadContext;
029: import org.apache.openejb.spi.SecurityService;
030:
031: /**
032: * @version $Rev: 602704 $ $Date: 2007-12-09 09:58:22 -0800 $
033: */
034: public class MdbContext extends BaseContext implements
035: MessageDrivenContext {
036:
037: protected final static State[] states = new State[Operation
038: .values().length];
039:
040: public static State[] getStates() {
041: return states;
042: }
043:
044: public MdbContext(TransactionManager transactionManager,
045: SecurityService securityService) {
046: super (transactionManager, securityService);
047: }
048:
049: protected MdbContext(TransactionManager transactionManager,
050: SecurityService securityService,
051: UserTransaction userTransaction) {
052: super (transactionManager, securityService, userTransaction);
053: }
054:
055: protected State getState() {
056: Operation operation = ThreadContext.getThreadContext()
057: .getCurrentOperation();
058: State state = states[operation.ordinal()];
059:
060: if (state == null)
061: throw new IllegalArgumentException("Invalid operation "
062: + operation + " for this context");
063:
064: return state;
065: }
066:
067: /**
068: * Dependency injection methods (e.g., setMessageDrivenContext)
069: */
070: protected static class InjectionMdbState extends State {
071: @Override
072: public EJBHome getEJBHome() {
073: throw new IllegalStateException();
074: }
075:
076: @Override
077: public EJBLocalHome getEJBLocalHome() {
078: throw new IllegalStateException();
079: }
080:
081: @Override
082: public Principal getCallerPrincipal(
083: SecurityService securityService) {
084: throw new IllegalStateException();
085: }
086:
087: @Override
088: public boolean isCallerInRole(SecurityService securityService,
089: String roleName) {
090: throw new IllegalStateException();
091: }
092:
093: @Override
094: public UserTransaction getUserTransaction(
095: UserTransaction userTransaction)
096: throws IllegalStateException {
097: throw new IllegalStateException();
098: }
099:
100: @Override
101: public boolean getRollbackOnly(
102: TransactionManager transactionManager)
103: throws IllegalStateException {
104: throw new IllegalStateException();
105: }
106:
107: @Override
108: public void setRollbackOnly(
109: TransactionManager transactionManager)
110: throws IllegalStateException {
111: throw new IllegalStateException();
112: }
113:
114: @Override
115: public TimerService getTimerService()
116: throws IllegalStateException {
117: throw new IllegalStateException();
118: }
119:
120: @Override
121: public boolean isUserTransactionAccessAllowed() {
122: return false;
123: }
124:
125: @Override
126: public boolean isMessageContextAccessAllowed() {
127: return false;
128: }
129:
130: @Override
131: public boolean isEntityManagerFactoryAccessAllowed() {
132: return false;
133: }
134:
135: @Override
136: public boolean isEntityManagerAccessAllowed() {
137: return false;
138: }
139:
140: @Override
141: public boolean isTimerAccessAllowed() {
142: return false;
143: }
144:
145: @Override
146: public boolean isTimerMethodAllowed() {
147: return false;
148: }
149: }
150:
151: /**
152: * PostConstruct, Pre-Destroy lifecycle callback interceptor methods
153: */
154: protected static class LifecycleMdbState extends State {
155: @Override
156: public EJBHome getEJBHome() {
157: throw new IllegalStateException();
158: }
159:
160: @Override
161: public EJBLocalHome getEJBLocalHome() {
162: throw new IllegalStateException();
163: }
164:
165: @Override
166: public Principal getCallerPrincipal(
167: SecurityService securityService) {
168: throw new IllegalStateException();
169: }
170:
171: @Override
172: public boolean isCallerInRole(SecurityService securityService,
173: String roleName) {
174: throw new IllegalStateException();
175: }
176:
177: @Override
178: public boolean getRollbackOnly(
179: TransactionManager transactionManager)
180: throws IllegalStateException {
181: throw new IllegalStateException();
182: }
183:
184: @Override
185: public void setRollbackOnly(
186: TransactionManager transactionManager)
187: throws IllegalStateException {
188: throw new IllegalStateException();
189: }
190:
191: @Override
192: public boolean isMessageContextAccessAllowed() {
193: return false;
194: }
195:
196: @Override
197: public boolean isEntityManagerAccessAllowed() {
198: return false;
199: }
200:
201: @Override
202: public boolean isTimerAccessAllowed() {
203: return super .isTimerAccessAllowed(); //todo: consider this autogenerated code
204: }
205:
206: @Override
207: public boolean isTimerMethodAllowed() {
208: return false;
209: }
210: }
211:
212: /**
213: * Message listener method, business method interceptor method
214: * and imeout callback method
215: */
216: protected static class BusinessTimeoutMdbState extends State {
217: @Override
218: public EJBHome getEJBHome() {
219: throw new IllegalStateException();
220: }
221:
222: @Override
223: public EJBLocalHome getEJBLocalHome() {
224: throw new IllegalStateException();
225: }
226:
227: @Override
228: public boolean isCallerInRole(SecurityService securityService,
229: String roleName) {
230: throw new IllegalStateException();
231: }
232:
233: }
234:
235: static {
236: states[Operation.INJECTION.ordinal()] = new InjectionMdbState();
237: states[Operation.CREATE.ordinal()] = new LifecycleMdbState();
238: states[Operation.POST_CONSTRUCT.ordinal()] = new LifecycleMdbState();
239: states[Operation.PRE_DESTROY.ordinal()] = new LifecycleMdbState();
240: states[Operation.BUSINESS.ordinal()] = new BusinessTimeoutMdbState();
241: states[Operation.TIMEOUT.ordinal()] = new BusinessTimeoutMdbState();
242: }
243:
244: }
|