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.cmp2.optimisticlock.bug1006723.testsession;
023:
024: import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityALocal;
025: import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityBLocal;
026: import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityBLocalHome;
027: import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityALocalHome;
028:
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import javax.ejb.CreateException;
032: import javax.ejb.EJBException;
033: import javax.naming.Context;
034: import javax.naming.InitialContext;
035: import javax.naming.NamingException;
036: import java.util.Iterator;
037: import java.util.Date;
038:
039: public class TestSessionBean implements SessionBean {
040:
041: public Long setup() throws Exception {
042: try {
043: /* Add an entity A and then add some entity Bs to it. */
044: EntityALocal entityA = getEntityALocalHome().create(
045: new Long(1));
046: entityA.addEntityB(getEntityBLocalHome()
047: .create(new Long(2)));
048: return entityA.getOID();
049: } catch (Exception e) {
050: sessionContext.setRollbackOnly();
051: throw e;
052: }
053: }
054:
055: public void test(Long entityAOID) throws Exception {
056: try {
057: EntityALocal entityA = getEntityALocalHome()
058: .findByPrimaryKey(entityAOID);
059: Iterator entityBs = entityA.listEntityBs().iterator();
060: while (entityBs.hasNext()) {
061: ((EntityBLocal) entityBs.next())
062: .setLastModified(new Date());
063: }
064: } catch (Exception e) {
065: sessionContext.setRollbackOnly();
066: throw e;
067: }
068: }
069:
070: public void ejbCreate() throws CreateException {
071: }
072:
073: public void ejbRemove() {
074: }
075:
076: public void ejbActivate() {
077: }
078:
079: public void ejbPassivate() {
080: }
081:
082: public void setSessionContext(SessionContext context) {
083: this .sessionContext = context;
084: }
085:
086: private Context getContext() {
087: if (context == null) {
088: try {
089: context = new InitialContext();
090: } catch (NamingException e) {
091: throw new EJBException(e.getMessage());
092: }
093: }
094: return context;
095: }
096:
097: private EntityALocalHome getEntityALocalHome() {
098: if (entityALocalHome == null) {
099: try {
100: entityALocalHome = (EntityALocalHome) getContext()
101: .lookup("java:comp/env/ejb/EntityA");
102: } catch (NamingException e) {
103: throw new EJBException(e.getMessage());
104: }
105: }
106: return entityALocalHome;
107: }
108:
109: private EntityBLocalHome getEntityBLocalHome() {
110: if (entityBLocalHome == null) {
111: try {
112: entityBLocalHome = (EntityBLocalHome) getContext()
113: .lookup("java:comp/env/ejb/EntityB");
114: } catch (NamingException e) {
115: throw new EJBException(e.getMessage());
116: }
117: }
118: return entityBLocalHome;
119: }
120:
121: private SessionContext sessionContext;
122: private Context context;
123: private EntityALocalHome entityALocalHome;
124: private EntityBLocalHome entityBLocalHome;
125:
126: }
|