KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > entity > CacheableEntityItem


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.amber.entity;
31
32 import com.caucho.amber.manager.AmberConnection;
33 import com.caucho.util.Alarm;
34
35 import java.sql.SQLException JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * An entity item handles the living entities.
40  */

41 public class CacheableEntityItem extends EntityItem {
42   private AmberEntityHome _home;
43   private Entity _cacheItem;
44   private long _expireTime;
45
46   public CacheableEntityItem(AmberEntityHome home, Entity cacheItem)
47   {
48     _home = home;
49     _cacheItem = cacheItem;
50
51     _expireTime = Alarm.getCurrentTime() + _home.getCacheTimeout();
52   }
53
54   /**
55    * Returns the entity home.
56    */

57   AmberEntityHome getEntityHome()
58   {
59     return _home;
60   }
61
62   /**
63    * Returns the cached entity.
64    *
65    * @return true if the cached value is valid.
66    */

67   public Entity getEntity()
68   {
69     long now = Alarm.getCurrentTime();
70
71     if (_expireTime < now) {
72       _expireTime = now + _home.getCacheTimeout();
73
74       _cacheItem.__caucho_expire();
75     }
76
77     return _cacheItem;
78   }
79
80   /**
81    * Returns the cached entity.
82    *
83    * @return true if the cached value is valid.
84    */

85   public Entity loadEntity(int loadGroup)
86   {
87     return loadEntity(loadGroup, null);
88   }
89
90   /**
91    * Returns the cached entity.
92    *
93    * @return true if the cached value is valid.
94    */

95   public Entity loadEntity(int loadGroup,
96                            Map JavaDoc preloadedProperties)
97   {
98     long now = Alarm.getCurrentTime();
99
100     if (_expireTime < now) {
101       _expireTime = now + _home.getCacheTimeout();
102       _cacheItem.__caucho_expire();
103     }
104
105     AmberConnection aConn = _home.getManager().getCacheConnection();
106
107     try {
108       _cacheItem.__caucho_setConnection(aConn);
109       _cacheItem.__caucho_retrieve(aConn, preloadedProperties);
110     } catch (SQLException JavaDoc e) {
111       // XXX: item is dead
112
throw new RuntimeException JavaDoc(e);
113     } finally {
114       aConn.freeConnection();
115     }
116
117     return _cacheItem;
118   }
119
120   /**
121    * Creates a bean instance
122    */

123   public Entity copy(AmberConnection aConn)
124   {
125     return _cacheItem.__caucho_copy(aConn, this);
126   }
127
128   /**
129    * Saves the item values into the cache.
130    */

131   public void save(Entity item)
132   {
133     /*
134       long now = Alarm.getCurrentTime();
135
136       synchronized (_cacheItem) {
137       _expireTime = now + _home.getCacheTimeout();
138
139       _cacheItem.__caucho_loadFromObject(item);
140       }
141     */

142   }
143
144   /**
145    * Saves the item values into the cache.
146    */

147   public void savePart(Entity item)
148   {
149     /*
150       synchronized (_cacheItem) {
151       _cacheItem.__caucho_loadFromObject(item);
152       }
153     */

154   }
155
156   /**
157    * Expire the value from the cache.
158    */

159   public void expire()
160   {
161     _cacheItem.__caucho_expire();
162   }
163
164   Class JavaDoc getInstanceClass()
165   {
166     return _cacheItem.getClass();
167   }
168
169   public String JavaDoc toString()
170   {
171     return "CacheableEntityItem[" + _cacheItem + "]";
172   }
173 }
174
Popular Tags