KickJava   Java API By Example, From Geeks To Geeks.

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


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.amber.entity.EntityItem;
32 import com.caucho.ejb.AbstractContext;
33 import com.caucho.ejb.AbstractServer;
34 import com.caucho.ejb.xa.EjbTransactionManager;
35
36 import javax.ejb.EJBObject JavaDoc;
37 import javax.ejb.EntityContext JavaDoc;
38 import javax.ejb.FinderException JavaDoc;
39 import javax.ejb.Handle JavaDoc;
40 import javax.ejb.HomeHandle JavaDoc;
41 import javax.ejb.RemoveException JavaDoc;
42 import javax.transaction.UserTransaction JavaDoc;
43 import java.util.logging.Level JavaDoc;
44
45 /**
46  * Abstract base class for an EntityHome.
47  */

48 public abstract class QEntityContext extends AbstractContext
49   implements EntityContext JavaDoc {
50   protected final EntityServer _server;
51   public Object JavaDoc _primaryKey;
52   
53   private Object JavaDoc _remoteView;
54   private EJBObject JavaDoc _remote;
55
56   protected QEntityContext(EntityServer server)
57   {
58     _server = server;
59   }
60
61   /**
62    * Returns the owning server.
63    */

64   public AbstractServer getServer()
65   {
66     return _server;
67   }
68
69   /**
70    * Returns the owning server.
71    */

72   public EntityServer getEntityServer()
73   {
74     return _server;
75   }
76
77   /**
78    * Returns the transaction manager.
79    */

80   public EjbTransactionManager getTransactionManager()
81   {
82     return getEntityServer().getTransactionManager();
83   }
84
85   /**
86    * Returns the home handle for the home bean.
87    */

88   public HomeHandle JavaDoc getHomeHandle()
89   {
90     return getServer().getHomeHandle();
91   }
92
93   public void setPrimaryKey(Object JavaDoc key)
94   {
95     _primaryKey = key;
96   }
97
98   /**
99    * Returns null, since primary key isn't available to the home.
100    */

101   public Object JavaDoc getPrimaryKey()
102     throws IllegalStateException JavaDoc
103   {
104     if (_primaryKey == null) {
105       throw new IllegalStateException JavaDoc("Can't get primary key. The context may belong to a home instance or it may be called before ejbActivate or ejbCreate.");
106     }
107     else
108       return _primaryKey;
109   }
110
111   /**
112    * Returns null, since primary key isn't available to the home.
113    */

114   public Object JavaDoc _caucho_getPrimaryKey()
115   {
116     return _primaryKey;
117   }
118
119   /**
120    * Returns the current UserTransaction. Only Session beans with
121    * bean-managed transactions may use this.
122    */

123   public UserTransaction JavaDoc getUserTransaction()
124     throws IllegalStateException JavaDoc
125   {
126     throw new IllegalStateException JavaDoc("Entity beans may not call getUserTransaction()");
127   }
128
129   /**
130    * Returns the object's handle.
131    */

132   public Handle JavaDoc getHandle()
133   {
134     return getEntityServer().createHandle(_primaryKey);
135   }
136
137   /**
138    * Returns null, since the ejb object isn't available to the home.
139    */

140   public EJBObject JavaDoc getEJBObject()
141     throws IllegalStateException JavaDoc
142   {
143     /*
144     if (_remote == null)
145       _remote = getEntityServer().createEJBObject(getPrimaryKey());
146     
147     return _remote;
148     */

149     return getRemoteView();
150   }
151
152   /**
153    * Finish any caching for the entity.
154    */

155   public void postCreate(Object JavaDoc primaryKey)
156   {
157     _primaryKey = primaryKey;
158
159     getEntityServer().postCreate(primaryKey, this);
160   }
161
162   /**
163    * Remove the object specified by the handle.
164    */

165   public void remove(Handle JavaDoc handle)
166   {
167     getServer().remove(handle);
168   }
169   
170   /**
171    * Remove the object specified by the primary key.
172    */

173   public void remove(Object JavaDoc primaryKey)
174   {
175     getServer().remove(primaryKey);
176   }
177
178   public void _caucho_remove_callback(Class JavaDoc listenClass, Object JavaDoc primaryKey)
179     throws RemoveException JavaDoc
180   {
181   }
182
183   /**
184    * Invalidate the cache the entity server.
185    */

186   public void invalidateHomeCache()
187   {
188     getServer().invalidateCache();
189   }
190
191   /**
192    * Update the context.
193    */

194   public void update()
195   {
196   }
197
198   /**
199    * Sets the Amber entity item.
200    */

201   protected void __caucho_setAmber(EntityItem item)
202   {
203   }
204
205   public abstract void _caucho_load() throws FinderException JavaDoc;
206   
207   /**
208    * Callback when removed from the cache.
209    */

210   public void removeEvent()
211   {
212     try {
213       destroy();
214     } catch (Exception JavaDoc e) {
215       log.log(Level.WARNING, e.toString(), e);
216     }
217   }
218
219   /**
220    * Destroy the context.
221    */

222   public void destroy() throws Exception JavaDoc
223   {
224     super.destroy();
225     
226     _remote = null;
227     _remoteView = null;
228   }
229 }
230
Popular Tags