1 22 package org.jboss.ejb.plugins; 23 24 import java.rmi.RemoteException ; 25 26 import java.io.ByteArrayInputStream ; 27 import java.io.ByteArrayOutputStream ; 28 import java.io.ObjectInputStream ; 29 import java.io.ObjectOutputStream ; 30 import java.io.IOException ; 31 32 import javax.ejb.SessionBean ; 33 import javax.ejb.RemoveException ; 34 import javax.ejb.EJBException ; 35 36 import javax.naming.Context ; 37 import javax.naming.InitialContext ; 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 65 66 public class StatefulHASessionPersistenceManager 67 extends ServiceMBeanSupport 68 implements StatefulSessionPersistenceManager, 69 HASessionState.HASessionStateListener, 70 HAPersistentManager 71 { 72 73 74 public StatefulHASessionPersistenceManager () 75 { 76 } 77 78 80 82 private StatefulSessionContainer con; 83 84 private HASessionState sessionState = null; 85 86 private String localNodeName = null; 87 private String appName = null; 88 89 91 private static String DEFAULT_HASESSIONSTATE_JNDI_NAME = "/HASessionState/Default"; 92 93 95 97 public void setContainer (Container c) 98 { 99 con = (StatefulSessionContainer)c; 100 } 101 102 protected void createService() throws Exception 103 { 104 106 String 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 ctx = new InitialContext (); 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 createId(StatefulSessionEnterpriseContext ctx) 133 throws Exception 134 { 135 return this.localNodeName + ":" + new UID(); 142 } 143 144 public void createdSession(StatefulSessionEnterpriseContext ctx) 145 throws Exception 146 { 147 this.sessionState.createSession(this.appName, ctx.getId()); 148 } 149 150 public void activateSession (StatefulSessionEnterpriseContext ctx) throws RemoteException 151 { 152 try 153 { 154 ObjectInputStream in; 155 156 PackagedSession state = this.sessionState.getStateWithOwnership (this.appName, ctx.getId ()); 158 159 if (state == null) 160 throw new EJBException ("Could not activate; failed to recover session (session has been probably removed by session-timeout)"); 161 162 in = new SessionObjectInputStream (ctx, new ByteArrayInputStream (state.getState ()));; 163 164 ctx.setInstance ( in.readObject () ); 165 166 in.close (); 167 168 170 171 ((SessionBean )ctx.getInstance()).ejbActivate(); 173 } 174 catch (ClassNotFoundException e) 175 { 176 throw new EJBException ("Could not activate", e); 177 } 178 catch (IOException e) 179 { 180 throw new EJBException ("Could not activate", e); 181 } 182 } 183 184 public void passivateSession (StatefulSessionEnterpriseContext ctx) 185 throws RemoteException 186 { 187 } 189 190 public void synchroSession (StatefulSessionEnterpriseContext ctx) throws RemoteException 191 { 192 try 193 { 194 ((SessionBean )ctx.getInstance()).ejbPassivate(); 196 197 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 199 ObjectOutputStream 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 )ctx.getInstance()).ejbActivate(); } 209 catch (IOException e) 210 { 211 throw new EJBException ("Could not passivate", e); 212 } 213 } 214 215 public void removeSession (StatefulSessionEnterpriseContext ctx) 216 throws RemoteException , RemoveException 217 { 218 try 219 { 220 ((SessionBean )ctx.getInstance()).ejbRemove(); 222 } 223 finally 224 { 225 this.sessionState.removeSession (this.appName, ctx.getId ()); 226 } 227 } 228 229 public void removePassivated (Object key) 230 { 231 this.sessionState.removeSession (this.appName, key); 232 } 233 234 public void sessionExternallyModified (PackagedSession session) 237 { 238 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 |