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.ejb.AbstractEJBObject;
032: import com.caucho.ejb.AbstractServer;
033: import com.caucho.ejb.xa.TransactionContext;
034: import com.caucho.util.Log;
035:
036: import javax.ejb.EJBHome;
037: import javax.ejb.EJBLocalHome;
038: import javax.ejb.EJBLocalObject;
039: import javax.ejb.EJBObject;
040: import javax.ejb.Handle;
041: import java.io.Serializable;
042: import java.rmi.RemoteException;
043: import java.util.logging.Level;
044: import java.util.logging.Logger;
045:
046: /**
047: * Abstract base class for an EntityObject.
048: */
049: abstract public class EntityObject extends AbstractEJBObject implements
050: Serializable, EJBLocalObject {
051: protected static final Logger log = Log.open(EntityObject.class);
052:
053: public static final byte _CAUCHO_IS_DEAD = QEntity._CAUCHO_IS_DEAD;
054: public static final byte _CAUCHO_IS_ACTIVE = QEntity._CAUCHO_IS_ACTIVE;
055: public static final byte _CAUCHO_IS_LOADED = QEntity._CAUCHO_IS_LOADED;
056: public static final byte _CAUCHO_IS_DIRTY = QEntity._CAUCHO_IS_DIRTY;
057:
058: protected abstract QEntityContext getEntityContext();
059:
060: /**
061: * Returns the Entity bean's primary key
062: */
063: public Object getPrimaryKey() {
064: return getEntityContext().getPrimaryKey();
065: }
066:
067: public Object getEJBObject() {
068: return getEntityContext().getEJBObject();
069: }
070:
071: public Object getEJBLocalObject() {
072: return getEntityContext().getEJBLocalObject();
073: }
074:
075: /**
076: * Returns the server.
077: */
078: public EntityServer _caucho_getEntityServer() {
079: return getEntityContext().getEntityServer();
080: }
081:
082: /**
083: * Return if matching context.
084: */
085: public boolean isMatch(EntityServer server, Object key) {
086: return server == _caucho_getEntityServer()
087: && key.equals(getPrimaryKey());
088: }
089:
090: /**
091: * Returns the handle.
092: */
093: public Handle getHandle() {
094: return getEntityContext().getHandle();
095: }
096:
097: /**
098: * Returns an underlying bean
099: */
100: public Object _caucho_getBean(TransactionContext trans,
101: boolean doLoad) {
102: throw new UnsupportedOperationException();
103: }
104:
105: /**
106: * Returns an underlying bean
107: */
108: public Object _caucho_getBean() {
109: throw new UnsupportedOperationException();
110: }
111:
112: /**
113: * Returns the EJBHome stub for the container.
114: */
115: public EJBHome getEJBHome() {
116: try {
117: return _caucho_getEntityServer().getEJBHome();
118: } catch (Exception e) {
119: return null;
120: }
121: }
122:
123: /**
124: * Returns the EJBHome stub for the container.
125: */
126: public EJBLocalHome getEJBLocalHome() {
127: try {
128: return (EJBLocalHome) _caucho_getEntityServer()
129: .getEJBLocalHome();
130: } catch (Exception e) {
131: return null;
132: }
133: }
134:
135: /**
136: * Returns true if the two objects are identical.
137: */
138: public boolean isIdentical(EJBObject obj) throws RemoteException {
139: return getHandle().equals(obj.getHandle());
140: }
141:
142: /**
143: * Returns true if the two objects are identical.
144: */
145: public boolean isIdentical(EJBLocalObject o) {
146: if (o == null || !getClass().equals(o.getClass()))
147: return false;
148:
149: EntityObject obj = (EntityObject) o;
150:
151: try {
152: Object key = getPrimaryKey();
153: Object objKey = obj.getPrimaryKey();
154:
155: if (key != null)
156: return key.equals(objKey);
157: else
158: return objKey == null;
159: } catch (Exception e) {
160: return false;
161: }
162: }
163:
164: /**
165: * Remove the object.
166: */
167: public void remove() throws javax.ejb.RemoveException {
168: //_caucho_getServer().remove(getHandle());
169: }
170:
171: /**
172: * Returns the server.
173: */
174: public AbstractServer __caucho_getServer() {
175: return _caucho_getEntityServer();
176: }
177:
178: /**
179: * The home id is null.
180: */
181: public String __caucho_getId() {
182: return _caucho_getEntityServer().encodeId(getPrimaryKey());
183: }
184:
185: /**
186: * Returns a hash code for the object.
187: */
188: public int hashCode() {
189: try {
190: Object key = getPrimaryKey();
191:
192: if (key != null)
193: return key.hashCode();
194: else
195: return 0;
196: } catch (Throwable e) {
197: return 0;
198: }
199: }
200:
201: /**
202: * Returns true if this object equals the test object.
203: */
204:
205: public boolean equals(Object o) {
206: if (this == o)
207: return true;
208:
209: if (o == null || !getClass().equals(o.getClass()))
210: return false;
211:
212: EntityObject obj = (EntityObject) o;
213:
214: try {
215: Object key = getPrimaryKey();
216: Object objKey = obj.getPrimaryKey();
217:
218: if (key != null)
219: return key.equals(objKey);
220: else
221: return objKey == null;
222: } catch (Throwable e) {
223: log.log(Level.WARNING, e.toString(), e);
224:
225: return false;
226: }
227: }
228:
229: /**
230: * Returns the string value.
231: */
232: public String toString() {
233: Object key = "";
234: try {
235: key = getPrimaryKey();
236: } catch (Throwable e) {
237: }
238:
239: return getClass().getName() + "[" + key + "]";
240: }
241: }
|