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