KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.NoSuchObjectException JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import org.jboss.ejb.EnterpriseContext;
27
28 /**
29  * SFSB cache for clustered environment. Mainly avoid "excessive" locking
30  * that can generate cluster-distributed deadlocks.
31  *
32  * @see org.jboss.ejb.plugins.StatefulSessionInstanceCache
33  *
34  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>.
35  * @version $Revision: 37459 $
36  *
37  * <p><b>Revisions:</b>
38  *
39  * <p><b>19 decembre 2002 Sacha Labourey:</b>
40  * <ul>
41  * <li> First implementation </li>
42  * </ul>
43  */

44
45 public class StatefulHASessionInstanceCache
46    extends StatefulSessionInstanceCache
47 {
48    
49    // Constants -----------------------------------------------------
50

51    // Attributes ----------------------------------------------------
52

53    // Static --------------------------------------------------------
54

55    // Constructors --------------------------------------------------
56

57    // Public --------------------------------------------------------
58

59    /**
60     * Remove an object from the local cache *without* any locking
61     * (synchronized, etc.) to avoid huge cluster-wide deadlock situations
62     * We have to unschedule passivation as well as the bean may be
63     * used on another node.
64     */

65    public void invalidateLocally (Object JavaDoc id)
66    {
67       if (id == null) return;
68       
69       try
70       {
71          getCache().remove(id);
72       }
73       catch (Exception JavaDoc e)
74       {
75          log.debug (e);
76       }
77    }
78    
79    // Z implementation ----------------------------------------------
80

81    // AbstractInstanceCache overrides -------------------------------
82

83    /*
84     * Make less extensive use of global locking when state must be activated: it
85     * will most probably generate time-costly cluster-wide communication =>
86     * we must avoid globally locking the whole cache! Furthermore, The
87     * StatefulSessionInstanceInterceptor *already* locks concurrent access to the
88     * cache that targets the *same* SFSB identity
89     */

90    public EnterpriseContext get(Object JavaDoc id)
91       throws RemoteException JavaDoc, NoSuchObjectException JavaDoc
92    {
93       if (id == null) throw new IllegalArgumentException JavaDoc("Can't get an object with a null key");
94
95       EnterpriseContext ctx = null;
96
97       synchronized (getCacheLock())
98       {
99          ctx = (EnterpriseContext)getCache().get(id);
100          if (ctx != null)
101          {
102             return ctx;
103          }
104       }
105
106       // If the ctx is still null at this point, it means that we must activate it
107
// => we don't lock the cache during this operation
108
// StatefulSessionInstanceInterceptor prevents multiple accesses for the same id
109
//
110
try
111       {
112          ctx = acquireContext();
113          setKey(id, ctx);
114          activate(ctx);
115          logActivation(id);
116          insert(ctx);
117       }
118       catch (Exception JavaDoc x)
119       {
120          if (ctx != null)
121             freeContext(ctx);
122          log.debug("Activation failure, id="+id, x);
123          throw new NoSuchObjectException JavaDoc(x.getMessage());
124       }
125       
126       // FIXME marcf: How can this ever be reached? the ctx is always assigned
127
if (ctx == null) throw new NoSuchObjectException JavaDoc("Can't find bean with id = " + id);
128
129       return ctx;
130    }
131    
132    
133
134    // Package protected ---------------------------------------------
135

136    // Protected -----------------------------------------------------
137

138    // Private -------------------------------------------------------
139

140    // Inner classes -------------------------------------------------
141

142 }
143
Popular Tags