KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > readahead > ejb > CMPFindTestSession


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.readahead.ejb;
23
24 import java.util.Iterator JavaDoc;
25 import java.util.Collection JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import javax.naming.NamingException JavaDoc;
28 import javax.ejb.EJBException JavaDoc;
29 import javax.ejb.SessionBean JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import org.jboss.test.readahead.interfaces.AddressHome;
32 import org.jboss.test.readahead.interfaces.AddressRemote;
33 import org.jboss.test.readahead.interfaces.CMPFindTestEntityHome;
34 import org.jboss.test.readahead.interfaces.CMPFindTestEntityRemote;
35
36 /**
37  * Implementation class for session bean used in read-ahead finder
38  * tests
39  *
40  * @author <a HREF="mailto:danch@nvisia.com">danch (Dan Christopherson</a>
41  * @version $Id: CMPFindTestSession.java 58115 2006-11-04 08:42:14Z scott.stark@jboss.org $
42  *
43  * Revision:
44  */

45 public class CMPFindTestSession implements SessionBean JavaDoc {
46    org.jboss.logging.Logger log = org.jboss.logging.Logger.getLogger(getClass());
47    
48    private static final int DATASET_SIZE = 100;
49    
50    private SessionContext JavaDoc sessionContext;
51    
52    public void ejbCreate() {
53    }
54    public void ejbRemove() {
55    }
56    public void ejbActivate() {
57    }
58    public void ejbPassivate() {
59    }
60    public void setSessionContext(SessionContext JavaDoc context) {
61       sessionContext = context;
62    }
63    
64    public void removeTestData() {
65       try {
66          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
67          CMPFindTestEntityHome home = (CMPFindTestEntityHome)ctx.lookup("CMPFindTestEntity");
68          
69          Collection JavaDoc coll = home.findAll();
70          Iterator JavaDoc iter = coll.iterator();
71          while (iter.hasNext()) {
72             CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote)iter.next();
73             
74             rem.remove();
75          }
76       } catch (Exception JavaDoc e) {
77       }
78    }
79    
80    public void createTestData() {
81       try {
82          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
83          CMPFindTestEntityHome home = (CMPFindTestEntityHome)ctx.lookup("CMPFindTestEntity");
84          AddressHome addrHome = (AddressHome)ctx.lookup("Address");
85          for (int i=0;i<DATASET_SIZE;i++) {
86             String JavaDoc key = Long.toString(System.currentTimeMillis())+"-"+i;
87             CMPFindTestEntityRemote rem = home.create(key);
88             rem.setName("Name");
89             rem.setRank("Rank");
90             rem.setSerialNumber("123456789");
91             //give him an address
92
if ((i % 2) ==0) {
93                addrHome.create(rem.getKey(), "1", "123 east st.", "Eau Claire", "WI", "54701");
94             } else {
95                addrHome.create(rem.getKey(), "1", "123 east st.", "Milwaukee", "WI", "54201");
96             }
97          }
98       } catch (Exception JavaDoc e) {
99          log.debug("Exception caught: "+e);
100          log.debug("failed", e);
101          throw new EJBException JavaDoc(e.getMessage());
102       }
103    }
104    
105    public void addressByCity() {
106       try {
107          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
108          AddressHome home = (AddressHome)ctx.lookup("Address");
109          
110          long startTime = System.currentTimeMillis();
111          Collection JavaDoc coll = home.findByCity("Eau Claire");
112          int count = 0;
113          Iterator JavaDoc iter = coll.iterator();
114          while (iter.hasNext()) {
115             AddressRemote rem = (AddressRemote)iter.next();
116             rem.getCity();
117 // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
118
count++;
119          }
120          long endTime = System.currentTimeMillis();
121          log.debug("called "+count+" beans in "+(endTime-startTime)+" ms.");
122       } catch (Exception JavaDoc e) {
123          log.debug("Caught "+e);
124       }
125    }
126    
127    public void testByCity() {
128       try {
129          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
130          CMPFindTestEntityHome home = (CMPFindTestEntityHome)ctx.lookup("CMPFindTestEntity");
131          
132          long startTime = System.currentTimeMillis();
133          Collection JavaDoc coll = home.findByCity("Eau Claire");
134          int count = 0;
135          Iterator JavaDoc iter = coll.iterator();
136          while (iter.hasNext()) {
137             CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote)iter.next();
138             rem.getName();
139 // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
140
count++;
141          }
142          long endTime = System.currentTimeMillis();
143          log.debug("called "+count+" beans in "+(endTime-startTime)+" ms.");
144       } catch (Exception JavaDoc e) {
145       }
146    }
147    public void testFinder() {
148       try {
149          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
150          CMPFindTestEntityHome home = (CMPFindTestEntityHome)ctx.lookup("CMPFindTestEntity");
151          
152          long startTime = System.currentTimeMillis();
153          Collection JavaDoc coll = home.findAll();
154          int count = 0;
155          Iterator JavaDoc iter = coll.iterator();
156          while (iter.hasNext()) {
157             CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote)iter.next();
158             rem.getName();
159 // log.debug("Name: "+rem.getName()+" Rank: "+rem.getRank());
160
count++;
161          }
162          long endTime = System.currentTimeMillis();
163          log.debug("called "+count+" beans in "+(endTime-startTime)+" ms.");
164       } catch (Exception JavaDoc e) {
165       }
166    }
167    
168    public void testUpdates() {
169       try {
170          InitialContext JavaDoc ctx = new InitialContext JavaDoc();
171          CMPFindTestEntityHome home = (CMPFindTestEntityHome)ctx.lookup("CMPFindTestEntity");
172          
173          long startTime = System.currentTimeMillis();
174          Collection JavaDoc coll = home.findAll();
175          int count = 0;
176          Iterator JavaDoc iter = coll.iterator();
177          while (iter.hasNext()) {
178             CMPFindTestEntityRemote rem = (CMPFindTestEntityRemote)iter.next();
179             rem.getName();
180             rem.setRank("Very");
181             count++;
182          }
183          long endTime = System.currentTimeMillis();
184          log.debug("called "+count+" beans in "+(endTime-startTime)+" ms.");
185       } catch (Exception JavaDoc e) {
186       }
187    }
188 }
189
Popular Tags