1 28 29 package com.caucho.ejb.session; 30 31 import com.caucho.ejb.AbstractContext; 32 import com.caucho.ejb.AbstractServer; 33 import com.caucho.ejb.EJBExceptionWrapper; 34 import com.caucho.ejb.EjbServerManager; 35 import com.caucho.ejb.protocol.AbstractHandle; 36 import com.caucho.naming.Jndi; 37 import com.caucho.util.Log; 38 39 import javax.ejb.EJBHome ; 40 import javax.ejb.EJBLocalObject ; 41 import javax.ejb.EJBObject ; 42 import javax.ejb.FinderException ; 43 import java.lang.reflect.Constructor ; 44 import java.util.logging.Level ; 45 import java.util.logging.Logger ; 46 47 50 public class StatelessServer extends AbstractServer { 51 protected static Logger log = Log.open(StatelessServer.class); 52 53 private AbstractStatelessContext _homeContext; 54 55 private EJBObject _remoteObject; 56 private EJBLocalObject _localObject; 57 58 65 public StatelessServer(EjbServerManager ejbManager) 66 { 67 super(ejbManager); 68 } 69 70 73 @Override 74 public void init() 75 throws Exception 76 { 77 Thread thread = Thread.currentThread(); 78 ClassLoader oldLoader = thread.getContextClassLoader(); 79 80 try { 81 thread.setContextClassLoader(_loader); 82 83 super.init(); 84 85 Jndi.rebindDeep("java:comp/env/ejb/sessionContext", 86 getStatelessContext()); 87 88 _localHome = getStatelessContext().createLocalHome(); 89 _remoteHomeView = getStatelessContext().createRemoteHomeView(); 90 91 try { 92 _localObject = getStatelessContext().getEJBLocalObject(); 93 } catch (Throwable e) { 94 } 95 96 try { 97 _remoteObject = getStatelessContext().getEJBObject(); 98 } catch (Throwable e) { 99 } 100 113 114 log.config("initialized session bean: " + this); 115 } finally { 116 thread.setContextClassLoader(oldLoader); 117 } 118 } 119 120 @Override 121 public boolean isLocal() 122 { 123 return super.isLocal() || _localObject != null; 124 } 125 126 @Override 127 public boolean isRemote() 128 { 129 return super.isRemote() || _remoteObject != null; 130 } 131 132 135 @Override 136 public EJBHome getEJBHome() 137 { 138 return _remoteHomeView; 139 } 140 141 144 @Override 145 public Object getRemoteObject() 146 { 147 Object home = getEJBHome(); 148 149 if (home != null) 150 return home; 151 152 if (_remoteObject != null) 153 return _remoteObject; 154 155 return null; 156 } 157 158 161 @Override 162 public Object getClientObject() 163 { 164 Object home = getClientLocalHome(); 165 166 if (home != null) 167 return home; 168 169 if (_localObject != null) 170 return _localObject; 171 172 return null; 173 } 174 175 182 @Override 183 public EJBObject getEJBObject(Object key) 184 throws FinderException 185 { 186 return getStatelessContext().getEJBObject(); 187 } 188 189 public AbstractContext getContext() 190 { 191 return getStatelessContext(); 192 } 193 194 @Override 195 public AbstractContext getContext(Object key, boolean forceLoad) 196 { 197 return getStatelessContext(); 198 } 199 200 private AbstractStatelessContext getStatelessContext() 201 { 202 synchronized (this) { 203 if (_homeContext == null) { 204 try { 205 Class []param = new Class [] { StatelessServer.class }; 206 Constructor cons = _contextImplClass.getConstructor(param); 207 208 _homeContext = (AbstractStatelessContext) cons.newInstance(this); 209 } catch (Exception e) { 210 throw new EJBExceptionWrapper(e); 211 } 212 } 213 } 214 215 return _homeContext; 216 } 217 218 221 AbstractHandle createHandle(AbstractContext context) 222 { 223 String key = createSessionKey(context); 224 225 return getHandleEncoder().createHandle(key); 226 } 227 228 231 244 245 248 String createSessionKey(AbstractContext context) 249 { 250 return "::ejb:stateless"; 251 } 252 253 256 @Override 257 public void destroy() 258 { 259 if (_homeContext != null) { 260 try { 261 _homeContext.destroy(); 262 } catch (Throwable e) { 263 log.log(Level.WARNING, e.toString(), e); 264 } 265 } 266 267 super.destroy(); 268 } 269 } 270 | Popular Tags |