001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.ejb.plugins;
023:
024: import java.util.HashMap;
025:
026: import org.jboss.deployment.DeploymentException;
027: import org.jboss.ejb.EnterpriseContext;
028: import org.jboss.util.CachePolicy;
029: import org.jboss.metadata.MetaData;
030: import org.jboss.metadata.XmlLoadable;
031: import org.jboss.monitor.Monitorable;
032: import org.jboss.monitor.client.BeanCacheSnapshot;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Implementation of a no passivation cache policy.
037: *
038: * @see AbstractInstanceCache
039: * @author <a href="mailto:simone.bordet@compaq.com">Simone Bordet</a>
040: * @version $Revision: 57209 $
041: */
042: public class NoPassivationCachePolicy implements CachePolicy,
043: Monitorable, XmlLoadable {
044: // Constants -----------------------------------------------------
045:
046: // Attributes ----------------------------------------------------
047: private HashMap m_map;
048:
049: /** Whether you can flush the cache */
050: private boolean flushEnabled = false;
051:
052: // Static --------------------------------------------------------
053:
054: // Constructors --------------------------------------------------
055: /**
056: * Creates a no passivation cache policy object given the instance
057: * cache that use this policy object, that btw is not used.
058: */
059: public NoPassivationCachePolicy(AbstractInstanceCache eic) {
060: }
061:
062: // Public --------------------------------------------------------
063:
064: // Monitorable implementation
065:
066: public void sample(Object s) {
067: if (m_map == null)
068: return;
069:
070: synchronized (m_map) {
071: BeanCacheSnapshot snapshot = (BeanCacheSnapshot) s;
072: snapshot.m_passivatingBeans = 0;
073: snapshot.m_cacheMinCapacity = 0;
074: snapshot.m_cacheMaxCapacity = Integer.MAX_VALUE;
075: snapshot.m_cacheCapacity = Integer.MAX_VALUE;
076: snapshot.m_cacheSize = m_map.size();
077: }
078: }
079:
080: // Z implementation ----------------------------------------------
081: public void create() throws Exception {
082: m_map = new HashMap();
083: }
084:
085: public void start() throws Exception {
086: }
087:
088: public void stop() {
089: }
090:
091: public void destroy() {
092: synchronized (m_map) {
093: m_map.clear();
094: }
095: }
096:
097: public Object get(Object key) {
098: if (key == null) {
099: throw new IllegalArgumentException(
100: "Requesting an object using a null key");
101: }
102: EnterpriseContext ctx = null;
103: synchronized (m_map) {
104: ctx = (EnterpriseContext) m_map.get(key);
105: }
106: return ctx;
107: }
108:
109: public Object peek(Object key) {
110: return get(key);
111: }
112:
113: public void insert(Object key, Object ctx) {
114: if (ctx == null) {
115: throw new IllegalArgumentException(
116: "Cannot insert a null object in the cache");
117: }
118: if (key == null) {
119: throw new IllegalArgumentException(
120: "Cannot insert an object in the cache with null key");
121: }
122:
123: synchronized (m_map) {
124: Object obj = m_map.get(key);
125: if (obj == null) {
126: m_map.put(key, ctx);
127: } else {
128: throw new IllegalStateException(
129: "Attempt to put in the cache an object that is already there");
130: }
131: }
132: }
133:
134: public void remove(Object key) {
135: if (key == null) {
136: throw new IllegalArgumentException(
137: "Removing an object using a null key");
138: }
139:
140: synchronized (m_map) {
141: Object value = m_map.get(key);
142: if (value != null) {
143: m_map.remove(key);
144: } else {
145: throw new IllegalArgumentException(
146: "Cannot remove an object that isn't in the cache");
147: }
148: }
149: }
150:
151: public void flush() {
152: if (flushEnabled) {
153: synchronized (m_map) {
154: m_map.clear();
155: }
156: }
157: }
158:
159: public int size() {
160: synchronized (m_map) {
161: return m_map.size();
162: }
163: }
164:
165: public void importXml(Element element) throws DeploymentException {
166: String flushString = MetaData.getElementContent(MetaData
167: .getOptionalChild(element, "flush-enabled"));
168: flushEnabled = Boolean.valueOf(flushString).booleanValue();
169: }
170:
171: // Y overrides ---------------------------------------------------
172:
173: // Package protected ---------------------------------------------
174:
175: // Protected -----------------------------------------------------
176:
177: // Private -------------------------------------------------------
178:
179: // Inner classes -------------------------------------------------
180: }
|