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.perf.ejb;
023:
024: // SessionBean.java
025:
026: import java.rmi.RemoteException;
027: import java.rmi.ServerException;
028: import java.util.Collection;
029: import java.util.Iterator;
030: import javax.ejb.CreateException;
031: import javax.ejb.FinderException;
032: import javax.ejb.RemoveException;
033: import javax.ejb.SessionContext;
034: import javax.naming.Context;
035: import javax.naming.InitialContext;
036: import javax.naming.NamingException;
037: import javax.rmi.PortableRemoteObject;
038:
039: import org.apache.log4j.Logger;
040:
041: import org.jboss.test.perf.interfaces.EntityLocalHome;
042: import org.jboss.test.perf.interfaces.EntityLocal;
043: import org.jboss.test.perf.interfaces.EntityPK;
044:
045: public class SessionBean implements javax.ejb.SessionBean {
046: private static Logger log = Logger.getLogger(SessionBean.class);
047: private EntityLocalHome entityHome;
048:
049: public void setSessionContext(SessionContext context) {
050: }
051:
052: public void ejbCreate(String entityName) throws CreateException {
053: try {
054: Context context = new InitialContext();
055: Object ref = context.lookup("java:comp/env/" + entityName);
056: entityHome = (EntityLocalHome) PortableRemoteObject.narrow(
057: ref, EntityLocalHome.class);
058: } catch (NamingException e) {
059: throw new CreateException("Cound not resolve name: " + e);
060: }
061: }
062:
063: private EntityLocal findByPrimaryKey(int key)
064: throws FinderException {
065: EntityPK primaryKey = new EntityPK(key);
066: return entityHome.findByPrimaryKey(primaryKey);
067: }
068:
069: private Collection findInRange(int min, int max)
070: throws FinderException {
071: return entityHome.findInRange(min, max);
072: }
073:
074: public void create(int low, int high) throws CreateException {
075: for (int i = low; i < high; i++) {
076: entityHome.create(i, 0);
077: }
078: }
079:
080: public void remove(int low, int high) throws RemoveException {
081: if (low + 1 == high) {
082: try {
083: EntityLocal entity = findByPrimaryKey(low);
084: entity.remove();
085: } catch (FinderException e) {
086: log.error("Failed to find and remove entity", e);
087: throw new RemoveException(
088: "Failed to find and remove entity");
089: }
090: } else {
091: //There is no find in range finder! till someone implements it...
092: //java.util.Enumeration elements = findInRange(low, high);
093: int count = 0;
094: for (int i = low; i < high; i++) {
095: try {
096: EntityLocal entity = findByPrimaryKey(i);
097: entity.remove();
098: count++;
099: } catch (Exception e) {
100: //ignore
101: } // end of try-catch
102: } // end of for ()
103:
104: if (count != (high - low)) {
105: throw new RemoveException("Removed " + count
106: + " but should remove:" + (high - low));
107: }
108: }
109: }
110:
111: public void read(int id) throws RemoteException {
112: try {
113: EntityLocal entity = findByPrimaryKey(id);
114: entity.read();
115: } catch (FinderException e) {
116: throw new ServerException("findByPrimaryKey failed for id="
117: + id, e);
118: }
119: }
120:
121: public void read(int low, int high) throws RemoteException {
122: Collection elements = null;
123: try {
124: elements = findInRange(low, high);
125: } catch (FinderException e) {
126: throw new ServerException("findInRange failed for low="
127: + low + ", high=" + high, e);
128: }
129:
130: Iterator iter = elements.iterator();
131: while (iter.hasNext()) {
132: EntityLocal entity = (EntityLocal) iter.next();
133: entity.read();
134: }
135: }
136:
137: public void write(int id) throws RemoteException {
138: try {
139: EntityLocal entity = findByPrimaryKey(id);
140: int value = entity.read();
141: entity.write(value + 1);
142: } catch (FinderException e) {
143: throw new ServerException("findByPrimaryKey failed for id="
144: + id, e);
145: }
146: }
147:
148: public void write(int low, int high) throws RemoteException {
149: Collection elements = null;
150: try {
151: elements = findInRange(low, high);
152: } catch (FinderException e) {
153: throw new ServerException("findInRange failed for low="
154: + low + ", high=" + high, e);
155: }
156:
157: Iterator iter = elements.iterator();
158: while (iter.hasNext()) {
159: EntityLocal entity = (EntityLocal) iter.next();
160: int value = entity.read();
161: entity.write(value + 1);
162: }
163: }
164:
165: public void ejbRemove() {
166: }
167:
168: public void ejbActivate() {
169: }
170:
171: public void ejbPassivate() {
172: }
173:
174: }
|