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.entity;
030:
031: import com.caucho.amber.entity.EntityItem;
032: import com.caucho.ejb.AbstractContext;
033: import com.caucho.ejb.AbstractServer;
034: import com.caucho.ejb.xa.EjbTransactionManager;
035:
036: import javax.ejb.*;
037: import javax.transaction.UserTransaction;
038: import java.util.logging.Level;
039: import java.util.logging.Logger;
040:
041: /**
042: * Abstract base class for an EntityHome.
043: */
044: public abstract class QEntityContext extends AbstractContext implements
045: EntityContext {
046: private static final Logger log = Logger
047: .getLogger(QEntityContext.class.getName());
048:
049: protected final EntityServer _server;
050: public Object _primaryKey;
051:
052: private Object _remoteView;
053: private EJBObject _remote;
054:
055: protected QEntityContext(EntityServer server) {
056: _server = server;
057: }
058:
059: /**
060: * Returns the owning server.
061: */
062: public AbstractServer getServer() {
063: return _server;
064: }
065:
066: /**
067: * Returns the owning server.
068: */
069: public EntityServer getEntityServer() {
070: return _server;
071: }
072:
073: /**
074: * Returns the transaction manager.
075: */
076: public EjbTransactionManager getTransactionManager() {
077: return getEntityServer().getTransactionManager();
078: }
079:
080: /**
081: * Returns the home handle for the home bean.
082: */
083: public HomeHandle getHomeHandle() {
084: return getServer().getHomeHandle();
085: }
086:
087: /**
088: * Returns the home interface
089: */
090: public EJBHome getEJBHome() {
091: return getServer().getEJBHome();
092: }
093:
094: public void setPrimaryKey(Object key) {
095: _primaryKey = key;
096: }
097:
098: /**
099: * Returns null, since primary key isn't available to the home.
100: */
101: public Object getPrimaryKey() throws IllegalStateException {
102: if (_primaryKey == null) {
103: throw new IllegalStateException(
104: "Can't get primary key. The context may belong to a home instance or it may be called before ejbActivate or ejbCreate.");
105: } else
106: return _primaryKey;
107: }
108:
109: /**
110: * Returns null, since primary key isn't available to the home.
111: */
112: public Object _caucho_getPrimaryKey() {
113: return _primaryKey;
114: }
115:
116: /**
117: * Returns the current UserTransaction. Only Session beans with
118: * bean-managed transactions may use this.
119: */
120: public UserTransaction getUserTransaction()
121: throws IllegalStateException {
122: throw new IllegalStateException(
123: "Entity beans may not call getUserTransaction()");
124: }
125:
126: /**
127: * Returns the object's handle.
128: */
129: public Handle getHandle() {
130: return getEntityServer().createHandle(_primaryKey);
131: }
132:
133: /**
134: * Returns null, since the ejb object isn't available to the home.
135: */
136: public EJBObject getEJBObject() throws IllegalStateException {
137: /*
138: if (_remote == null)
139: _remote = getEntityServer().createEJBObject(getPrimaryKey());
140:
141: return _remote;
142: */
143: return getRemoteView();
144: }
145:
146: /**
147: * Finish any caching for the entity.
148: */
149: public void postCreate(Object primaryKey) {
150: _primaryKey = primaryKey;
151:
152: getEntityServer().postCreate(primaryKey, this );
153: }
154:
155: /**
156: * Remove the object specified by the handle.
157: */
158: public void remove(Handle handle) {
159: getServer().remove(handle);
160: }
161:
162: /**
163: * Remove the object specified by the primary key.
164: */
165: public void remove(Object primaryKey) {
166: getServer().remove(primaryKey);
167: }
168:
169: public void _caucho_remove_callback(Class listenClass,
170: Object primaryKey) throws RemoveException {
171: }
172:
173: /**
174: * Invalidate the cache the entity server.
175: */
176: public void invalidateHomeCache() {
177: getServer().invalidateCache();
178: }
179:
180: /**
181: * Update the context.
182: */
183: public void update() {
184: }
185:
186: /**
187: * Sets the Amber entity item.
188: */
189: protected void __caucho_setAmber(EntityItem item) {
190: }
191:
192: public abstract void _caucho_load() throws FinderException;
193:
194: /**
195: * Callback when removed from the cache.
196: */
197: public void removeEvent() {
198: try {
199: destroy();
200: } catch (Exception e) {
201: log.log(Level.WARNING, e.toString(), e);
202: }
203: }
204:
205: /**
206: * Destroy the context.
207: */
208: public void destroy() throws Exception {
209: super.destroy();
210:
211: _remote = null;
212: _remoteView = null;
213: }
214: }
|