KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jca > ejb > ReentrantBean


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.jca.ejb;
23
24 import java.rmi.RemoteException JavaDoc;
25 import java.sql.Connection JavaDoc;
26
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.EntityBean JavaDoc;
30 import javax.ejb.EntityContext JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.sql.DataSource JavaDoc;
33
34 import org.jboss.test.jca.interfaces.Reentrant;
35
36 /**
37  * ReentrantBean.java tests if CachedConnectionManager works with reentrant ejbs.
38  *
39  * @author <a HREF="mailto:d_jencks@users.sourceforge.net">David Jencks</a>
40  * @version <tt>$Revision: 37406 $</tt>
41  *
42  * @ejb.bean
43  * jndi-name="ejb/jca/Reentrant"
44  * name="Reentrant"
45  * type="BMP"
46  * view-type="remote"
47  * reentrant="true"
48  * @ejb.pk class="java.lang.Integer"
49  * @ejb.transaction
50  * type="Required"
51  *
52  */

53
54 public class ReentrantBean implements EntityBean JavaDoc
55 {
56    /** The serialVersionUID */
57    private static final long serialVersionUID = 1L;
58
59    private Integer JavaDoc id;
60
61    private EntityContext JavaDoc ctx;
62
63    public ReentrantBean()
64    {
65
66    }
67
68    /**
69     * Creates a new <code>ejbCreate</code> instance.
70     *
71     * @param id an <code>Integer</code> value
72     * @param other a <code>Reentrant</code> value
73     * @exception CreateException if an error occurs
74     * @exception RemoteException if an error occurs
75     *
76     * @ejb.create-method
77     */

78    public Integer JavaDoc ejbCreate(Integer JavaDoc id, Reentrant other) throws CreateException JavaDoc, RemoteException JavaDoc
79    {
80       this.id = id;
81       return id;
82    }
83
84    /**
85     * Creates a new <code>ejbPostCreate</code> instance.
86     *
87     * @param id an <code>Integer</code> value
88     * @param other a <code>Reentrant</code> value
89     * @exception CreateException if an error occurs
90     * @exception RemoteException if an error occurs
91     */

92    public void ejbPostCreate(Integer JavaDoc id, Reentrant other) throws CreateException JavaDoc, RemoteException JavaDoc
93    {
94       this.id = id;
95       Reentrant me = (Reentrant) ctx.getEJBObject();
96       Connection JavaDoc c = null;
97       try
98       {
99          try
100          {
101             DataSource JavaDoc ds = (DataSource JavaDoc) new InitialContext JavaDoc().lookup("java:/DefaultDS");
102             c = ds.getConnection();
103             if (other != null)
104             {
105                other.doSomething(me);
106             }
107          }
108          finally
109          {
110             c.close();
111          }
112       }
113       catch (Exception JavaDoc e)
114       {
115          throw new CreateException JavaDoc("could not get DataSource or Connection" + e.getMessage());
116       }
117    }
118
119    /**
120     * Describe <code>doSomething</code> method here.
121     *
122     * @param first a <code>Reentrant</code> value
123     * @exception RemoteException if an error occurs
124     *
125     * @ejb.interface-method
126     */

127    public void doSomething(Reentrant first) throws RemoteException JavaDoc
128    {
129       if (first != null)
130       {
131          first.doSomething(null);
132       }
133    }
134
135    public Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc id)
136    {
137       return id;
138    }
139
140    // implementation of javax.ejb.EntityBean interface
141

142    public void ejbActivate()
143    {
144    }
145
146    public void ejbLoad()
147    {
148       this.id = (Integer JavaDoc) ctx.getPrimaryKey();
149    }
150
151    public void ejbPassivate()
152    {
153    }
154
155    public void ejbRemove() throws EJBException JavaDoc
156    {
157    }
158
159    public void ejbStore() throws EJBException JavaDoc
160    {
161    }
162
163    public void setEntityContext(EntityContext JavaDoc ctx)
164    {
165       this.ctx = ctx;
166    }
167
168    public void unsetEntityContext()
169    {
170       ctx = null;
171    }
172
173    public String JavaDoc toString()
174    {
175       if (id == null)
176          return null;
177       else
178          return id.toString();
179    }
180 }
181
Popular Tags