KickJava   Java API By Example, From Geeks To Geeks.

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


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.RemoteException JavaDoc;
25
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.ByteArrayOutputStream JavaDoc;
28 import java.io.ObjectInputStream JavaDoc;
29 import java.io.ObjectOutputStream JavaDoc;
30 import java.io.IOException JavaDoc;
31
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.RemoveException JavaDoc;
34 import javax.ejb.EJBException JavaDoc;
35
36 import javax.naming.Context JavaDoc;
37 import javax.naming.InitialContext JavaDoc;
38
39 import org.jboss.ejb.Container;
40 import org.jboss.ejb.StatefulSessionContainer;
41 import org.jboss.ejb.StatefulSessionPersistenceManager;
42 import org.jboss.ejb.StatefulSessionEnterpriseContext;
43 import org.jboss.metadata.ClusterConfigMetaData;
44
45 import org.jboss.ha.hasessionstate.interfaces.HASessionState;
46 import org.jboss.ha.hasessionstate.interfaces.PackagedSession;
47 import org.jboss.ha.framework.interfaces.HAPartition;
48
49 import org.jboss.system.ServiceMBeanSupport;
50
51 import org.jboss.util.id.UID;
52 /**
53  * This persistence manager work with an underlying HASessionState to get
54  * clustered state.
55  *
56  * @see HASessionState
57  * @see org.jboss.ha.hasessionstate.server.HASessionStateImpl
58  *
59  * @author <a HREF="mailto:sacha.labourey@cogito-info.ch">Sacha Labourey</a>
60  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
61  * @version $Revision: 37459 $
62  *
63  * <p><b>Revisions:</b>
64  */

65
66 public class StatefulHASessionPersistenceManager
67     extends ServiceMBeanSupport
68     implements StatefulSessionPersistenceManager,
69           HASessionState.HASessionStateListener,
70           HAPersistentManager
71 {
72
73    /** Creates new StatefulHASessionPersistenceManager */
74    public StatefulHASessionPersistenceManager ()
75    {
76    }
77
78    // Constants -----------------------------------------------------
79

80    // Attributes ----------------------------------------------------
81

82    private StatefulSessionContainer con;
83
84    private HASessionState sessionState = null;
85
86    private String JavaDoc localNodeName = null;
87    private String JavaDoc appName = null;
88
89    // Static --------------------------------------------------------
90

91    private static String JavaDoc DEFAULT_HASESSIONSTATE_JNDI_NAME = "/HASessionState/Default";
92
93    // Constructors --------------------------------------------------
94

95    // Public --------------------------------------------------------
96

97    public void setContainer (Container c)
98    {
99       con = (StatefulSessionContainer)c;
100    }
101
102    protected void createService() throws Exception JavaDoc
103    {
104       // Initialize the dataStore
105

106       // Find HASessionState that we will use
107
//
108
String JavaDoc sessionStateName = org.jboss.metadata.ClusterConfigMetaData.DEFAULT_SESSION_STATE_NAME;
109       ClusterConfigMetaData config = con.getBeanMetaData ().getClusterConfigMetaData ();
110       if (config != null)
111          sessionStateName = config.getHaSessionStateName ();
112
113       Context JavaDoc ctx = new InitialContext JavaDoc ();
114       try {
115          this.sessionState = (HASessionState)ctx.lookup (sessionStateName);
116       }
117       finally {
118          ctx.close();
119       }
120
121       this.localNodeName = this.sessionState.getNodeName ();
122       this.appName = this.con.getBeanMetaData ().getJndiName ();
123
124       this.sessionState.subscribe (this.appName, this);
125    }
126
127    protected void stopService ()
128    {
129       this.sessionState.unsubscribe (this.appName, this);
130    }
131
132    public Object JavaDoc createId(StatefulSessionEnterpriseContext ctx)
133       throws Exception JavaDoc
134    {
135       //
136
// jason: could probably use org.jboss.util.collection.CompoundKey here
137
// for better uniqueness based on hashCode and such...
138
//
139
// return new CompoundKey(this.localNodeName, new UID());
140
//
141
return this.localNodeName + ":" + new UID();
142    }
143
144    public void createdSession(StatefulSessionEnterpriseContext ctx)
145       throws Exception JavaDoc
146    {
147       this.sessionState.createSession(this.appName, ctx.getId());
148    }
149
150    public void activateSession (StatefulSessionEnterpriseContext ctx) throws RemoteException JavaDoc
151    {
152       try
153       {
154          ObjectInputStream JavaDoc in;
155
156          // Load state
157
PackagedSession state = this.sessionState.getStateWithOwnership (this.appName, ctx.getId ());
158
159          if (state == null)
160             throw new EJBException JavaDoc ("Could not activate; failed to recover session (session has been probably removed by session-timeout)");
161
162          in = new SessionObjectInputStream (ctx, new ByteArrayInputStream JavaDoc (state.getState ()));;
163
164          ctx.setInstance ( in.readObject () );
165
166          in.close ();
167
168          //removePassivated (ctx.getId ());
169

170
171          // Instruct the bean to perform activation logic
172
((SessionBean JavaDoc)ctx.getInstance()).ejbActivate();
173       }
174       catch (ClassNotFoundException JavaDoc e)
175       {
176          throw new EJBException JavaDoc ("Could not activate", e);
177       }
178       catch (IOException JavaDoc e)
179       {
180          throw new EJBException JavaDoc ("Could not activate", e);
181       }
182    }
183
184    public void passivateSession (StatefulSessionEnterpriseContext ctx)
185       throws RemoteException JavaDoc
186    {
187       // do nothing
188
}
189
190    public void synchroSession (StatefulSessionEnterpriseContext ctx) throws RemoteException JavaDoc
191    {
192       try
193       {
194          // Call bean
195
((SessionBean JavaDoc)ctx.getInstance()).ejbPassivate();
196
197          // Store state
198
ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc ();
199          ObjectOutputStream JavaDoc out = new SessionObjectOutputStream (baos);
200
201          out.writeObject (ctx.getInstance ());
202
203          out.close ();
204
205          this.sessionState.setState (this.appName, ctx.getId (), baos.toByteArray ());
206
207          ((SessionBean JavaDoc)ctx.getInstance()).ejbActivate(); //needed?
208
}
209       catch (IOException JavaDoc e)
210       {
211          throw new EJBException JavaDoc ("Could not passivate", e);
212       }
213    }
214
215    public void removeSession (StatefulSessionEnterpriseContext ctx)
216       throws RemoteException JavaDoc, RemoveException JavaDoc
217    {
218       try
219       {
220          // Call bean
221
((SessionBean JavaDoc)ctx.getInstance()).ejbRemove();
222       }
223       finally
224       {
225          this.sessionState.removeSession (this.appName, ctx.getId ());
226       }
227    }
228
229    public void removePassivated (Object JavaDoc key)
230    {
231       this.sessionState.removeSession (this.appName, key);
232    }
233
234    // HASessionState.HASessionStateListener methods -----------------
235
//
236
public void sessionExternallyModified (PackagedSession session)
237    {
238       // this callback warns us that a session (i.e. a bean) has been externally modified
239
// this means that we need to tell to our InstanceCache that this bean is no more under its control
240
// and that it should not keep a cached version of it => CACHE MISS
241
//
242
log.trace ("Invalidating cache for session: " + session.getKey());
243       this.con.getInstanceCache ().remove (session.getKey ());
244    }
245
246    public void newSessionStateTopology (HAPartition haSubPartition)
247    {
248    }
249
250 }
251
Popular Tags