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.testbean.bean;
023:
024: import java.rmi.*;
025: import javax.ejb.*;
026: import javax.naming.Context;
027: import javax.sql.DataSource;
028: import javax.naming.InitialContext;
029: import java.sql.Connection;
030: import org.jboss.test.testbean.interfaces.StatelessSession;
031: import org.jboss.test.testbean.interfaces.StatelessSessionHome;
032: import java.util.Collection;
033: import java.util.Collections;
034: import java.util.Vector;
035: import java.util.Enumeration;
036:
037: public class EntityBMPBean implements EntityBean {
038: org.apache.log4j.Category log = org.apache.log4j.Category
039: .getInstance(getClass());
040:
041: private EntityContext entityContext;
042: public String name;
043: private StatelessSession statelessSession;
044: private boolean wasFind = false;
045:
046: public String ejbCreate() throws RemoteException, CreateException {
047:
048: log.debug("EntityBMP.ejbCreate() called");
049: this .name = "noname";
050:
051: return name;
052:
053: }
054:
055: public String ejbCreate(String name) throws RemoteException,
056: CreateException {
057:
058: log.debug("EntityBMP.ejbCreate(" + name + ") called");
059: this .name = name;
060:
061: return name;
062: }
063:
064: public String ejbCreateMETHOD(String name) throws RemoteException,
065: CreateException {
066:
067: log.debug("EntityBMP.ejbCreateMETHOD(" + name + ") called");
068: this .name = name;
069:
070: return name;
071: }
072:
073: // For usage in BMP only (The return is the primary key type)
074:
075: public String ejbFindByPrimaryKey(String name)
076: throws RemoteException, FinderException {
077:
078: log.debug("EntityBMP.ejbFindByPrimaryKey() called");
079:
080: wasFind = true;
081:
082: return name;
083: }
084:
085: public Collection ejbFindCollectionKeys(int num)
086: throws RemoteException, FinderException {
087:
088: log.debug("EntityBMP.ejbFindMultipleKeys() called with num "
089: + num);
090: Collection pks = new Vector();
091: pks.add("primary key number 1");
092: pks.add("primary key number 2");
093: pks.add("primary key number 3");
094:
095: return pks;
096: }
097:
098: public Enumeration ejbFindEnumeratedKeys(int num)
099: throws RemoteException, FinderException {
100:
101: log.debug("EntityBMP.ejbFindEnumeratedKeys() called with num "
102: + num);
103: Collection pks = new Vector();
104: pks.add("primary key number 1");
105: pks.add("primary key number 2");
106: pks.add("primary key number 3");
107:
108: return Collections.enumeration(pks);
109: }
110:
111: public void ejbPostCreate() throws RemoteException, CreateException {
112:
113: log.debug("EntityBMP.ejbPostCreate() called");
114: }
115:
116: public void ejbPostCreate(String name) throws RemoteException,
117: CreateException {
118:
119: log.debug("EntityBMP.ejbPostCreate(" + name + ") called");
120: }
121:
122: public void ejbPostCreateMETHOD(String name)
123: throws RemoteException, CreateException {
124:
125: log.debug("EntityBMP.ejbPostCreateMETHOD(" + name + ") called");
126: }
127:
128: public void ejbActivate() throws RemoteException {
129: log.debug("EntityBMP.ejbActivate() called");
130: }
131:
132: public void ejbLoad() throws RemoteException {
133: log.debug("EntityBMP.ejbLoad() called");
134: }
135:
136: public void ejbPassivate() throws RemoteException {
137:
138: log.debug("Was Find " + wasFind);
139: log.debug("EntityBMP.ejbPassivate() called");
140: }
141:
142: public void ejbRemove() throws RemoteException, RemoveException {
143: log.debug("EntityBMP.ejbRemove() called");
144: }
145:
146: public void ejbStore() throws RemoteException {
147:
148: log.debug("EntityBMP.ejbStore() called");
149: }
150:
151: public String callBusinessMethodA() throws RemoteException {
152:
153: log.debug("EntityBMP.callBusinessMethodA() called");
154: return "EntityBMP.callBusinessMethodA() called, my primaryKey is "
155: + entityContext.getPrimaryKey().toString();
156: }
157:
158: public String callBusinessMethodB() throws RemoteException {
159:
160: log
161: .debug("EntityBMP.callBusinessMethodB() called, calling B2B");
162: String b2bMessage = statelessSession.callBusinessMethodB();
163: log.debug("EntityBMP called stateless Bean and it said "
164: + b2bMessage);
165: return "EntityBMP.callBusinessMethodB() called, called other bean (B2B) and it said \""
166: + b2bMessage + "\"";
167:
168: }
169:
170: public String callBusinessMethodB(String words)
171: throws RemoteException {
172: log
173: .debug("EntityBMP.callBusinessMethodB(String) called, calling B2B");
174: String b2bMessage = statelessSession.callBusinessMethodB();
175: log.debug("EntityBMP called stateless Bean and it said "
176: + b2bMessage);
177: return "EntityBMP.callBusinessMethodB() called, called other bean (B2B) and it said \""
178: + b2bMessage + "\"" + " words " + words;
179:
180: }
181:
182: public void setEntityContext(EntityContext context)
183: throws RemoteException {
184: log.debug("EntityBMP.setSessionContext() called");
185: entityContext = context;
186:
187: //we use the setEntityContext to lookup the connection and the EJBReference
188:
189: try {
190:
191: Context namingContext = new InitialContext();
192:
193: Connection connection = ((DataSource) namingContext
194: .lookup("java:comp/env/jdbc/myDatabase"))
195: .getConnection();
196: connection.close();
197:
198: log
199: .debug("EntityBMP I did get the connection to the database for BMP");
200: }
201:
202: catch (Exception e) {
203:
204: log.debug("failed", e);
205:
206: log
207: .debug("EntityBMP Could not find the database connection, check settings");
208:
209: throw new RemoteException(
210: "EntityBMP Could not find the database connection");
211: }
212:
213: try {
214:
215: Context namingContext = new InitialContext();
216:
217: log.debug("EntityBMP Looking up Stateless Home");
218:
219: StatelessSessionHome statelessHome = ((StatelessSessionHome) namingContext
220: .lookup("java:comp/env/ejb/myEJBRef"));
221:
222: log.debug("EntityBMP Calling create on Stateless Home");
223:
224: statelessSession = statelessHome.create();
225:
226: log
227: .debug("EntityBMP Found the EJB Reference in my java:comp/env/ environment");
228:
229: }
230:
231: catch (Exception e) {
232:
233: log.debug("failed", e);
234:
235: log
236: .debug("EntityBMP Could not find the EJB Reference called myEJBRef, check settings");
237:
238: throw new RemoteException(
239: "EntityBean Could not find EJB Reference");
240: }
241: }
242:
243: public void unsetEntityContext() throws RemoteException {
244:
245: log.debug("EntityBMP.unsetSessionContext() called");
246: entityContext = null;
247: }
248: }
|