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.ejb3.cache;
023:
024: import java.util.HashMap;
025: import javax.ejb.EJBException;
026: import javax.ejb.NoSuchEJBException;
027: import org.jboss.ejb3.Container;
028: import org.jboss.ejb3.Pool;
029: import org.jboss.ejb3.stateful.StatefulBeanContext;
030: import org.jboss.util.id.GUID;
031:
032: /**
033: * Comment
034: *
035: * @author <a href="mailto:bill@jboss.org">Bill Burke</a>
036: * @version $Revision: 61329 $
037: */
038: public class NoPassivationCache implements StatefulCache {
039: private Pool pool;
040: private HashMap cacheMap;
041: protected int createCount = 0;
042: protected int removeCount = 0;
043:
044: public void initialize(Container container) throws Exception {
045: this .pool = container.getPool();
046: cacheMap = new HashMap();
047: }
048:
049: public NoPassivationCache() {
050: }
051:
052: public void start() {
053: }
054:
055: public void stop() {
056: synchronized (cacheMap) {
057: cacheMap.clear();
058: }
059: }
060:
061: public StatefulBeanContext create() {
062: StatefulBeanContext ctx = null;
063: try {
064: ctx = (StatefulBeanContext) pool.get();
065: ++createCount;
066: synchronized (cacheMap) {
067: cacheMap.put(ctx.getId(), ctx);
068: }
069: } catch (EJBException e) {
070: e.printStackTrace();
071: throw e;
072: } catch (Exception e) {
073: e.printStackTrace();
074: throw new EJBException(e);
075: }
076: return ctx;
077: }
078:
079: public StatefulBeanContext create(Class[] initTypes,
080: Object[] initValues) {
081: StatefulBeanContext ctx = null;
082: try {
083: ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
084: synchronized (cacheMap) {
085: cacheMap.put(ctx.getId(), ctx);
086: }
087: } catch (EJBException e) {
088: throw e;
089: } catch (Exception e) {
090: throw new EJBException(e);
091: }
092: return ctx;
093: }
094:
095: public StatefulBeanContext get(Object key) throws EJBException {
096: return get(key, true);
097: }
098:
099: public StatefulBeanContext get(Object key, boolean markInUse)
100: throws EJBException {
101: StatefulBeanContext entry = null;
102: synchronized (cacheMap) {
103: entry = (StatefulBeanContext) cacheMap.get(key);
104: }
105:
106: if (entry == null) {
107: throw new NoSuchEJBException(
108: "Could not find Stateful bean: " + key);
109: }
110:
111: if (markInUse) {
112: if (entry.isRemoved()) {
113: throw new NoSuchEJBException(
114: "Could not find stateful bean: " + key
115: + " (bean was marked as removed");
116: }
117:
118: entry.setInUse(true);
119: entry.lastUsed = System.currentTimeMillis();
120: }
121:
122: return entry;
123: }
124:
125: public void finished(StatefulBeanContext ctx) {
126: synchronized (ctx) {
127: ctx.setInUse(false);
128: ctx.lastUsed = System.currentTimeMillis();
129: }
130: }
131:
132: public void remove(Object key) {
133: StatefulBeanContext ctx = null;
134: synchronized (cacheMap) {
135: ctx = (StatefulBeanContext) cacheMap.remove(key);
136: }
137: if (ctx != null) {
138: pool.remove(ctx);
139: ++removeCount;
140: }
141: }
142:
143: public int getCacheSize() {
144: return cacheMap.size();
145: }
146:
147: public int getTotalSize() {
148: return cacheMap.size();
149: }
150:
151: public int getCreateCount() {
152: return createCount;
153: }
154:
155: public int getPassivatedCount() {
156: return 0;
157: }
158:
159: public int getRemoveCount() {
160: return removeCount;
161: }
162:
163: public int getAvailableCount() {
164: return -1;
165: }
166:
167: public int getMaxSize() {
168: return -1;
169: }
170:
171: public int getCurrentSize() {
172: return cacheMap.size();
173: }
174:
175: }
|