KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > perf > ejb > SessionBean


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.perf.ejb;
23 import java.rmi.RemoteException JavaDoc;
24 import java.rmi.ServerException JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import javax.ejb.CreateException JavaDoc;
28 import javax.ejb.FinderException JavaDoc;
29 import javax.ejb.RemoveException JavaDoc;
30 import javax.ejb.SessionContext JavaDoc;
31 import javax.naming.Context JavaDoc;
32 import javax.naming.InitialContext JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.rmi.PortableRemoteObject JavaDoc;
35
36 import org.jboss.logging.Logger;
37
38 import org.jboss.test.perf.interfaces.EntityLocalHome;
39 import org.jboss.test.perf.interfaces.EntityLocal;
40 import org.jboss.test.perf.interfaces.EntityPK;
41
42 public class SessionBean implements javax.ejb.SessionBean JavaDoc
43 {
44    private static Logger log = Logger.getLogger(SessionBean.class);
45    private EntityLocalHome entityHome;
46
47    public void setSessionContext(SessionContext JavaDoc context)
48    {
49    }
50
51    public void ejbCreate(String JavaDoc entityName) throws CreateException JavaDoc
52    {
53       try
54       {
55          Context JavaDoc context = new InitialContext JavaDoc();
56          Object JavaDoc ref = context.lookup("java:comp/env/"+entityName);
57          entityHome = (EntityLocalHome) PortableRemoteObject.narrow(ref, EntityLocalHome.class);
58       }
59       catch(NamingException JavaDoc e)
60       {
61          throw new CreateException JavaDoc("Cound not resolve name: " + e);
62       }
63    }
64
65    private EntityLocal findByPrimaryKey(int key) throws FinderException JavaDoc
66    {
67       EntityPK primaryKey = new EntityPK(key);
68       return entityHome.findByPrimaryKey(primaryKey);
69    }
70
71    private Collection JavaDoc findInRange(int min, int max) throws FinderException JavaDoc
72    {
73       return entityHome.findInRange(min, max);
74    }
75
76    public void create(int low, int high)
77       throws CreateException JavaDoc
78    {
79       for(int i = low; i < high; i++)
80       {
81          entityHome.create(i, 0);
82       }
83    }
84    
85    public void remove(int low, int high)
86       throws RemoveException JavaDoc
87    {
88       if(low + 1 == high)
89       {
90          try
91          {
92             EntityLocal entity = findByPrimaryKey(low);
93             entity.remove();
94          }
95          catch(FinderException JavaDoc e)
96          {
97             log.error("Failed to find and remove entity", e);
98             throw new RemoveException JavaDoc("Failed to find and remove entity");
99          }
100       }
101       else
102       {
103          //There is no find in range finder! till someone implements it...
104
//java.util.Enumeration elements = findInRange(low, high);
105
int count = 0;
106          for (int i = low; i < high; i++)
107          {
108             try
109             {
110                EntityLocal entity = findByPrimaryKey(i);
111                entity.remove();
112                count++;
113             }
114             catch (Exception JavaDoc e)
115             {
116                //ignore
117
} // end of try-catch
118
} // end of for ()
119

120          if( count != (high-low) )
121          {
122             throw new RemoveException JavaDoc("Removed "+count+" but should remove:"+(high-low));
123          }
124       }
125    }
126
127    public void read(int id) throws RemoteException JavaDoc
128    {
129       try
130       {
131          EntityLocal entity = findByPrimaryKey(id);
132          entity.read();
133       }
134       catch(FinderException JavaDoc e)
135       {
136          throw new ServerException JavaDoc("findByPrimaryKey failed for id="+id, e);
137       }
138    }
139
140    public void read(int low, int high) throws RemoteException JavaDoc
141    {
142       Collection JavaDoc elements = null;
143       try
144       {
145          elements = findInRange(low, high);
146       }
147       catch(FinderException JavaDoc e)
148       {
149          throw new ServerException JavaDoc("findInRange failed for low="+low+", high="+high, e);
150       }
151
152       Iterator JavaDoc iter = elements.iterator();
153       while( iter.hasNext() )
154       {
155          EntityLocal entity = (EntityLocal) iter.next();
156          entity.read();
157       }
158    }
159
160    public void write(int id) throws RemoteException JavaDoc
161    {
162       try
163       {
164          EntityLocal entity = findByPrimaryKey(id);
165          int value = entity.read();
166          entity.write(value + 1);
167       }
168       catch(FinderException JavaDoc e)
169       {
170          throw new ServerException JavaDoc("findByPrimaryKey failed for id="+id, e);
171       }
172    }
173
174    public void write(int low, int high) throws RemoteException JavaDoc
175    {
176       Collection JavaDoc elements = null;
177       try
178       {
179          elements = findInRange(low, high);
180       }
181       catch(FinderException JavaDoc e)
182       {
183          throw new ServerException JavaDoc("findInRange failed for low="+low+", high="+high, e);
184       }
185
186       Iterator JavaDoc iter = elements.iterator();
187       while( iter.hasNext() )
188       {
189          EntityLocal entity = (EntityLocal) iter.next();
190          int value = entity.read();
191          entity.write(value + 1);
192       }
193    }
194
195    public void ejbRemove()
196    {
197    }
198    
199    public void ejbActivate()
200    {
201    }
202    
203    public void ejbPassivate()
204    {
205    }
206    
207 }
208
209
Popular Tags