001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.ejb;
030:
031: import com.caucho.ejb.xa.TransactionContext;
032: import com.caucho.security.SecurityContext;
033: import com.caucho.security.SecurityContextException;
034: import com.caucho.util.L10N;
035:
036: import javax.ejb.*;
037: import javax.transaction.UserTransaction;
038: import java.rmi.RemoteException;
039: import java.security.Identity;
040: import java.security.Principal;
041: import java.util.Properties;
042: import java.util.logging.Level;
043: import java.util.logging.Logger;
044:
045: /**
046: * Base class for an abstract context
047: */
048: abstract public class AbstractContext implements EJBContext {
049: private static final L10N L = new L10N(AbstractContext.class);
050: private static final Logger log = Logger
051: .getLogger(AbstractContext.class.getName());
052:
053: private boolean _isDead;
054:
055: private Class _invokedBusinessInterface;
056:
057: /**
058: * Returns true if the context is dead.
059: */
060: public boolean isDead() {
061: return _isDead;
062: }
063:
064: /**
065: * Returns the server which owns this bean.
066: */
067: public abstract AbstractServer getServer();
068:
069: /**
070: * Returns the EJB's meta data.
071: */
072: public EJBMetaData getEJBMetaData() {
073: return getServer().getEJBMetaData();
074: }
075:
076: /**
077: * Returns the EJBHome stub for the container.
078: */
079: public EJBHome getEJBHome() {
080: throw new UnsupportedOperationException(getClass().getName());
081: }
082:
083: /**
084: * Returns the EJBLocalHome stub for the container.
085: */
086: public EJBLocalHome getEJBLocalHome() {
087: try {
088: Object localHome = getServer().getEJBLocalHome();
089:
090: // ejb/0f61
091: if (localHome == null && getServer().getEJBHome() == null)
092: throw new IllegalStateException(
093: "getEJBLocalHome() is only allowed through EJB 2.1 interfaces");
094:
095: return null;
096: } catch (RuntimeException e) {
097: throw e;
098: } catch (Exception e) {
099: return null;
100: }
101: }
102:
103: /**
104: * Returns the object's handle.
105: */
106: public Handle getHandle() {
107: throw new UnsupportedOperationException();
108: }
109:
110: /**
111: * Returns the object's home handle.
112: */
113: public HomeHandle getHomeHandle() {
114: return getServer().getHomeHandle();
115: }
116:
117: /**
118: * Returns the local object in the context.
119: */
120: public EJBLocalObject getEJBLocalObject()
121: throws IllegalStateException {
122: throw new IllegalStateException(
123: L
124: .l(
125: "`{0}' has no local interface. Local beans need a local-home and a local interface. Remote beans must be called with a remote context.",
126: getServer()));
127: }
128:
129: /**
130: * Looks up an object in the current JNDI context.
131: */
132: public Object lookup(String name) {
133: return getServer().lookup(name);
134: }
135:
136: /**
137: * Returns the EJBObject stub for the container.
138: */
139: public EJBObject getEJBObject() {
140: EJBObject obj = getRemoteView();
141:
142: if (obj == null)
143: throw new IllegalStateException(
144: "getEJBObject() is only allowed through EJB 2.1 interfaces");
145:
146: return obj;
147:
148: /*
149: throw new IllegalStateException(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
150: getServer().getEJBName()));
151: */
152: }
153:
154: /**
155: * Returns the underlying bean
156: */
157: public EJBObject getRemoteView() {
158: return null;
159:
160: /*
161: throw new IllegalStateException(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
162: getServer()));
163: */
164: }
165:
166: /**
167: * Create the home view.
168: */
169: public EJBHome createRemoteHomeView() {
170: return null;
171: /*
172: throw new IllegalStateException(L.l("`{0}' has no remote interface. Remote beans need a home and a remote interface. Local beans must be called with a local context.",
173: getServer().getEJBName()));
174: */
175: }
176:
177: /**
178: * Create the local home view.
179: */
180: public EJBLocalHome createLocalHome() {
181: return null;
182: /*
183: throw new IllegalStateException(L.l("`{0}' has no local interface. Local beans need a local-home and a local interface. Remote beans must be called with a remote context.",
184: getServer().getEJBName()));
185: */
186: }
187:
188: /**
189: * Create the 2.1 remote view.
190: */
191: public Object createRemoteView21() {
192: return null;
193: }
194:
195: /**
196: * Create the 3.0 remote view.
197: */
198: public Object createRemoteView() {
199: return null;
200: }
201:
202: /**
203: * Obsolete method which returns the EJB 1.0 environment.
204: */
205: public Properties getEnvironment() {
206: return new Properties();
207: }
208:
209: /**
210: * Obsolete method returns null.
211: */
212: public Identity getCallerIdentity() {
213: return null;
214: }
215:
216: /**
217: * Returns the principal
218: */
219: public Principal getCallerPrincipal() {
220: try {
221: return SecurityContext.getUserPrincipal();
222: } catch (SecurityContextException e) {
223: log.log(Level.WARNING, e.toString(), e);
224:
225: return null;
226: }
227: }
228:
229: /**
230: * Obsolete method returns false.
231: */
232: public boolean isCallerInRole(Identity role) {
233: return false;
234: }
235:
236: /**
237: * Returns true if the caller is in the named role.
238: */
239: public boolean isCallerInRole(String roleName) {
240: return SecurityContext.isUserInRole(roleName);
241: }
242:
243: public void remove() throws RemoveException {
244: EJBObject obj = null;
245: try {
246: obj = getEJBObject();
247: } catch (Exception e) {
248: }
249:
250: try {
251: if (obj != null) {
252: obj.remove();
253: return;
254: }
255: } catch (RemoteException e) {
256: }
257:
258: EJBLocalObject local = null;
259: try {
260: local = getEJBLocalObject();
261: } catch (Exception e) {
262: }
263:
264: if (local != null) {
265: local.remove();
266: return;
267: }
268: }
269:
270: /**
271: * Returns the current UserTransaction. Only Session beans with
272: * bean-managed transactions may use this.
273: */
274: public UserTransaction getUserTransaction()
275: throws IllegalStateException {
276: if (getServer().isContainerTransaction())
277: throw new IllegalStateException(
278: "getUserTransaction() is not allowed with container-managed transaction");
279:
280: return getServer().getUserTransaction();
281: }
282:
283: /**
284: * Looks the timer service.
285: */
286: public TimerService getTimerService() throws IllegalStateException {
287: return getServer().getTimerService();
288: }
289:
290: /**
291: * Forces a rollback of the current transaction.
292: */
293: public void setRollbackOnly() throws IllegalStateException {
294: if (!getServer().isContainerTransaction())
295: throw new IllegalStateException(
296: "setRollbackOnly() is only allowed with container-managed transaction");
297:
298: TransactionContext trans = getServer().getTransaction();
299:
300: if (trans != null)
301: trans.setRollbackOnly();
302: else
303: throw new IllegalStateException("invalid transaction");
304: }
305:
306: /**
307: * Returns true if the current transaction will rollback.
308: */
309: public boolean getRollbackOnly() throws IllegalStateException {
310: if (!getServer().isContainerTransaction())
311: throw new IllegalStateException(
312: "getRollbackOnly() is only allowed with container-managed transaction");
313:
314: TransactionContext trans = getServer().getTransaction();
315:
316: if (trans != null)
317: return trans.getRollbackOnly();
318: else
319: throw new IllegalStateException("invalid transaction");
320: }
321:
322: /**
323: * Destroy the context.
324: */
325: public void destroy() throws Exception {
326: _isDead = true;
327: }
328:
329: public Class getInvokedBusinessInterface()
330: throws IllegalStateException {
331: if (_invokedBusinessInterface == null)
332: throw new IllegalStateException(
333: "SessionContext.getInvokedBusinessInterface() is only allowed through EJB 3.0 interfaces");
334:
335: return _invokedBusinessInterface;
336: }
337:
338: public void __caucho_setInvokedBusinessInterface(
339: Class invokedBusinessInterface) {
340: _invokedBusinessInterface = invokedBusinessInterface;
341: }
342:
343: /**
344: * Runs the timeout callbacks.
345: */
346: public void __caucho_timeout_callback(Timer timer) {
347: }
348: }
|