KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > entity > ejb > EntitySessionBean


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.entity.ejb;
23
24 import java.rmi.RemoteException JavaDoc;
25
26 import javax.ejb.CreateException JavaDoc;
27 import javax.ejb.EJBException JavaDoc;
28 import javax.ejb.SessionBean JavaDoc;
29 import javax.ejb.SessionContext JavaDoc;
30 import javax.naming.InitialContext JavaDoc;
31
32 import org.jboss.test.entity.interfaces.EntitySession;
33 import org.jboss.test.entity.interfaces.EntitySessionHome;
34 import org.jboss.test.entity.interfaces.Pathological;
35 import org.jboss.test.entity.interfaces.PathologicalEntity;
36 import org.jboss.test.entity.interfaces.PathologicalEntityHome;
37
38 /**
39  * Session facade for entity testing.
40  *
41  * @author Adrian.Brock@HappeningTimes.com
42  * @version $Revision: 37406 $
43  */

44 public class EntitySessionBean
45    implements SessionBean JavaDoc
46 {
47    private transient SessionContext JavaDoc ctx;
48
49    public void createPathological(String JavaDoc name, boolean pathological)
50    {
51       Pathological.setPathological(pathological);
52       try
53       {
54          PathologicalEntityHome home = getPathologicalEJB();
55          home.create(name);
56       }
57       catch (Throwable JavaDoc e)
58       {
59          check(e);
60       }
61       finally
62       {
63          Pathological.setPathological(false);
64       }
65    }
66
67    public void removeHomePathological(String JavaDoc name, boolean pathological)
68    {
69       Pathological.setPathological(pathological);
70       try
71       {
72          PathologicalEntityHome home = getPathologicalEJB();
73          home.remove(name);
74       }
75       catch (Throwable JavaDoc e)
76       {
77          check(e);
78       }
79       finally
80       {
81          Pathological.setPathological(false);
82       }
83    }
84
85    public void removePathological(String JavaDoc name, boolean pathological)
86    {
87       try
88       {
89          PathologicalEntityHome home = getPathologicalEJB();
90          PathologicalEntity bean = home.findByPrimaryKey(name);
91          Pathological.setPathological(pathological);
92          bean.remove();
93       }
94       catch (Throwable JavaDoc e)
95       {
96          check(e);
97       }
98       finally
99       {
100          Pathological.setPathological(false);
101       }
102    }
103
104    public void findPathological(String JavaDoc name, boolean pathological)
105    {
106       Pathological.setPathological(pathological);
107       try
108       {
109          PathologicalEntityHome home = getPathologicalEJB();
110          home.findByPrimaryKey(name);
111       }
112       catch (Throwable JavaDoc e)
113       {
114          check(e);
115       }
116       finally
117       {
118          Pathological.setPathological(false);
119       }
120    }
121
122    public void getPathological(String JavaDoc name, boolean pathological)
123    {
124       try
125       {
126          PathologicalEntityHome home = getPathologicalEJB();
127          PathologicalEntity bean = home.findByPrimaryKey(name);
128          Pathological.setPathological(pathological);
129          bean.getSomething();
130       }
131       catch (Throwable JavaDoc e)
132       {
133          check(e);
134       }
135       finally
136       {
137          Pathological.setPathological(false);
138       }
139    }
140
141    public void setPathological(String JavaDoc name, boolean pathological)
142    {
143       try
144       {
145          PathologicalEntityHome home = getPathologicalEJB();
146          PathologicalEntity bean = home.findByPrimaryKey(name);
147          Pathological.setPathological(pathological);
148          bean.setSomething("something");
149       }
150       catch (Throwable JavaDoc e)
151       {
152          check(e);
153       }
154       finally
155       {
156          Pathological.setPathological(false);
157       }
158    }
159
160    public void ejbCreate()
161       throws CreateException JavaDoc
162    {
163    }
164    
165    public void setSessionContext(SessionContext JavaDoc ctx)
166    {
167       this.ctx = ctx;
168    }
169     
170    public void ejbActivate()
171    {
172    }
173     
174    public void ejbPassivate()
175    {
176    }
177     
178    public void ejbRemove()
179    {
180    }
181
182    private void check(Throwable JavaDoc e)
183    {
184       while (true)
185       {
186          if (e instanceof EJBException JavaDoc)
187             e = ((EJBException JavaDoc) e).getCausedByException();
188          else if (e instanceof RemoteException JavaDoc)
189             e = ((RemoteException JavaDoc) e).detail;
190          else if (e instanceof IllegalStateException JavaDoc)
191             throw (IllegalStateException JavaDoc) e;
192          else
193             return;
194       }
195    }
196
197    private PathologicalEntityHome getPathologicalEJB()
198       throws Exception JavaDoc
199    {
200       return (PathologicalEntityHome) new InitialContext JavaDoc().lookup("java:comp/env/ejb/PathologicalEJB");
201    }
202 }
203
Popular Tags