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.readahead.ejb;
023:
024: import java.util.Iterator;
025: import java.util.Collection;
026: import javax.naming.InitialContext;
027: import javax.naming.NamingException;
028: import javax.ejb.EJBException;
029: import javax.ejb.SessionBean;
030: import javax.ejb.SessionContext;
031: import org.jboss.test.readahead.interfaces.AddressHome;
032: import org.jboss.test.readahead.interfaces.AddressRemote;
033: import org.jboss.test.readahead.interfaces.CMPFindTestEntityHome;
034: import org.jboss.test.readahead.interfaces.CMPFindTestEntityRemote;
035:
036: /**
037: * Implementation class for session bean used in read-ahead finder
038: * tests
039: *
040: * @author <a href="mailto:danch@nvisia.com">danch (Dan Christopherson</a>
041: * @version $Id: CMPFindTestSession.java 57211 2006-09-26 12:39:46Z dimitris@jboss.org $
042: *
043: * Revision:
044: */
045: public class CMPFindTestSession implements SessionBean {
046: org.apache.log4j.Category log = org.apache.log4j.Category
047: .getInstance(getClass());
048:
049: private static final int DATASET_SIZE = 100;
050:
051: private SessionContext sessionContext;
052:
053: public void ejbCreate() {
054: }
055:
056: public void ejbRemove() {
057: }
058:
059: public void ejbActivate() {
060: }
061:
062: public void ejbPassivate() {
063: }
064:
065: public void setSessionContext(SessionContext context) {
066: sessionContext = context;
067: }
068:
069: public void removeTestData() {
070: try {
071: InitialContext ctx = new InitialContext();
072: CMPFindTestEntityHome home = (CMPFindTestEntityHome) ctx
073: .lookup("CMPFindTestEntity");
074:
075: Collection coll = home.findAll();
076: Iterator iter = coll.iterator();
077: while (iter.hasNext()) {
078: CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote) iter
079: .next();
080:
081: rem.remove();
082: }
083: } catch (Exception e) {
084: }
085: }
086:
087: public void createTestData() {
088: try {
089: InitialContext ctx = new InitialContext();
090: CMPFindTestEntityHome home = (CMPFindTestEntityHome) ctx
091: .lookup("CMPFindTestEntity");
092: AddressHome addrHome = (AddressHome) ctx.lookup("Address");
093: for (int i = 0; i < DATASET_SIZE; i++) {
094: String key = Long.toString(System.currentTimeMillis())
095: + "-" + i;
096: CMPFindTestEntityRemote rem = home.create(key);
097: rem.setName("Name");
098: rem.setRank("Rank");
099: rem.setSerialNumber("123456789");
100: //give him an address
101: if ((i % 2) == 0) {
102: addrHome.create(rem.getKey(), "1", "123 east st.",
103: "Eau Claire", "WI", "54701");
104: } else {
105: addrHome.create(rem.getKey(), "1", "123 east st.",
106: "Milwaukee", "WI", "54201");
107: }
108: }
109: } catch (Exception e) {
110: log.debug("Exception caught: " + e);
111: log.debug("failed", e);
112: throw new EJBException(e.getMessage());
113: }
114: }
115:
116: public void addressByCity() {
117: try {
118: InitialContext ctx = new InitialContext();
119: AddressHome home = (AddressHome) ctx.lookup("Address");
120:
121: long startTime = System.currentTimeMillis();
122: Collection coll = home.findByCity("Eau Claire");
123: int count = 0;
124: Iterator iter = coll.iterator();
125: while (iter.hasNext()) {
126: AddressRemote rem = (AddressRemote) iter.next();
127: rem.getCity();
128: // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
129: count++;
130: }
131: long endTime = System.currentTimeMillis();
132: log.debug("called " + count + " beans in "
133: + (endTime - startTime) + " ms.");
134: } catch (Exception e) {
135: log.debug("Caught " + e);
136: }
137: }
138:
139: public void testByCity() {
140: try {
141: InitialContext ctx = new InitialContext();
142: CMPFindTestEntityHome home = (CMPFindTestEntityHome) ctx
143: .lookup("CMPFindTestEntity");
144:
145: long startTime = System.currentTimeMillis();
146: Collection coll = home.findByCity("Eau Claire");
147: int count = 0;
148: Iterator iter = coll.iterator();
149: while (iter.hasNext()) {
150: CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote) iter
151: .next();
152: rem.getName();
153: // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
154: count++;
155: }
156: long endTime = System.currentTimeMillis();
157: log.debug("called " + count + " beans in "
158: + (endTime - startTime) + " ms.");
159: } catch (Exception e) {
160: }
161: }
162:
163: public void testFinder() {
164: try {
165: InitialContext ctx = new InitialContext();
166: CMPFindTestEntityHome home = (CMPFindTestEntityHome) ctx
167: .lookup("CMPFindTestEntity");
168:
169: long startTime = System.currentTimeMillis();
170: Collection coll = home.findAll();
171: int count = 0;
172: Iterator iter = coll.iterator();
173: while (iter.hasNext()) {
174: CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote) iter
175: .next();
176: rem.getName();
177: // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
178: count++;
179: }
180: long endTime = System.currentTimeMillis();
181: log.debug("called " + count + " beans in "
182: + (endTime - startTime) + " ms.");
183: } catch (Exception e) {
184: }
185: }
186:
187: public void testUpdates() {
188: try {
189: InitialContext ctx = new InitialContext();
190: CMPFindTestEntityHome home = (CMPFindTestEntityHome) ctx
191: .lookup("CMPFindTestEntity");
192:
193: long startTime = System.currentTimeMillis();
194: Collection coll = home.findAll();
195: int count = 0;
196: Iterator iter = coll.iterator();
197: while (iter.hasNext()) {
198: CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote) iter
199: .next();
200: rem.getName();
201: rem.setRank("Very");
202: count++;
203: }
204: long endTime = System.currentTimeMillis();
205: log.debug("called " + count + " beans in "
206: + (endTime - startTime) + " ms.");
207: } catch (Exception e) {
208: }
209: }
210: }
|