1 29 30 package com.caucho.ejb.session; 31 32 import com.caucho.ejb.AbstractContext; 33 import com.caucho.ejb.AbstractServer; 34 import com.caucho.ejb.EJBExceptionWrapper; 35 import com.caucho.ejb.EjbServerManager; 36 import com.caucho.ejb.protocol.AbstractHandle; 37 import com.caucho.ejb.protocol.JVMObject; 38 import com.caucho.naming.Jndi; 39 import com.caucho.util.Log; 40 import com.caucho.util.LruCache; 41 42 import javax.ejb.EJBHome ; 43 import javax.ejb.EJBLocalHome ; 44 import javax.ejb.FinderException ; 45 import javax.ejb.SessionBean ; 46 import java.lang.reflect.Constructor ; 47 import java.rmi.RemoteException ; 48 import java.util.ArrayList ; 49 import java.util.Iterator ; 50 import java.util.logging.Level ; 51 import java.util.logging.Logger ; 52 53 56 public class SessionServer extends AbstractServer { 57 protected final static Logger log = Log.open(SessionServer.class); 58 59 private StatefulContext _homeContext; 60 61 private LruCache<Object ,AbstractSessionContext> _sessions 62 = new LruCache<Object ,AbstractSessionContext>(1024); 63 64 private boolean _isClosed; 65 66 public SessionServer(EjbServerManager manager) 67 { 68 super(manager); 69 } 70 71 74 @Override 75 public void init() 76 throws Exception 77 { 78 Thread thread = Thread.currentThread(); 79 ClassLoader oldLoader = thread.getContextClassLoader(); 80 81 try { 82 thread.setContextClassLoader(_loader); 83 84 super.init(); 85 86 String prefix = getServerManager().getLocalJndiPrefix(); 88 if (prefix != null) 89 Jndi.rebindDeep(prefix + "/sessionContext", getSessionContext()); 90 91 prefix = getServerManager().getRemoteJndiPrefix(); 92 if (prefix != null) 93 Jndi.rebindDeep(prefix + "/sessionContext", getSessionContext()); 94 95 _localHome = getSessionContext().createLocalHome(); 96 _remoteHomeView = getSessionContext().createRemoteHomeView(); 97 98 log.config("initialized session bean: " + this); 99 } finally { 100 thread.setContextClassLoader(oldLoader); 101 } 102 } 103 104 107 @Override 108 public Class getPrimaryKeyClass() 109 { 110 return null; 111 } 112 113 116 @Override 117 public EJBLocalHome getEJBLocalHome() 118 { 119 return _localHome; 120 } 121 122 125 @Override 126 public EJBHome getEJBHome() 127 throws RemoteException 128 { 129 return _remoteHomeView; 130 } 131 132 135 @Override 136 public Object getHomeObject() 137 { 138 return _remoteHomeView; 139 } 140 141 144 @Override 145 public Object getRemoteObject() 146 { 147 if (_remoteHomeView != null) 148 return _remoteHomeView; 149 else 150 return _homeContext._caucho_newRemoteInstance(); 151 } 152 153 156 @Override 157 public Object getClientObject() 158 { 159 return new StatefulJndiFactory(this); 160 } 161 162 165 Object newInstance() 166 { 167 return _homeContext._caucho_newInstance(); 168 } 169 170 173 SessionObject getEJBLocalObject(SessionBean bean) 174 { 175 try { 176 SessionObject obj = null; 177 178 182 if (obj == null) 183 throw new IllegalStateException ("bean has no local interface"); 184 185 return obj; 186 } catch (Exception e) { 187 throw new EJBExceptionWrapper(e); 188 } 189 } 190 191 194 JVMObject createEJBObject(Object primaryKey) 195 { 196 try { 197 JVMObject obj = (JVMObject) _remoteStubClass.newInstance(); 198 obj._init(this, primaryKey); 199 200 return obj; 201 } catch (Exception e) { 202 throw new EJBExceptionWrapper(e); 203 } 204 } 205 206 209 AbstractHandle createHandle(AbstractContext context) 210 { 211 String key = ((AbstractSessionContext) context).getPrimaryKey(); 212 213 return getHandleEncoder().createHandle(key); 214 } 215 216 219 public String createSessionKey(AbstractSessionContext context) 220 { 221 String key = getHandleEncoder().createRandomStringKey(); 222 223 _sessions.put(key, context); 224 225 return key; 226 } 227 228 235 @Override 236 public AbstractContext getContext(Object key, boolean forceLoad) 237 throws FinderException 238 { 239 if (key == null) 240 return null; 241 242 AbstractSessionContext cxt = _sessions.get(key); 243 if (cxt == null) 244 throw new FinderException ("no matching object:" + key); 245 246 return cxt; 247 } 248 249 private AbstractSessionContext getSessionContext() 250 { 251 synchronized (this) { 252 if (_homeContext == null) { 253 try { 254 Class []param = new Class [] { SessionServer.class }; 255 Constructor cons = _contextImplClass.getConstructor(param); 256 257 _homeContext = (StatefulContext) cons.newInstance(this); 258 } catch (Exception e) { 259 throw new EJBExceptionWrapper(e); 260 } 261 } 262 } 263 264 return _homeContext; 265 } 266 267 public void addSession(AbstractSessionContext context) 268 { 269 createSessionKey(context); 270 } 271 272 275 @Override 276 public void remove(AbstractHandle handle) 277 { 278 _sessions.remove(handle.getObjectId()); 279 } 281 282 285 @Override 286 public void destroy() 287 { 288 synchronized (this) { 289 if (_isClosed) 290 return; 291 292 _isClosed = true; 293 } 294 295 ArrayList <AbstractSessionContext> values; 296 values = new ArrayList <AbstractSessionContext>(); 297 298 Iterator <AbstractSessionContext> iter = _sessions.values(); 299 while (iter.hasNext()) { 300 values.add(iter.next()); 301 } 302 303 _sessions = null; 304 305 log.fine("closing session server " + this); 306 307 for (AbstractSessionContext cxt : values) { 308 try { 309 cxt.destroy(); 310 } catch (Throwable e) { 311 log.log(Level.WARNING, e.toString(), e); 312 } 313 } 314 315 super.destroy(); 316 } 317 } 318 | Popular Tags |