1 22 package org.jboss.ejb3.test.stateful; 23 24 import java.io.ObjectOutputStream ; 25 import java.io.ByteArrayOutputStream ; 26 import java.io.ByteArrayInputStream ; 27 import java.io.IOException ; 28 29 import javax.annotation.Resource; 30 import javax.annotation.Resources; 31 import javax.ejb.Init ; 32 import javax.ejb.Local ; 33 import javax.ejb.Remote ; 34 import javax.ejb.SessionContext ; 35 import javax.ejb.Stateful ; 36 import javax.ejb.PrePassivate ; 37 import javax.ejb.PostActivate ; 38 import javax.interceptor.Interceptors; 39 import javax.naming.InitialContext ; 40 41 import org.jboss.ejb3.Container; 42 43 import org.jboss.annotation.ejb.RemoteBinding; 44 import org.jboss.annotation.ejb.cache.simple.CacheConfig; 45 import org.jboss.annotation.security.SecurityDomain; 46 import org.jboss.logging.Logger; 47 import org.jboss.serial.io.JBossObjectOutputStream; 48 import org.jboss.serial.io.JBossObjectInputStream; 49 50 56 @Stateful(name="StatefulBean") 57 @Remote (org.jboss.ejb3.test.stateful.Stateful.class) 58 @Local (org.jboss.ejb3.test.stateful.StatefulLocal.class) 59 @RemoteBinding(jndiBinding = "Stateful", 60 interceptorStack="RemoteBindingStatefulSessionClientInterceptors", 61 factory = org.jboss.ejb3.test.stateful.StatefulRemoteProxyFactory.class) 62 @CacheConfig(maxSize = 1000, idleTimeoutSeconds = 1) 63 @SecurityDomain("test") 64 @Resources({@Resource(name="jdbc/ds", mappedName="java:/DefaultDS")}) 65 public class StatefulBean implements org.jboss.ejb3.test.stateful.Stateful 66 { 67 private static final Logger log = Logger.getLogger(StatefulBean.class); 68 69 @Resource 70 private SessionContext sessionContext; 71 72 @Resource(mappedName="java:/DefaultDS") 73 private transient javax.sql.DataSource datasource; 74 75 @Resource(mappedName="java:/ConnectionFactory") 76 public transient javax.jms.QueueConnectionFactory connectionFactory; 77 78 private StatefulLocal localStateful; 79 80 private String state; 81 private int stuff; 82 private boolean wasPassivated = false; 83 84 public void lookupStateful() throws Exception 85 { 86 localStateful = (StatefulLocal)new InitialContext ().lookup("StatefulBean/local"); 87 } 88 89 public void testStateful() throws Exception 90 { 91 localStateful.getState(); 92 } 93 94 @Interceptors(MyInterceptor.class) 95 public String getInterceptorState() 96 { 97 throw new RuntimeException ("NOT REACHABLE"); 98 } 99 100 @Interceptors(MyInterceptor.class) 101 public void setInterceptorState(String param) 102 { 103 throw new RuntimeException ("NOT REACHABLE"); 104 } 105 106 public boolean testSessionContext() 107 { 108 return sessionContext.isCallerInRole("role"); 109 } 110 111 public void testResources() throws Exception 112 { 113 datasource.toString(); 114 connectionFactory.toString(); 115 116 javax.sql.DataSource ds = (javax.sql.DataSource )new InitialContext ().lookup(Container.ENC_CTX_NAME + "/env/jdbc/ds"); 117 ds.toString(); 118 } 119 120 public String getState() throws Exception 121 { 122 Thread.sleep(1000); 123 return state; 124 } 125 126 public void setState(String state) throws Exception 127 { 128 Thread.sleep(1000); 129 this.state = state; 130 } 131 132 public boolean interceptorAccessed() 133 { 134 return RemoteBindingInterceptor.accessed; 135 } 136 137 public void testThrownException() throws Exception 138 { 139 throw new Exception (); 140 } 141 142 public void testExceptionCause() throws Exception 143 { 144 Object o = null; 145 o.toString(); 146 } 147 148 @PrePassivate 149 public void passivate() 150 { 151 log.info("************ passivating"); 152 wasPassivated = true; 153 } 154 155 @PostActivate 156 public void activate() 157 { 158 log.info("************ activating"); 159 } 160 161 public void testSerializedState(String state) 162 { 163 this.state = state; 164 165 StatefulBean bean = null; 166 try 167 { 168 ObjectOutputStream out; 169 170 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 171 172 out = new JBossObjectOutputStream(baos, false); 173 out.writeObject(this); 174 out.flush(); 175 176 ByteArrayInputStream bais = new ByteArrayInputStream (baos.toByteArray()); 177 178 JBossObjectInputStream is = new JBossObjectInputStream(bais); 179 bean = (StatefulBean)is.readObject(); 180 } 181 catch (IOException e) 182 { 183 throw new RuntimeException (e); 184 } 185 catch (ClassNotFoundException e) 186 { 187 throw new RuntimeException (e); 188 } 189 190 if (!state.equals(bean.state)) throw new RuntimeException ("failed to serialize: " + bean.state); 191 } 192 193 public boolean wasPassivated() 194 { 195 return wasPassivated; 196 } 197 198 @Init 199 public void ejbCreate(Integer state) 200 { 201 this.state=state.toString(); 202 } 203 204 @Init 205 public void ejbCreate(State state) 206 { 207 this.state=state.getState(); 208 } 209 210 @Init 211 public void ejbCreate(String state) 212 { 213 this.state=state; 214 } 215 216 public void removeBean() 217 { 218 219 } 220 } 221 | Popular Tags |