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.entity.ejb;
023:
024: import java.rmi.RemoteException;
025:
026: import javax.ejb.CreateException;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionBean;
029: import javax.ejb.SessionContext;
030: import javax.naming.InitialContext;
031:
032: import org.jboss.test.entity.interfaces.EntitySession;
033: import org.jboss.test.entity.interfaces.EntitySessionHome;
034: import org.jboss.test.entity.interfaces.Pathological;
035: import org.jboss.test.entity.interfaces.PathologicalEntity;
036: import org.jboss.test.entity.interfaces.PathologicalEntityHome;
037:
038: /**
039: * Session facade for entity testing.
040: *
041: * @author Adrian.Brock@HappeningTimes.com
042: * @version $Revision: 57211 $
043: */
044: public class EntitySessionBean implements SessionBean {
045: private transient SessionContext ctx;
046:
047: public void createPathological(String name, boolean pathological) {
048: Pathological.setPathological(pathological);
049: try {
050: PathologicalEntityHome home = getPathologicalEJB();
051: home.create(name);
052: } catch (Throwable e) {
053: check(e);
054: } finally {
055: Pathological.setPathological(false);
056: }
057: }
058:
059: public void removeHomePathological(String name, boolean pathological) {
060: Pathological.setPathological(pathological);
061: try {
062: PathologicalEntityHome home = getPathologicalEJB();
063: home.remove(name);
064: } catch (Throwable e) {
065: check(e);
066: } finally {
067: Pathological.setPathological(false);
068: }
069: }
070:
071: public void removePathological(String name, boolean pathological) {
072: try {
073: PathologicalEntityHome home = getPathologicalEJB();
074: PathologicalEntity bean = home.findByPrimaryKey(name);
075: Pathological.setPathological(pathological);
076: bean.remove();
077: } catch (Throwable e) {
078: check(e);
079: } finally {
080: Pathological.setPathological(false);
081: }
082: }
083:
084: public void findPathological(String name, boolean pathological) {
085: Pathological.setPathological(pathological);
086: try {
087: PathologicalEntityHome home = getPathologicalEJB();
088: home.findByPrimaryKey(name);
089: } catch (Throwable e) {
090: check(e);
091: } finally {
092: Pathological.setPathological(false);
093: }
094: }
095:
096: public void getPathological(String name, boolean pathological) {
097: try {
098: PathologicalEntityHome home = getPathologicalEJB();
099: PathologicalEntity bean = home.findByPrimaryKey(name);
100: Pathological.setPathological(pathological);
101: bean.getSomething();
102: } catch (Throwable e) {
103: check(e);
104: } finally {
105: Pathological.setPathological(false);
106: }
107: }
108:
109: public void setPathological(String name, boolean pathological) {
110: try {
111: PathologicalEntityHome home = getPathologicalEJB();
112: PathologicalEntity bean = home.findByPrimaryKey(name);
113: Pathological.setPathological(pathological);
114: bean.setSomething("something");
115: } catch (Throwable e) {
116: check(e);
117: } finally {
118: Pathological.setPathological(false);
119: }
120: }
121:
122: public void ejbCreate() throws CreateException {
123: }
124:
125: public void setSessionContext(SessionContext ctx) {
126: this .ctx = ctx;
127: }
128:
129: public void ejbActivate() {
130: }
131:
132: public void ejbPassivate() {
133: }
134:
135: public void ejbRemove() {
136: }
137:
138: private void check(Throwable e) {
139: while (true) {
140: if (e instanceof EJBException)
141: e = ((EJBException) e).getCausedByException();
142: else if (e instanceof RemoteException)
143: e = ((RemoteException) e).detail;
144: else if (e instanceof IllegalStateException)
145: throw (IllegalStateException) e;
146: else
147: return;
148: }
149: }
150:
151: private PathologicalEntityHome getPathologicalEJB()
152: throws Exception {
153: return (PathologicalEntityHome) new InitialContext()
154: .lookup("java:comp/env/ejb/PathologicalEJB");
155: }
156: }
|