001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.testbeancluster.bean;
023:
024: import java.rmi.RemoteException;
025: import java.rmi.dgc.VMID;
026: import javax.naming.InitialContext;
027: import javax.naming.Context;
028: import javax.ejb.CreateException;
029: import javax.ejb.SessionContext;
030: import javax.ejb.SessionBean;
031: import javax.ejb.FinderException;
032:
033: import org.apache.log4j.Logger;
034: import org.jboss.test.testbean.interfaces.AComplexPK;
035: import org.jboss.test.testbeancluster.interfaces.EntityPK;
036: import org.jboss.test.testbeancluster.interfaces.EntityPKHome;
037: import org.jboss.test.testbeancluster.interfaces.NodeAnswer;
038:
039: /** A stateful session which access an entity bean used in testing the CIF.
040: * @author Scott.Stark@jboss.org
041: * @version $Revision: 57211 $
042: */
043: public class SessionToEntityBean implements SessionBean {
044: private static Logger log = Logger
045: .getLogger(SessionToEntityBean.class);
046: private static VMID nodeID = new VMID();
047:
048: private int accessCount;
049: private AComplexPK theKey;
050:
051: public void ejbCreate(AComplexPK key) throws CreateException {
052: log.debug("ejbCreate(AComplexPK) called, nodeID=" + nodeID);
053: this .theKey = key;
054: }
055:
056: public void ejbActivate() {
057: log.debug("ejbActivate() called, nodeID=" + nodeID);
058: }
059:
060: public void ejbPassivate() {
061: log.debug("ejbPassivate() called, nodeID=" + nodeID);
062: }
063:
064: public void ejbRemove() {
065: log.debug("ejbRemove() called, nodeID=" + nodeID);
066: try {
067: InitialContext ctx = new InitialContext();
068: Context enc = (Context) ctx.lookup("java:comp/env");
069: EntityPKHome home = (EntityPKHome) enc
070: .lookup("ejb/EntityPKHome");
071: home.remove(theKey);
072: } catch (Exception e) {
073: log.error("Failed to remove EntityPK", e);
074: }
075: }
076:
077: public void setSessionContext(SessionContext context) {
078: }
079:
080: public String createEntity() throws CreateException {
081: String msg = null;
082: EntityPKHome home = null;
083: log.info("Enter createEntity, theKey=" + theKey);
084: try {
085: InitialContext ctx = new InitialContext();
086: Context enc = (Context) ctx.lookup("java:comp/env");
087: home = (EntityPKHome) enc.lookup("ejb/EntityPKHome");
088: EntityPK bean = home.findByPrimaryKey(theKey);
089: msg = "Found EntityPK, bean=" + bean;
090: log.info(msg);
091: } catch (FinderException e) {
092: EntityPK bean = home.create(theKey.aBoolean, theKey.anInt,
093: theKey.aLong, theKey.aDouble, theKey.aString);
094: msg = "Created EntityPK, bean=" + bean;
095: log.info(msg);
096: } catch (Exception e) {
097: log.error("Failed to create EntityPK", e);
098: throw new CreateException("Failed to create EntityPK: "
099: + e.getMessage());
100: }
101: return msg;
102: }
103:
104: public NodeAnswer accessEntity() {
105: accessCount++;
106: log.debug("Enter accessEntity(), accessCount=" + accessCount);
107: int beanCount = 0;
108: try {
109: InitialContext ctx = new InitialContext();
110: Context enc = (Context) ctx.lookup("java:comp/env");
111: EntityPKHome home = (EntityPKHome) enc
112: .lookup("ejb/EntityPKHome");
113: EntityPK bean = home.findByPrimaryKey(theKey);
114: bean.setOtherField(accessCount);
115: log.debug("Set EntityPK.OtherField to: " + accessCount);
116: beanCount = bean.getOtherField();
117: } catch (Exception e) {
118: log.debug("failed", e);
119: }
120: log.debug("Exit accessEntity()");
121: return new NodeAnswer(nodeID, new Integer(beanCount));
122: }
123:
124: public int getAccessCount() {
125: return accessCount;
126: }
127:
128: public NodeAnswer validateAccessCount(int count)
129: throws RemoteException {
130: if (accessCount != count)
131: throw new RemoteException("AccessCount: " + accessCount
132: + " != " + count);
133:
134: int beanCount = 0;
135: try {
136: InitialContext ctx = new InitialContext();
137: Context enc = (Context) ctx.lookup("java:comp/env");
138: EntityPKHome home = (EntityPKHome) enc
139: .lookup("ejb/EntityPKHome");
140: EntityPK bean = home.findByPrimaryKey(theKey);
141: beanCount = bean.getOtherField();
142: if (beanCount != count)
143: throw new RemoteException("BeanCount: " + beanCount
144: + " != " + count);
145: } catch (RemoteException e) {
146: log.error("Failed to validate EntityPK", e);
147: throw e;
148: } catch (Exception e) {
149: log.error("Failed to validate EntityPK", e);
150: throw new RemoteException("Failed to validate EntityPK");
151: }
152: return new NodeAnswer(nodeID, new Integer(beanCount));
153: }
154: }
|