KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 /**
51  * Comment
52  *
53  * @author <a HREF="mailto:bdecoste@jboss.com">William DeCoste</a>
54  * @version $Revision: 57744 $
55  */

56 @Stateful(name="StatefulBean")
57 @Remote JavaDoc(org.jboss.ejb3.test.stateful.Stateful.class)
58 @Local JavaDoc(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 JavaDoc sessionContext;
71    
72    @Resource(mappedName="java:/DefaultDS")
73    private transient javax.sql.DataSource JavaDoc datasource;
74    
75    @Resource(mappedName="java:/ConnectionFactory")
76    public transient javax.jms.QueueConnectionFactory JavaDoc connectionFactory;
77    
78    private StatefulLocal localStateful;
79
80    private String JavaDoc state;
81    private int stuff;
82    private boolean wasPassivated = false;
83    
84    public void lookupStateful() throws Exception JavaDoc
85    {
86       localStateful = (StatefulLocal)new InitialContext JavaDoc().lookup("StatefulBean/local");
87    }
88    
89    public void testStateful() throws Exception JavaDoc
90    {
91       localStateful.getState();
92    }
93
94    @Interceptors(MyInterceptor.class)
95    public String JavaDoc getInterceptorState()
96    {
97       throw new RuntimeException JavaDoc("NOT REACHABLE");
98    }
99
100    @Interceptors(MyInterceptor.class)
101    public void setInterceptorState(String JavaDoc param)
102    {
103       throw new RuntimeException JavaDoc("NOT REACHABLE");
104    }
105    
106    public boolean testSessionContext()
107    {
108       return sessionContext.isCallerInRole("role");
109    }
110    
111    public void testResources() throws Exception JavaDoc
112    {
113       datasource.toString();
114       connectionFactory.toString();
115       
116       javax.sql.DataSource JavaDoc ds = (javax.sql.DataSource JavaDoc)new InitialContext JavaDoc().lookup(Container.ENC_CTX_NAME + "/env/jdbc/ds");
117       ds.toString();
118    }
119
120    public String JavaDoc getState() throws Exception JavaDoc
121    {
122       Thread.sleep(1000);
123       return state;
124    }
125
126    public void setState(String JavaDoc state) throws Exception JavaDoc
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 JavaDoc
138    {
139       throw new Exception JavaDoc();
140    }
141
142    public void testExceptionCause() throws Exception JavaDoc
143    {
144       Object JavaDoc o = null;
145       o.toString();
146    }
147
148    @PrePassivate JavaDoc
149    public void passivate()
150    {
151       log.info("************ passivating");
152       wasPassivated = true;
153    }
154    
155    @PostActivate JavaDoc
156    public void activate()
157    {
158       log.info("************ activating");
159    }
160
161    public void testSerializedState(String JavaDoc state)
162    {
163       this.state = state;
164
165       StatefulBean bean = null;
166       try
167       {
168          ObjectOutputStream JavaDoc out;
169
170          ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
171
172          out = new JBossObjectOutputStream(baos, false);
173          out.writeObject(this);
174          out.flush();
175
176          ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(baos.toByteArray());
177
178          JBossObjectInputStream is = new JBossObjectInputStream(bais);
179          bean = (StatefulBean)is.readObject();
180       }
181       catch (IOException JavaDoc e)
182       {
183          throw new RuntimeException JavaDoc(e);
184       }
185       catch (ClassNotFoundException JavaDoc e)
186       {
187          throw new RuntimeException JavaDoc(e);
188       }
189
190       if (!state.equals(bean.state)) throw new RuntimeException JavaDoc("failed to serialize: " + bean.state);
191    }
192
193    public boolean wasPassivated()
194    {
195       return wasPassivated;
196    }
197
198    @Init JavaDoc
199    public void ejbCreate(Integer JavaDoc state)
200    {
201       this.state=state.toString();
202    }
203
204    @Init JavaDoc
205    public void ejbCreate(State state)
206    {
207       this.state=state.getState();
208    }
209
210    @Init JavaDoc
211    public void ejbCreate(String JavaDoc state)
212    {
213       this.state=state;
214    }
215    
216    public void removeBean()
217    {
218       
219    }
220 }
221
Popular Tags