KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > PerTxEntityInstanceCache


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.ejb.plugins;
23
24 import org.jboss.ejb.EnterpriseContext;
25 import org.jboss.ejb.Container;
26 import org.jboss.ejb.EntityContainer;
27 import org.jboss.ejb.EntityEnterpriseContext;
28 import org.jboss.ejb.BeanLock;
29 import org.jboss.ejb.EntityCache;
30 import org.jboss.tm.TransactionLocal;
31 import org.jboss.logging.Logger;
32
33 import java.rmi.RemoteException JavaDoc;
34 import java.rmi.NoSuchObjectException JavaDoc;
35 import java.util.HashMap JavaDoc;
36 import java.util.Map JavaDoc;
37
38 /**
39  * Per transaction instance cache.
40  *
41  * @jmx:mbean
42  *
43  * @author <a HREF="mailto:alex@jboss.org">Alexey Loubyansky</a>
44  * @version <tt>$Revision: 37459 $</tt>
45  */

46 public class PerTxEntityInstanceCache
47    implements EntityCache, PerTxEntityInstanceCacheMBean
48 {
49    private static final Logger log = Logger.getLogger(PerTxEntityInstanceCache.class);
50
51    // per container tx instance cache
52
private final TransactionLocal txLocalCache = new TransactionLocal()
53    {
54       protected Object JavaDoc initialValue()
55       {
56          return new HashMap JavaDoc();
57       }
58    };
59
60    private EntityContainer container;
61
62    // EntityCache implementation
63

64    public Object JavaDoc createCacheKey(Object JavaDoc id)
65    {
66       return id;
67    }
68
69    public EnterpriseContext get(Object JavaDoc id) throws RemoteException JavaDoc, NoSuchObjectException JavaDoc
70    {
71       if(id == null) throw new IllegalArgumentException JavaDoc("Can't get an object with a null key");
72
73       Map JavaDoc cache = getLocalCache();
74       EntityEnterpriseContext instance = (EntityEnterpriseContext) cache.get(id);
75       if(instance == null)
76       {
77          try
78          {
79             // acquire
80
instance = (EntityEnterpriseContext) container.getInstancePool().get();
81             // set key
82
instance.setId(id);
83             instance.setCacheKey(id);
84             // activate
85
container.getPersistenceManager().activateEntity(instance);
86             // insert
87
cache.put(id, instance);
88          }
89          catch(Throwable JavaDoc x)
90          {
91             throw new NoSuchObjectException JavaDoc(x.getMessage());
92          }
93       }
94       return instance;
95    }
96
97    public void insert(EnterpriseContext instance)
98    {
99       if(instance == null) throw new IllegalArgumentException JavaDoc("Can't insert a null object in the cache");
100
101       EntityEnterpriseContext entity = (EntityEnterpriseContext) instance;
102       getLocalCache().put(entity.getCacheKey(), instance);
103    }
104
105    public void release(EnterpriseContext instance)
106    {
107       if(instance == null) throw new IllegalArgumentException JavaDoc("Can't release a null object");
108
109       tryToPassivate(instance);
110    }
111
112    public void remove(Object JavaDoc id)
113    {
114       getLocalCache().remove(id);
115    }
116
117    public boolean isActive(Object JavaDoc id)
118    {
119       return getLocalCache().containsKey(id);
120    }
121
122    public long getCacheSize()
123    {
124       return 0;
125    }
126
127    public void flush()
128    {
129    }
130
131    // ContainerPlugin implementation
132

133    public void setContainer(Container con)
134    {
135       this.container = (EntityContainer) con;
136    }
137
138    // Service implementation
139

140    public void create() throws Exception JavaDoc
141    {
142    }
143
144    public void start() throws Exception JavaDoc
145    {
146    }
147
148    public void stop()
149    {
150    }
151
152    public void destroy()
153    {
154    }
155
156    // Protected
157

158    protected void tryToPassivate(EnterpriseContext instance)
159    {
160       Object JavaDoc id = instance.getId();
161       if(id != null)
162       {
163          BeanLock lock = container.getLockManager().getLock(id);
164          try
165          {
166             lock.sync();
167             if(canPassivate(instance))
168             {
169                try
170                {
171                   remove(id);
172                   EntityEnterpriseContext entity = (EntityEnterpriseContext) instance;
173                   container.getPersistenceManager().passivateEntity(entity);
174                   container.getInstancePool().free(instance);
175                }
176                catch(Exception JavaDoc ignored)
177                {
178                   log.warn("failed to passivate, id=" + id, ignored);
179                }
180             }
181             else
182             {
183                log.warn("Unable to passivate due to ctx lock, id=" + id);
184             }
185          }
186          finally
187          {
188             lock.releaseSync();
189             container.getLockManager().removeLockRef(id);
190          }
191       }
192    }
193
194    protected boolean canPassivate(EnterpriseContext ctx)
195    {
196       if(ctx.isLocked())
197       {
198          // The context is in the interceptor chain
199
return false;
200       }
201
202       if(ctx.getTransaction() != null)
203       {
204          return false;
205       }
206
207       Object JavaDoc key = ((EntityEnterpriseContext) ctx).getCacheKey();
208       return container.getLockManager().canPassivate(key);
209    }
210
211    // Private
212

213    private Map JavaDoc getLocalCache()
214    {
215       return (Map JavaDoc) txLocalCache.get();
216    }
217 }
218
Popular Tags