KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > stateful > ClusteredStatefulBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.stateful;
23
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.io.ByteArrayOutputStream JavaDoc;
26 import java.io.ByteArrayInputStream JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 import javax.annotation.Resource;
30 import javax.annotation.Resources;
31 import javax.ejb.Init JavaDoc;
32 import javax.ejb.Local JavaDoc;
33 import javax.ejb.Remote JavaDoc;
34 import javax.ejb.SessionContext JavaDoc;
35 import javax.ejb.Stateful JavaDoc;
36 import javax.ejb.PrePassivate JavaDoc;
37 import javax.ejb.PostActivate JavaDoc;
38 import javax.interceptor.Interceptors;
39 import javax.naming.InitialContext JavaDoc;
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 /**
52  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
53  */

54 @Stateful(name="ClusteredStatefulBean")
55 @Remote JavaDoc(org.jboss.ejb3.test.stateful.ClusteredStateful.class)
56 @Local JavaDoc(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 JavaDoc sessionContext;
70    
71 // @Resource(mappedName="java:/DefaultDS")
72
// private transient javax.sql.DataSource datasource;
73

74 // @Resource(mappedName="java:/ConnectionFactory")
75
// public transient javax.jms.QueueConnectionFactory connectionFactory;
76

77    private String JavaDoc state;
78    private int stuff;
79    private boolean wasPassivated = false;
80
81    @Interceptors(MyInterceptor.class)
82    public String JavaDoc getInterceptorState()
83    {
84       throw new RuntimeException JavaDoc("NOT REACHABLE");
85    }
86
87    @Interceptors(MyInterceptor.class)
88    public void setInterceptorState(String JavaDoc param)
89    {
90       throw new RuntimeException JavaDoc("NOT REACHABLE");
91    }
92    
93    public boolean testSessionContext()
94    {
95       return sessionContext.isCallerInRole("role");
96    }
97    
98    public void testResources() throws Exception JavaDoc
99    {
100 // datasource.toString();
101
// connectionFactory.toString();
102

103       javax.sql.DataSource JavaDoc ds = (javax.sql.DataSource JavaDoc)new InitialContext JavaDoc().lookup(Container.ENC_CTX_NAME + "/env/jdbc/ds");
104       ds.toString();
105    }
106
107    public String JavaDoc getState() throws Exception JavaDoc
108    {
109       Thread.sleep(1000);
110       return state;
111    }
112
113    public void setState(String JavaDoc state) throws Exception JavaDoc
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 JavaDoc
126    {
127       throw new Exception JavaDoc();
128    }
129
130    public void testExceptionCause() throws Exception JavaDoc
131    {
132       Object JavaDoc o = null;
133       o.toString();
134    }
135
136    @PrePassivate JavaDoc
137    public void passivate()
138    {
139       log.info("************ passivating " + this);
140       wasPassivated = true;
141    }
142    
143    @PostActivate JavaDoc
144    public void activate()
145    {
146       log.info("************ activating");
147    }
148
149    public void testSerializedState(String JavaDoc state)
150    {
151       this.state = state;
152
153       ClusteredStatefulBean bean = null;
154       try
155       {
156          ObjectOutputStream JavaDoc out;
157
158          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
159
160          out = new JBossObjectOutputStream(baos, false);
161          out.writeObject(this);
162          out.flush();
163
164          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
165
166          JBossObjectInputStream is = new JBossObjectInputStream(bais);
167          bean = (ClusteredStatefulBean)is.readObject();
168       }
169       catch (IOException JavaDoc e)
170       {
171          throw new RuntimeException JavaDoc(e);
172       }
173       catch (ClassNotFoundException JavaDoc e)
174       {
175          throw new RuntimeException JavaDoc(e);
176       }
177
178       if (!state.equals(bean.state)) throw new RuntimeException JavaDoc("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 JavaDoc
188    public void ejbCreate(Integer JavaDoc state)
189    {
190       this.state=state.toString();
191    }
192
193    @Init JavaDoc
194    public void ejbCreate(State state)
195    {
196       this.state=state.getState();
197    }
198
199    @Init JavaDoc
200    public void ejbCreate(String JavaDoc state)
201    {
202       this.state=state;
203    }
204    
205    public void removeBean()
206    {
207       
208    }
209 }
210
Popular Tags