KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > optimisticlock > bug1006723 > testsession > TestSessionBean


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.cmp2.optimisticlock.bug1006723.testsession;
23
24
25 import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityALocal;
26 import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityBLocal;
27 import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityBLocalHome;
28 import org.jboss.test.cmp2.optimisticlock.bug1006723.testentity.EntityALocalHome;
29
30 import javax.ejb.SessionBean JavaDoc;
31 import javax.ejb.SessionContext JavaDoc;
32 import javax.ejb.CreateException JavaDoc;
33 import javax.ejb.EJBException JavaDoc;
34 import javax.naming.Context JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.NamingException JavaDoc;
37 import java.util.Iterator JavaDoc;
38 import java.util.Date JavaDoc;
39
40 public class TestSessionBean implements SessionBean JavaDoc{
41
42     public Long JavaDoc setup() throws Exception JavaDoc{
43         try{
44             /* Add an entity A and then add some entity Bs to it. */
45             EntityALocal entityA = getEntityALocalHome().create(new Long JavaDoc(1));
46             entityA.addEntityB(getEntityBLocalHome().create(new Long JavaDoc(2)));
47             return entityA.getOID();
48         }
49         catch (Exception JavaDoc e){
50             sessionContext.setRollbackOnly();
51             throw e;
52         }
53     }
54
55     public void test(Long JavaDoc entityAOID) throws Exception JavaDoc{
56         try{
57             EntityALocal entityA = getEntityALocalHome().findByPrimaryKey
58                 (entityAOID);
59             Iterator JavaDoc entityBs = entityA.listEntityBs().iterator();
60             while (entityBs.hasNext()){
61                 ((EntityBLocal)entityBs.next()).setLastModified(new Date JavaDoc());
62             }
63         }
64         catch (Exception JavaDoc e){
65             sessionContext.setRollbackOnly();
66             throw e;
67         }
68     }
69
70     public void ejbCreate() throws CreateException JavaDoc{}
71
72     public void ejbRemove(){}
73
74     public void ejbActivate(){}
75
76     public void ejbPassivate(){}
77
78     public void setSessionContext(SessionContext JavaDoc context){
79         this.sessionContext = context;
80     }
81
82     private Context JavaDoc getContext(){
83         if (context == null){
84             try{
85                 context = new InitialContext JavaDoc();
86             }
87             catch (NamingException JavaDoc e){
88                 throw new EJBException JavaDoc(e.getMessage());
89             }
90         }
91         return context;
92     }
93
94     private EntityALocalHome getEntityALocalHome(){
95         if (entityALocalHome == null){
96             try{
97                 entityALocalHome = (EntityALocalHome)getContext().lookup
98                     ("java:comp/env/ejb/EntityA");
99             }
100             catch (NamingException JavaDoc e){
101                 throw new EJBException JavaDoc(e.getMessage());
102             }
103         }
104         return entityALocalHome;
105     }
106
107     private EntityBLocalHome getEntityBLocalHome(){
108         if (entityBLocalHome == null){
109             try{
110                 entityBLocalHome = (EntityBLocalHome)getContext().lookup
111                     ("java:comp/env/ejb/EntityB");
112             }
113             catch (NamingException JavaDoc e){
114                 throw new EJBException JavaDoc(e.getMessage());
115             }
116         }
117         return entityBLocalHome;
118     }
119
120     private SessionContext JavaDoc sessionContext;
121     private Context JavaDoc context;
122     private EntityALocalHome entityALocalHome;
123     private EntityBLocalHome entityBLocalHome;
124
125 }
126
127
Popular Tags