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.hibernate.ejb;
023:
024: import org.jboss.test.hibernate.model.User;
025: import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceHome;
026: import org.jboss.test.hibernate.ejb.interfaces.ProfileServiceUtil;
027: import org.jboss.test.hibernate.ejb.interfaces.ProfileService;
028:
029: import javax.ejb.SessionBean;
030: import javax.ejb.EJBException;
031: import javax.ejb.SessionContext;
032: import javax.ejb.CreateException;
033: import javax.naming.NamingException;
034: import java.rmi.RemoteException;
035: import java.util.List;
036:
037: /**
038: * An ejb which simply wraps calls to the ProfileBean service.
039: * Used to test the issue with sessions, nested ejb calls,
040: * and the CachedConnectionManager.
041: *
042: * @author <a href="mailto:steve@hibernate.org">Steve Ebersole</a>
043: * @version $Revision: 57211 $
044: *
045: * @ejb:bean name="AggregateProfileService"
046: * jndi-name="AggregateProfileService"
047: * view-type="remote"
048: * type="Stateless"
049: */
050: public class AggregateProfileBean implements SessionBean {
051: /**
052: * @exception javax.ejb.EJBException if an error occurs
053: * @ejb:interface-method
054: */
055: public User storeUser(User user) throws EJBException {
056: ProfileService service = null;
057: try {
058: service = locateService();
059: return service.storeUser(user);
060: } catch (EJBException e) {
061: throw e;
062: } catch (RemoteException e) {
063: throw new EJBException("Unable to access profile service",
064: e);
065: } finally {
066: release(service);
067: }
068: }
069:
070: /**
071: * @exception javax.ejb.EJBException if an error occurs
072: * @ejb:interface-method
073: */
074: public User loadUser(long id) throws EJBException {
075: ProfileService service = null;
076: try {
077: service = locateService();
078: return service.loadUser(id);
079: } catch (EJBException e) {
080: throw e;
081: } catch (RemoteException e) {
082: throw new EJBException("Unable to access profile service",
083: e);
084: } finally {
085: release(service);
086: }
087: }
088:
089: /**
090: * @exception javax.ejb.EJBException if an error occurs
091: * @ejb:interface-method
092: */
093: public User loadUser(Long id) throws EJBException {
094: ProfileService service = null;
095: try {
096: service = locateService();
097: return service.loadUser(id);
098: } catch (EJBException e) {
099: throw e;
100: } catch (RemoteException e) {
101: throw new EJBException("Unable to access profile service",
102: e);
103: } finally {
104: release(service);
105: }
106: }
107:
108: /**
109: * @exception javax.ejb.EJBException if an error occurs
110: * @ejb:interface-method
111: */
112: public List listUsers() throws EJBException {
113: ProfileService service = null;
114: try {
115: service = locateService();
116: return service.listUsers();
117: } catch (EJBException e) {
118: throw e;
119: } catch (RemoteException e) {
120: throw new EJBException("Unable to access profile service",
121: e);
122: } finally {
123: release(service);
124: }
125: }
126:
127: /**
128: * @ejb:create-method
129: */
130: public void ejbCreate() {
131: }
132:
133: public void ejbActivate() throws EJBException, RemoteException {
134: }
135:
136: public void ejbPassivate() throws EJBException, RemoteException {
137: }
138:
139: public void ejbRemove() throws EJBException, RemoteException {
140: }
141:
142: public void setSessionContext(SessionContext ctx)
143: throws EJBException, RemoteException {
144: }
145:
146: private ProfileService locateService() throws EJBException {
147: try {
148: ProfileServiceHome home = ProfileServiceUtil.getHome();
149: return home.create();
150: } catch (NamingException e) {
151: throw new EJBException("Unable to locate profile service",
152: e);
153: } catch (CreateException e) {
154: throw new EJBException(
155: "Unable to create ref to profile service", e);
156: } catch (RemoteException e) {
157: throw new EJBException("Unable to access profile service",
158: e);
159: }
160: }
161:
162: private void release(ProfileService service) {
163: if (service != null) {
164: try {
165: service.remove();
166: } catch (Throwable ignore) {
167: }
168: }
169: }
170: }
|