KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbeancluster > bean > SessionToEntityBean


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.test.testbeancluster.bean;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.rmi.dgc.VMID JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.ejb.CreateException JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.FinderException JavaDoc;
32
33 import org.jboss.logging.Logger;
34 import org.jboss.test.testbean.interfaces.AComplexPK;
35 import org.jboss.test.testbeancluster.interfaces.EntityPK;
36 import org.jboss.test.testbeancluster.interfaces.EntityPKHome;
37 import org.jboss.test.testbeancluster.interfaces.NodeAnswer;
38
39 /** A stateful session which access an entity bean used in testing the CIF.
40  * @author Scott.Stark@jboss.org
41  * @version $Revision: 58115 $
42  */

43 public class SessionToEntityBean implements SessionBean JavaDoc
44 {
45    private static Logger log = Logger.getLogger(SessionToEntityBean.class);
46    private static VMID JavaDoc nodeID = new VMID JavaDoc();
47
48    private int accessCount;
49    private AComplexPK theKey;
50
51    public void ejbCreate(AComplexPK key) throws CreateException JavaDoc
52    {
53       log.debug("ejbCreate(AComplexPK) called, nodeID="+nodeID);
54       this.theKey = key;
55    }
56    public void ejbActivate()
57    {
58       log.debug("ejbActivate() called, nodeID="+nodeID);
59    }
60    public void ejbPassivate()
61    {
62       log.debug("ejbPassivate() called, nodeID="+nodeID);
63    }
64    public void ejbRemove()
65    {
66       log.debug("ejbRemove() called, nodeID="+nodeID);
67       try
68       {
69          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
70          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
71          EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
72          home.remove(theKey);
73       }
74       catch(Exception JavaDoc e)
75       {
76          log.error("Failed to remove EntityPK", e);
77       }
78    }
79    public void setSessionContext(SessionContext JavaDoc context)
80    {
81    }
82
83    public String JavaDoc createEntity()
84       throws CreateException JavaDoc
85    {
86       String JavaDoc msg = null;
87       EntityPKHome home = null;
88       log.info("Enter createEntity, theKey="+theKey);
89       try
90       {
91          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
92          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
93          home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
94          EntityPK bean = home.findByPrimaryKey(theKey);
95          msg = "Found EntityPK, bean="+bean;
96          log.info(msg);
97       }
98       catch(FinderException JavaDoc e)
99       {
100          EntityPK bean = home.create(theKey.aBoolean, theKey.anInt, theKey.aLong,
101             theKey.aDouble, theKey.aString);
102          msg = "Created EntityPK, bean="+bean;
103          log.info(msg);
104       }
105       catch(Exception JavaDoc e)
106       {
107          log.error("Failed to create EntityPK", e);
108          throw new CreateException JavaDoc("Failed to create EntityPK: "+e.getMessage());
109       }
110       return msg;
111    }
112    public NodeAnswer accessEntity()
113    {
114       accessCount ++;
115       log.debug("Enter accessEntity(), accessCount="+accessCount);
116       int beanCount = 0;
117       try
118       {
119          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
120          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
121          EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
122          EntityPK bean = home.findByPrimaryKey(theKey);
123          bean.setOtherField(accessCount);
124          log.debug("Set EntityPK.OtherField to: "+accessCount);
125          beanCount = bean.getOtherField();
126       }
127       catch(Exception JavaDoc e)
128       {
129          log.debug("failed", e);
130       }
131       log.debug("Exit accessEntity()");
132       return new NodeAnswer(nodeID, new Integer JavaDoc(beanCount));
133    }
134
135    public int getAccessCount()
136    {
137       return accessCount;
138    }
139    public NodeAnswer validateAccessCount(int count)
140       throws RemoteException JavaDoc
141    {
142       if( accessCount != count )
143          throw new RemoteException JavaDoc("AccessCount: " + accessCount + " != " + count);
144       
145       int beanCount = 0;
146       try
147       {
148          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
149          Context JavaDoc enc = (Context JavaDoc) ctx.lookup("java:comp/env");
150          EntityPKHome home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
151          EntityPK bean = home.findByPrimaryKey(theKey);
152          beanCount = bean.getOtherField();
153          if( beanCount != count )
154             throw new RemoteException JavaDoc("BeanCount: " + beanCount + " != " + count);
155       }
156       catch(RemoteException JavaDoc e)
157       {
158          log.error("Failed to validate EntityPK", e);
159          throw e;
160       }
161       catch(Exception JavaDoc e)
162       {
163          log.error("Failed to validate EntityPK", e);
164          throw new RemoteException JavaDoc("Failed to validate EntityPK");
165       }
166       return new NodeAnswer(nodeID, new Integer JavaDoc(beanCount));
167    }
168 }
169
Popular Tags