KickJava   Java API By Example, From Geeks To Geeks.

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


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.cache.invalidation.InvalidationManagerMBean;
25 import org.jboss.cache.invalidation.InvalidationGroup;
26 import org.jboss.metadata.ConfigurationMetaData;
27 import org.jboss.metadata.EntityMetaData;
28 import org.jboss.system.Registry;
29 import org.jboss.ejb.EnterpriseContext;
30
31 /**
32  * Cache implementation that registers with an InvalidationManager when in
33  * commit option A or D. Information is found in the EB meta-data (IM name,
34  * IG name and commit-option)
35  *
36  * @see org.jboss.cache.invalidation.InvalidationManagerMBean
37  * @see org.jboss.cache.invalidation.triggers.EntityBeanCacheBatchInvalidatorInterceptor
38  *
39  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
40  * @version $Revision: 44061 $
41  */

42 public class InvalidableEntityInstanceCache
43       extends org.jboss.ejb.plugins.EntityInstanceCache
44       implements org.jboss.cache.invalidation.Invalidatable
45 {
46
47    // Constants -----------------------------------------------------
48

49    // Attributes ----------------------------------------------------
50

51    protected InvalidationManagerMBean invalMgr = null;
52    protected InvalidationGroup ig = null;
53
54    protected boolean isTraceEnabled = false;
55
56    // Static --------------------------------------------------------
57

58    // Constructors --------------------------------------------------
59

60    public InvalidableEntityInstanceCache()
61    {
62       super();
63    }
64
65    // Public --------------------------------------------------------
66

67    // Invalidatable implementation ----------------------------------------------
68

69    public void areInvalid(java.io.Serializable JavaDoc[] keys)
70    {
71       if (this.isTraceEnabled)
72          log.trace("Invalidating entry in cache. Quantity: " + keys.length);
73
74       for (int i = 0; i < keys.length; i++)
75       {
76          try
77          {
78             doInvalidate(keys[i]);
79          }
80          catch (Exception JavaDoc ignored)
81          {
82             log.debug(ignored);
83          }
84       }
85    }
86
87    public void isInvalid(java.io.Serializable JavaDoc key)
88    {
89       try
90       {
91          doInvalidate(key);
92       }
93       catch (Exception JavaDoc ignored)
94       {
95          log.debug(ignored);
96       }
97    }
98
99    public void invalidateAll()
100    {
101       flush();
102    }
103
104    // ServiceMBeanSupport overrides ---------------------------------------------------
105

106    public void start() throws Exception JavaDoc
107    {
108       super.start();
109
110       log.debug("Starting InvalidableEntityInstanceCache...");
111
112       EntityMetaData emd = ((EntityMetaData) this.getContainer().getBeanMetaData());
113
114       boolean participateInDistInvalidations = emd.doDistributedCacheInvalidations();
115       byte co = emd.getContainerConfiguration().getCommitOption();
116
117       if (participateInDistInvalidations &&
118             (co == ConfigurationMetaData.A_COMMIT_OPTION || co == ConfigurationMetaData.D_COMMIT_OPTION)
119       )
120       {
121          // we are interested in receiving cache invalidation callbacks
122
//
123
String JavaDoc groupName = emd.getDistributedCacheInvalidationConfig().getInvalidationGroupName();
124          String JavaDoc imName = emd.getDistributedCacheInvalidationConfig().getInvalidationManagerName();
125
126          this.invalMgr = (InvalidationManagerMBean) Registry.lookup(imName);
127          this.ig = this.invalMgr.getInvalidationGroup(groupName);
128          this.ig.register(this);
129          this.isTraceEnabled = log.isTraceEnabled();
130       }
131
132    }
133
134    public void stop()
135    {
136       try
137       {
138          this.ig.unregister(this);
139          this.ig = null;
140          this.invalMgr = null;
141       }
142       catch (Exception JavaDoc e)
143       {
144          log.debug(e);
145       }
146
147       super.stop();
148    }
149
150    // Package protected ---------------------------------------------
151

152    // Protected -----------------------------------------------------
153

154    protected void doInvalidate(java.io.Serializable JavaDoc key)
155    {
156       if (key != null)
157       {
158          synchronized (getCacheLock())
159          {
160             EnterpriseContext ctx = (EnterpriseContext)getCache().peek(key);
161             if(ctx != null)
162             {
163                release(ctx);
164             }
165          }
166       }
167    }
168
169    // Private -------------------------------------------------------
170

171    // Inner classes -------------------------------------------------
172

173 }
174
Popular Tags