KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Method JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26
27 import org.jboss.ejb.Container;
28 import org.jboss.ejb.StatefulSessionContainer;
29 import org.jboss.ejb.StatefulSessionEnterpriseContext;
30 import org.jboss.ejb.EnterpriseContext;
31 import org.jboss.invocation.Invocation;
32
33 /**
34  * This interceptor synchronizes a HA SFSB instance with its underlying persistent manager.
35  *
36  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
37  * @version $Revision: 37459 $
38  *
39  * <p><b>Revisions:</b>
40   * <p><b>2002/07/28: <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a></b>
41  * <ol>
42  * <li>Added isModified check before replication (TaskId 57031)
43  * </ol>
44 */

45 public class StatefulHASessionSynchronisationInterceptor
46    extends AbstractInterceptor
47 {
48    protected StatefulSessionContainer container;
49    
50    // optional isModified method used to avoid unecessary state replication
51
//
52
protected Method JavaDoc isModified = null;
53
54    public void start () throws Exception JavaDoc
55    {
56       // Lookup the isModified method if it exists
57
//
58
try
59       {
60          isModified = this.container.getBeanClass().getMethod("isModified", new Class JavaDoc[0]);
61          if (!isModified.getReturnType().equals(Boolean.TYPE)) {
62             isModified = null; // Has to have "boolean" as return type!
63
log.warn("Found isModified method, but return type is not boolean; ignoring");
64          }
65          else
66          {
67             log.debug("Using isModified method: " + isModified);
68          }
69       }
70       catch (NoSuchMethodException JavaDoc ignored) {}
71    }
72    
73    public Object JavaDoc invokeHome (Invocation mi)
74       throws Exception JavaDoc
75    {
76       EnterpriseContext ctx = (EnterpriseContext)mi.getEnterpriseContext ();
77       
78       try
79       {
80          // Invoke through interceptors
81
return getNext ().invokeHome (mi);
82       }
83       finally
84       {
85          if ( (ctx != null) && (ctx.getId () != null) )
86             // Still a valid instance and instance not removed
87
//
88
{
89             // Everything went ok (at least no J2EE problem) and the instance will most probably be called
90
// many more times. Consequently, we need to synchronize the state of our bean instance with
91
// our persistant store (which will forward this to its HASessionState implementation) for clustering
92
// behaviour. This is only necessary for "create" calls (which is the case because ctx.getId() != null)
93
//
94
synchronizeState (ctx);
95          }
96       }
97    }
98    
99    public Object JavaDoc invoke (Invocation mi)
100       throws Exception JavaDoc
101    {
102       EnterpriseContext ctx = (EnterpriseContext)mi.getEnterpriseContext ();
103       
104       try
105       {
106          // Invoke through interceptors
107
return getNext ().invoke (mi);
108       }
109       catch (RemoteException JavaDoc e)
110       {
111          ctx = null;
112          throw e;
113       }
114       catch (RuntimeException JavaDoc e)
115       {
116          ctx = null;
117          throw e;
118       }
119       catch (Error JavaDoc e)
120       {
121          ctx = null;
122          throw e;
123       }
124       finally
125       {
126          if ( (ctx != null) && (ctx.getId () != null) )
127             // Still a valid instance and instance not removed
128
//
129
{
130             // Everything went ok (at least no J2EE problem) and the instance will most probably be called
131
// many more times. Consequently, we need to synchronize the state of our bean instance with
132
// our persistant store (which will forward this to its HASessionState implementation) for clustering
133
// behaviour.
134
//
135

136             if(isModified == null)
137             {
138                synchronizeState (ctx);
139             }
140             else
141             {
142                Boolean JavaDoc modified = (Boolean JavaDoc) isModified.invoke (ctx.getInstance (), new Object JavaDoc[0]);
143                if (modified.booleanValue ())
144                   synchronizeState (ctx);
145             }
146          }
147       }
148    }
149    
150    protected void synchronizeState (EnterpriseContext ctx) throws Exception JavaDoc
151    {
152       ((HAPersistentManager)container.getPersistenceManager ()).synchroSession ((StatefulSessionEnterpriseContext)ctx);
153    }
154    
155    /**
156     * This callback is set by the container so that the plugin may access it
157     *
158     * @param container The container using this plugin.
159     */

160    public void setContainer (Container container)
161    {
162       this.container = (StatefulSessionContainer)container;
163    }
164    
165    public Container getContainer ()
166    {
167       return container;
168    }
169    
170 }
171
172
Popular Tags