KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > cache > NoPassivationCache


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.ejb3.cache;
23
24 import java.util.HashMap JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.NoSuchEJBException JavaDoc;
27 import org.jboss.ejb3.Container;
28 import org.jboss.ejb3.Pool;
29 import org.jboss.ejb3.stateful.StatefulBeanContext;
30 import org.jboss.util.id.GUID;
31
32 /**
33  * Comment
34  *
35  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
36  * @version $Revision: 57789 $
37  */

38 public class NoPassivationCache implements StatefulCache
39 {
40    private Pool pool;
41    private HashMap JavaDoc cacheMap;
42
43    public void initialize(Container container) throws Exception JavaDoc
44    {
45       this.pool = container.getPool();
46       cacheMap = new HashMap JavaDoc();
47    }
48
49    public NoPassivationCache()
50    {
51    }
52
53    public void start()
54    {
55    }
56
57    public void stop()
58    {
59       synchronized (cacheMap)
60       {
61          cacheMap.clear();
62       }
63    }
64
65    public StatefulBeanContext create()
66    {
67       StatefulBeanContext ctx = null;
68       try
69       {
70          ctx = (StatefulBeanContext) pool.get();
71          synchronized (cacheMap)
72          {
73             cacheMap.put(ctx.getId(), ctx);
74          }
75       }
76       catch (EJBException JavaDoc e)
77       {
78          e.printStackTrace();
79          throw e;
80       }
81       catch (Exception JavaDoc e)
82       {
83          e.printStackTrace();
84          throw new EJBException JavaDoc(e);
85       }
86       return ctx;
87    }
88
89    public StatefulBeanContext create(Class JavaDoc[] initTypes, Object JavaDoc[] initValues)
90    {
91       StatefulBeanContext ctx = null;
92       try
93       {
94          ctx = (StatefulBeanContext) pool.get(initTypes, initValues);
95          synchronized (cacheMap)
96          {
97             cacheMap.put(ctx.getId(), ctx);
98          }
99       }
100       catch (EJBException JavaDoc e)
101       {
102          throw e;
103       }
104       catch (Exception JavaDoc e)
105       {
106          throw new EJBException JavaDoc(e);
107       }
108       return ctx;
109    }
110
111    public StatefulBeanContext get(Object JavaDoc key) throws EJBException JavaDoc
112    {
113       StatefulBeanContext entry = null;
114       synchronized (cacheMap)
115       {
116          entry = (StatefulBeanContext) cacheMap.get(key);
117       }
118       if (entry == null)
119       {
120          throw new NoSuchEJBException JavaDoc("Could not find Stateful bean: " + key);
121       }
122       entry.inUse = true;
123       entry.lastUsed = System.currentTimeMillis();
124       return entry;
125    }
126
127    public void finished(StatefulBeanContext ctx)
128    {
129       synchronized (ctx)
130       {
131          ctx.inUse = false;
132          ctx.lastUsed = System.currentTimeMillis();
133       }
134    }
135
136    public void remove(Object JavaDoc key)
137    {
138       StatefulBeanContext ctx = null;
139       synchronized (cacheMap)
140       {
141          ctx = (StatefulBeanContext) cacheMap.remove(key);
142       }
143       if (ctx != null) pool.remove(ctx);
144    }
145
146
147 }
148
Popular Tags