KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > testbean > bean > EntityBMPBean


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.testbean.bean;
23
24
25 import java.rmi.*;
26 import javax.ejb.*;
27 import javax.naming.Context JavaDoc;
28 import javax.sql.DataSource JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import org.jboss.test.testbean.interfaces.StatelessSession;
32 import org.jboss.test.testbean.interfaces.StatelessSessionHome;
33 import java.util.Collection JavaDoc;
34 import java.util.Collections JavaDoc;
35 import java.util.Vector JavaDoc;
36 import java.util.Enumeration JavaDoc;
37
38 public class EntityBMPBean implements EntityBean {
39    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
40    
41   private EntityContext entityContext;
42   public String JavaDoc name;
43   private StatelessSession statelessSession;
44   private boolean wasFind = false;
45
46
47   public String JavaDoc ejbCreate() throws RemoteException, CreateException {
48       
49        log.debug("EntityBMP.ejbCreate() called");
50        this.name = "noname";
51
52        return name;
53   
54       
55   }
56   public String JavaDoc ejbCreate(String JavaDoc name) throws RemoteException, CreateException {
57
58        log.debug("EntityBMP.ejbCreate("+name+") called");
59        this.name = name;
60
61        return name;
62   }
63
64   public String JavaDoc ejbCreateMETHOD(String JavaDoc name) throws RemoteException, CreateException {
65
66        log.debug("EntityBMP.ejbCreateMETHOD("+name+") called");
67        this.name = name;
68
69        return name;
70   }
71
72   // For usage in BMP only (The return is the primary key type)
73

74   public String JavaDoc ejbFindByPrimaryKey(String JavaDoc name) throws RemoteException, FinderException {
75
76       log.debug("EntityBMP.ejbFindByPrimaryKey() called");
77
78       wasFind = true;
79
80       return name;
81   }
82
83   public Collection JavaDoc ejbFindCollectionKeys(int num) throws RemoteException, FinderException {
84
85       log.debug("EntityBMP.ejbFindMultipleKeys() called with num "+num);
86       Collection JavaDoc pks = new Vector JavaDoc();
87       pks.add("primary key number 1");
88       pks.add("primary key number 2");
89       pks.add("primary key number 3");
90
91       return pks;
92   }
93   public Enumeration JavaDoc ejbFindEnumeratedKeys(int num) throws RemoteException, FinderException {
94
95       log.debug("EntityBMP.ejbFindEnumeratedKeys() called with num "+num);
96       Collection JavaDoc pks = new Vector JavaDoc();
97       pks.add("primary key number 1");
98       pks.add("primary key number 2");
99       pks.add("primary key number 3");
100
101       return Collections.enumeration(pks);
102   }
103
104   public void ejbPostCreate() throws RemoteException, CreateException {
105
106        log.debug("EntityBMP.ejbPostCreate() called");
107   }
108   
109   public void ejbPostCreate(String JavaDoc name) throws RemoteException, CreateException {
110
111        log.debug("EntityBMP.ejbPostCreate("+name+") called");
112   }
113   
114   public void ejbPostCreateMETHOD(String JavaDoc name) throws RemoteException, CreateException {
115
116        log.debug("EntityBMP.ejbPostCreateMETHOD("+name+") called");
117   }
118
119   public void ejbActivate() throws RemoteException {
120     log.debug("EntityBMP.ejbActivate() called");
121   }
122
123   public void ejbLoad() throws RemoteException {
124    log.debug("EntityBMP.ejbLoad() called");
125   }
126
127   public void ejbPassivate() throws RemoteException {
128
129      log.debug("Was Find "+wasFind);
130      log.debug("EntityBMP.ejbPassivate() called");
131   }
132
133   public void ejbRemove() throws RemoteException, RemoveException {
134    log.debug("EntityBMP.ejbRemove() called");
135   }
136
137   public void ejbStore() throws RemoteException {
138      
139    log.debug("EntityBMP.ejbStore() called");
140   }
141
142   public String JavaDoc callBusinessMethodA() throws RemoteException{
143
144      log.debug("EntityBMP.callBusinessMethodA() called");
145      return "EntityBMP.callBusinessMethodA() called, my primaryKey is "+
146             entityContext.getPrimaryKey().toString();
147   }
148
149    public String JavaDoc callBusinessMethodB() throws RemoteException {
150
151      log.debug("EntityBMP.callBusinessMethodB() called, calling B2B");
152      String JavaDoc b2bMessage = statelessSession.callBusinessMethodB();
153      log.debug("EntityBMP called stateless Bean and it said "+b2bMessage);
154      return "EntityBMP.callBusinessMethodB() called, called other bean (B2B) and it said \""+b2bMessage+"\"";
155
156   }
157   
158    public String JavaDoc callBusinessMethodB(String JavaDoc words) throws RemoteException {
159        log.debug("EntityBMP.callBusinessMethodB(String) called, calling B2B");
160      String JavaDoc b2bMessage = statelessSession.callBusinessMethodB();
161      log.debug("EntityBMP called stateless Bean and it said "+b2bMessage);
162      return "EntityBMP.callBusinessMethodB() called, called other bean (B2B) and it said \""+b2bMessage+"\""+" words "+words;
163
164   }
165
166   public void setEntityContext(EntityContext context) throws RemoteException {
167      log.debug("EntityBMP.setSessionContext() called");
168      entityContext = context;
169
170      //we use the setEntityContext to lookup the connection and the EJBReference
171

172         try {
173
174             Context JavaDoc namingContext = new InitialContext JavaDoc();
175
176             Connection JavaDoc connection = ((DataSource JavaDoc) namingContext.lookup("java:comp/env/jdbc/myDatabase")).getConnection();
177             connection.close();
178
179             log.debug("EntityBMP I did get the connection to the database for BMP");
180         }
181
182         catch (Exception JavaDoc e) {
183
184             log.debug("failed", e);
185
186             log.debug("EntityBMP Could not find the database connection, check settings");
187
188             throw new RemoteException("EntityBMP Could not find the database connection");
189         }
190
191         try {
192
193             Context JavaDoc namingContext = new InitialContext JavaDoc();
194
195             log.debug("EntityBMP Looking up Stateless Home");
196
197             StatelessSessionHome statelessHome = ((StatelessSessionHome) namingContext.lookup("java:comp/env/ejb/myEJBRef"));
198
199             log.debug("EntityBMP Calling create on Stateless Home");
200             
201             statelessSession = statelessHome.create();
202
203             log.debug("EntityBMP Found the EJB Reference in my java:comp/env/ environment");
204
205
206         }
207
208         catch (Exception JavaDoc e) {
209
210             log.debug("failed", e);
211
212             log.debug("EntityBMP Could not find the EJB Reference called myEJBRef, check settings");
213
214             throw new RemoteException("EntityBean Could not find EJB Reference");
215         }
216     }
217
218     public void unsetEntityContext() throws RemoteException {
219
220         log.debug("EntityBMP.unsetSessionContext() called");
221         entityContext = null;
222     }
223 }
224
Popular Tags