KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > ejb > entity > EntityObject


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.ejb.entity;
30
31 import com.caucho.ejb.AbstractEJBObject;
32 import com.caucho.ejb.AbstractServer;
33 import com.caucho.ejb.xa.TransactionContext;
34 import com.caucho.util.Log;
35
36 import javax.ejb.EJBHome JavaDoc;
37 import javax.ejb.EJBLocalHome JavaDoc;
38 import javax.ejb.EJBLocalObject JavaDoc;
39 import javax.ejb.EJBObject JavaDoc;
40 import javax.ejb.Handle JavaDoc;
41 import java.io.Serializable JavaDoc;
42 import java.rmi.RemoteException JavaDoc;
43 import java.util.logging.Level JavaDoc;
44 import java.util.logging.Logger JavaDoc;
45
46 /**
47  * Abstract base class for an EntityObject.
48  */

49 abstract public class EntityObject extends AbstractEJBObject
50   implements Serializable JavaDoc, EJBLocalObject JavaDoc
51 {
52   protected static final Logger JavaDoc log = Log.open(EntityObject.class);
53
54   public static final byte _CAUCHO_IS_DEAD = QEntity._CAUCHO_IS_DEAD;
55   public static final byte _CAUCHO_IS_ACTIVE = QEntity._CAUCHO_IS_ACTIVE;
56   public static final byte _CAUCHO_IS_LOADED = QEntity._CAUCHO_IS_LOADED;
57   public static final byte _CAUCHO_IS_DIRTY = QEntity._CAUCHO_IS_DIRTY;
58
59   protected abstract QEntityContext getEntityContext();
60
61   /**
62    * Returns the Entity bean's primary key
63    */

64   public Object JavaDoc getPrimaryKey()
65   {
66     return getEntityContext().getPrimaryKey();
67   }
68   
69   public Object JavaDoc getEJBObject()
70   {
71     return getEntityContext().getEJBObject();
72   }
73   
74   public Object JavaDoc getEJBLocalObject()
75   {
76     return getEntityContext().getEJBLocalObject();
77   }
78
79   /**
80    * Returns the server.
81    */

82   public EntityServer _caucho_getEntityServer()
83   {
84     return getEntityContext().getEntityServer();
85   }
86
87   /**
88    * Return if matching context.
89    */

90   public boolean isMatch(EntityServer server, Object JavaDoc key)
91   {
92     return server == _caucho_getEntityServer() && key.equals(getPrimaryKey());
93   }
94
95   /**
96    * Returns the handle.
97    */

98   public Handle JavaDoc getHandle()
99   {
100     return getEntityContext().getHandle();
101   }
102   
103   /**
104    * Returns an underlying bean
105    */

106   public Object JavaDoc _caucho_getBean(TransactionContext trans, boolean doLoad)
107   {
108     throw new UnsupportedOperationException JavaDoc();
109   }
110   
111   /**
112    * Returns an underlying bean
113    */

114   public Object JavaDoc _caucho_getBean()
115   {
116     throw new UnsupportedOperationException JavaDoc();
117   }
118
119   /**
120    * Returns the EJBHome stub for the container.
121    */

122   public EJBHome JavaDoc getEJBHome()
123   {
124     try {
125       return _caucho_getEntityServer().getEJBHome();
126     } catch (Exception JavaDoc e) {
127       return null;
128     }
129   }
130   
131   /**
132    * Returns the EJBHome stub for the container.
133    */

134   public EJBLocalHome JavaDoc getEJBLocalHome()
135   {
136     try {
137       return (EJBLocalHome JavaDoc) _caucho_getEntityServer().getEJBLocalHome();
138     } catch (Exception JavaDoc e) {
139       return null;
140     }
141   }
142
143   /**
144    * Returns true if the two objects are identical.
145    */

146   public boolean isIdentical(EJBObject obj) throws RemoteException JavaDoc
147   {
148     return getHandle().equals(obj.getHandle());
149   }
150
151   /**
152    * Returns true if the two objects are identical.
153    */

154   public boolean isIdentical(EJBLocalObject JavaDoc o)
155   {
156     if (o == null || ! getClass().equals(o.getClass()))
157       return false;
158
159     EntityObject obj = (EntityObject) o;
160
161     try {
162       Object JavaDoc key = getPrimaryKey();
163       Object JavaDoc objKey = obj.getPrimaryKey();
164       
165       if (key != null)
166         return key.equals(objKey);
167       else
168         return objKey == null;
169     } catch (Exception JavaDoc e) {
170       return false;
171     }
172   }
173
174   /**
175    * Remove the object.
176    */

177   /*
178   public void remove() throws RemoveException
179   {
180     _caucho_getServer().remove(getHandle());
181   }
182   */

183
184   /**
185    * Returns the server.
186    */

187   public AbstractServer __caucho_getServer()
188   {
189     return _caucho_getEntityServer();
190   }
191
192   /**
193    * The home id is null.
194    */

195   public String JavaDoc __caucho_getId()
196   {
197     return _caucho_getEntityServer().encodeId(getPrimaryKey());
198   }
199
200   /**
201    * Returns a hash code for the object.
202    */

203   public int hashCode()
204   {
205     try {
206       Object JavaDoc key = getPrimaryKey();
207       
208       if (key != null)
209         return key.hashCode();
210       else
211         return 0;
212     } catch (Throwable JavaDoc e) {
213       return 0;
214     }
215   }
216
217   /**
218    * Returns true if this object equals the test object.
219    */

220
221   public boolean equals(Object JavaDoc o)
222   {
223     if (this == o)
224       return true;
225
226     if (o == null || ! getClass().equals(o.getClass()))
227       return false;
228
229     EntityObject obj = (EntityObject) o;
230
231     try {
232       Object JavaDoc key = getPrimaryKey();
233       Object JavaDoc objKey = obj.getPrimaryKey();
234       
235       if (key != null)
236         return key.equals(objKey);
237       else
238         return objKey == null;
239     } catch (Throwable JavaDoc e) {
240       log.log(Level.WARNING, e.toString(), e);
241       
242       return false;
243     }
244   }
245
246   /**
247    * Returns the string value.
248    */

249   public String JavaDoc toString()
250   {
251     Object JavaDoc key = "";
252     try {
253       key = getPrimaryKey();
254     } catch (Throwable JavaDoc e) {
255     }
256
257     return getClass().getName() + "[" + key + "]";
258   }
259 }
260
Popular Tags