KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > cache > nested > ParentStatefulBean


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7
8 package org.jboss.ejb3.test.cache.nested;
9
10 import org.jboss.logging.Logger;
11 import org.jboss.system.server.ServerConfig;
12
13 import javax.interceptor.Interceptors;
14 import javax.annotation.PostConstruct;
15 import javax.ejb.EJB JavaDoc;
16 import javax.ejb.Stateful JavaDoc;
17 import javax.ejb.Remote JavaDoc;
18 import javax.ejb.PostActivate JavaDoc;
19 import javax.ejb.PrePassivate JavaDoc;
20 import javax.ejb.Remove JavaDoc;
21 import javax.ejb.EJBException JavaDoc;
22 import java.rmi.dgc.VMID JavaDoc;
23
24 /**
25  * SFSB with nested bean
26  *
27  * @author Ben Wang
28  * @version $Revision: 46483 $
29  */

30 @Stateful JavaDoc(name="testParentStateful")
31 @org.jboss.annotation.ejb.cache.simple.CacheConfig(maxSize = 1000, idleTimeoutSeconds = 1)
32 @Remote JavaDoc(ParentStatefulRemote.class)
33 public class ParentStatefulBean implements java.io.Serializable JavaDoc, ParentStatefulRemote
34 {
35    private static Logger log = Logger.getLogger(ParentStatefulBean.class);
36    private int counter = 0;
37    private String JavaDoc state;
38    public transient VMID JavaDoc myId = null;
39    public String JavaDoc name;
40
41    @EJB JavaDoc
42    private NestedStateful nested;
43
44    public int increment()
45    {
46       counter = nested.increment();
47
48       log.debug("INCREMENT - parent counter: " + counter);
49       return counter;
50    }
51
52    public String JavaDoc getHostAddress()
53    {
54       return System.getProperty(ServerConfig.SERVER_BIND_ADDRESS);
55    }
56
57    public static int postActivateCalled = 0;
58    public static int prePassivateCalled = 0;
59
60    /**
61     * Sleep to test
62     * @throws Exception
63     */

64    public void longRunning() throws Exception JavaDoc
65    {
66       log.debug("+++ longRunning() enter ");
67       Thread.sleep(10000);
68       log.debug("+++ longRunning() leave ");
69    }
70
71    public int getPostActivate()
72    {
73       return ParentStatefulBean.postActivateCalled;
74    }
75
76    public int getPrePassivate()
77    {
78       return ParentStatefulBean.prePassivateCalled;
79    }
80
81    public int getNestedPostActivate()
82    {
83       return NestedStatefulBean.postActivateCalled;
84    }
85
86    public int getNestedPrePassivate()
87    {
88       return NestedStatefulBean.prePassivateCalled;
89    }
90
91    public void setState(String JavaDoc state)
92    {
93       this.state = state;
94    }
95
96    public String JavaDoc getState()
97    {
98       log.debug("getState(): entering ...");
99       return this.state;
100    }
101
102    public void reset()
103    {
104       state = null;
105       counter = 0;
106       ParentStatefulBean.postActivateCalled = 0;
107       ParentStatefulBean.prePassivateCalled = 0;
108    }
109
110    public void resetActivationCounter()
111    {
112       ParentStatefulBean.postActivateCalled = 0;
113       ParentStatefulBean.prePassivateCalled = 0;
114       NestedStatefulBean.postActivateCalled = 0;
115       NestedStatefulBean.prePassivateCalled = 0;
116    }
117
118    @PostActivate JavaDoc
119    public void postActivate()
120    {
121       ++ParentStatefulBean.postActivateCalled;
122       if (this.myId == null)
123       {
124          //it is a failover: we need to assign ourself an id
125
this.myId = new VMID JavaDoc();
126       }
127       log.debug("Activate. My ID: " + this.myId + " name: " + this.name);
128    }
129
130    @PrePassivate JavaDoc
131    public void prePassivate()
132    {
133       ++ParentStatefulBean.prePassivateCalled;
134       log.debug("Passivate. My ID: " + this.myId + " name: " + this.name);
135    }
136
137    @Remove JavaDoc
138    public void remove()
139    {
140    }
141
142    @PostConstruct
143    public void ejbCreate()
144    {
145       this.myId = new VMID JavaDoc();
146       log.debug("My ID: " + this.myId);
147    }
148
149    // Remote Interface implementation ----------------------------------------------
150

151    public void setName(String JavaDoc name)
152    {
153       this.name = name;
154       log.debug("Name set to " + name);
155    }
156
157    public void setNameOnlyOnNode(String JavaDoc name, VMID JavaDoc node)
158    {
159       if (node.equals(this.myId))
160          this.setName(name);
161       else
162          throw new EJBException JavaDoc("Trying to assign value on node " + this.myId + " but this node expected: " + node);
163    }
164
165    public void setUpFailover(String JavaDoc failover) {
166       // To setup the failover property
167
log.debug("Setting up failover property: " +failover);
168       System.setProperty ("JBossCluster-DoFail", failover);
169    }
170 }
171
Popular Tags