KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashMap JavaDoc;
25
26 import org.jboss.deployment.DeploymentException;
27 import org.jboss.ejb.EnterpriseContext;
28 import org.jboss.util.CachePolicy;
29 import org.jboss.metadata.MetaData;
30 import org.jboss.metadata.XmlLoadable;
31 import org.jboss.monitor.Monitorable;
32 import org.jboss.monitor.client.BeanCacheSnapshot;
33 import org.w3c.dom.Element JavaDoc;
34
35 /**
36  * Implementation of a no passivation cache policy.
37  *
38  * @see AbstractInstanceCache
39  * @author <a HREF="mailto:simone.bordet@compaq.com">Simone Bordet</a>
40  * @version $Revision: 37459 $
41  */

42 public class NoPassivationCachePolicy
43    implements CachePolicy, Monitorable, XmlLoadable
44 {
45    // Constants -----------------------------------------------------
46

47    // Attributes ----------------------------------------------------
48
private HashMap JavaDoc m_map;
49
50    /** Whether you can flush the cache */
51    private boolean flushEnabled = false;
52
53    // Static --------------------------------------------------------
54

55    // Constructors --------------------------------------------------
56
/**
57     * Creates a no passivation cache policy object given the instance
58     * cache that use this policy object, that btw is not used.
59     */

60    public NoPassivationCachePolicy(AbstractInstanceCache eic)
61    {
62    }
63
64    // Public --------------------------------------------------------
65

66    // Monitorable implementation
67

68    public void sample(Object JavaDoc s)
69    {
70       if(m_map == null)
71          return;
72
73       synchronized(m_map)
74       {
75          BeanCacheSnapshot snapshot = (BeanCacheSnapshot)s;
76          snapshot.m_passivatingBeans = 0;
77          snapshot.m_cacheMinCapacity = 0;
78          snapshot.m_cacheMaxCapacity = Integer.MAX_VALUE;
79          snapshot.m_cacheCapacity = Integer.MAX_VALUE;
80          snapshot.m_cacheSize = m_map.size();
81       }
82    }
83
84    // Z implementation ----------------------------------------------
85
public void create() throws Exception JavaDoc
86    {
87       m_map = new HashMap JavaDoc();
88    }
89
90    public void start() throws Exception JavaDoc
91    {
92    }
93
94    public void stop()
95    {
96    }
97
98    public void destroy()
99    {
100       synchronized (m_map)
101       {
102          m_map.clear();
103       }
104    }
105
106    public Object JavaDoc get(Object JavaDoc key)
107    {
108       if (key == null)
109       {
110          throw new IllegalArgumentException JavaDoc("Requesting an object using a null key");
111       }
112       EnterpriseContext ctx = null;
113       synchronized (m_map)
114       {
115          ctx = (EnterpriseContext) m_map.get(key);
116       }
117       return ctx;
118    }
119
120    public Object JavaDoc peek(Object JavaDoc key)
121    {
122       return get(key);
123    }
124
125    public void insert(Object JavaDoc key, Object JavaDoc ctx)
126    {
127       if (ctx == null)
128       {
129          throw new IllegalArgumentException JavaDoc("Cannot insert a null object in the cache");
130       }
131       if (key == null)
132       {
133          throw new IllegalArgumentException JavaDoc("Cannot insert an object in the cache with null key");
134       }
135
136       synchronized (m_map)
137       {
138          Object JavaDoc obj = m_map.get(key);
139          if (obj == null)
140          {
141             m_map.put(key, ctx);
142          }
143          else
144          {
145             throw new IllegalStateException JavaDoc("Attempt to put in the cache an object that is already there");
146          }
147       }
148    }
149
150    public void remove(Object JavaDoc key)
151    {
152       if (key == null)
153       {
154          throw new IllegalArgumentException JavaDoc("Removing an object using a null key");
155       }
156
157       synchronized (m_map)
158       {
159          Object JavaDoc value = m_map.get(key);
160          if (value != null)
161          {
162             m_map.remove(key);
163          }
164          else
165          {
166             throw new IllegalArgumentException JavaDoc("Cannot remove an object that isn't in the cache");
167          }
168       }
169    }
170
171    public void flush()
172    {
173       if (flushEnabled)
174       {
175          synchronized (m_map)
176          {
177             m_map.clear();
178          }
179       }
180    }
181
182    public int size()
183    {
184       synchronized (m_map)
185       {
186          return m_map.size();
187       }
188    }
189
190    public void importXml(Element JavaDoc element) throws DeploymentException
191    {
192       String JavaDoc flushString = MetaData.getElementContent(MetaData.getOptionalChild(element, "flush-enabled"));
193       flushEnabled = Boolean.valueOf(flushString).booleanValue();
194    }
195
196    // Y overrides ---------------------------------------------------
197

198    // Package protected ---------------------------------------------
199

200    // Protected -----------------------------------------------------
201

202    // Private -------------------------------------------------------
203

204    // Inner classes -------------------------------------------------
205
}
206
Popular Tags