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.cmp2.cacheinvalidation.ejb;
023:
024: import org.jboss.logging.Logger;
025:
026: import javax.ejb.SessionBean;
027: import javax.ejb.SessionContext;
028: import javax.ejb.CreateException;
029: import javax.ejb.FinderException;
030: import javax.naming.InitialContext;
031: import javax.naming.NamingException;
032:
033: /**
034: * @author <a href="mailto:alex@jboss.org">Alexey Loubyansky</a>
035: * @version <tt>$Revision: 57211 $</tt>
036: */
037: public class FacadeSessionBean implements SessionBean {
038: private static final Logger log = Logger
039: .getLogger(FacadeSessionBean.class);
040: SessionContext ctx;
041:
042: private transient CLocalHome ch;
043: private transient ALocalHome ah;
044:
045: // Business methods
046:
047: /**
048: * @ejb.interface-method
049: * @ejb.transaction type="RequiresNew"
050: */
051: public void setup() throws Exception {
052: final long startTime = System.currentTimeMillis();
053: log.debug("SETUP>");
054:
055: CLocal c = getCLocalHome("CRWLocal").create(new Long(1));
056: c.setFirstName("Avoka");
057:
058: ALocal a = getALocalHome("ARWLocal").create(new Long(2),
059: "Ataka");
060: a.setC(c);
061:
062: log.debug("SETUP> done in "
063: + (System.currentTimeMillis() - startTime) + " ms.");
064: }
065:
066: /**
067: * @ejb.interface-method
068: * @ejb.transaction type="RequiresNew"
069: */
070: public void tearDown() throws Exception {
071: final long startTime = System.currentTimeMillis();
072: log.info("TEAR DOWN>");
073:
074: try {
075: CLocal c = getCLocalHome("CRWLocal").findByPrimaryKey(
076: new Long(1));
077: c.remove();
078: } catch (FinderException e) {
079: }
080:
081: try {
082: ALocal a = getALocalHome("ARWLocal").findByPrimaryKey(
083: new Long(2));
084: a.remove();
085: } catch (FinderException e) {
086: }
087:
088: log.info("TEAR DOWN> done in "
089: + (System.currentTimeMillis() - startTime) + " ms.");
090: }
091:
092: /**
093: * @ejb.interface-method
094: * @ejb.transaction type="RequiresNew"
095: */
096: public String readFirstName(String jndiName, Long id)
097: throws Exception {
098: final long startTime = System.currentTimeMillis();
099: log.info("READ> jndiName=" + jndiName);
100:
101: final CLocalHome ch = (CLocalHome) getHome(jndiName);
102: CLocal c = ch.findByPrimaryKey(id);
103:
104: final String firstName = c.getFirstName();
105: log.info(jndiName + ".name=" + firstName);
106:
107: log.info("READ> jndiName=" + jndiName + " done in "
108: + (System.currentTimeMillis() - startTime) + " ms.");
109: return firstName;
110: }
111:
112: /**
113: * @ejb.interface-method
114: * @ejb.transaction type="RequiresNew"
115: */
116: public void writeFirstName(String jndiName, Long id, String name)
117: throws Exception {
118: final long startTime = System.currentTimeMillis();
119: log.info("WRITE> jndiName=" + jndiName);
120:
121: final CLocalHome ch = (CLocalHome) getHome(jndiName);
122: CLocal c = ch.findByPrimaryKey(id);
123:
124: c.setFirstName(name);
125: log.info(jndiName + ".name=" + c.getFirstName());
126:
127: log.info("WRITE> jndiName=" + jndiName + " done in "
128: + (System.currentTimeMillis() - startTime) + " ms.");
129: }
130:
131: /**
132: * @ejb.interface-method
133: * @ejb.transaction type="RequiresNew"
134: */
135: public String readRelatedAFirstName(String jndiName, Long id)
136: throws Exception {
137: final long startTime = System.currentTimeMillis();
138: log.info("READ RELATED> jndiName=" + jndiName);
139:
140: final CLocalHome ch = (CLocalHome) getHome(jndiName);
141: CLocal c = ch.findByPrimaryKey(id);
142:
143: final String firstName = c.getA() == null ? null : c.getA()
144: .getName();
145: log.info(jndiName + ".a.name=" + firstName);
146:
147: log.info("READ RELATED> jndiName=" + jndiName + " done in "
148: + (System.currentTimeMillis() - startTime) + " ms.");
149: return firstName;
150: }
151:
152: /**
153: * @ejb.interface-method
154: * @ejb.transaction type="RequiresNew"
155: */
156: public void removeA(String jndiName, Long id) throws Exception {
157: final long startTime = System.currentTimeMillis();
158: log.info("REMOVE> jndiName=" + jndiName);
159:
160: final ALocalHome ah = (ALocalHome) getHome(jndiName);
161: ALocal a = ah.findByPrimaryKey(id);
162: a.remove();
163:
164: log.info("REMOVE> jndiName=" + jndiName + " done in "
165: + (System.currentTimeMillis() - startTime) + " ms.");
166: }
167:
168: // SessionBean implementation
169:
170: /**
171: * @throws CreateException Description of Exception
172: * @ejb.create-method
173: */
174: public void ejbCreate() throws CreateException {
175: }
176:
177: public void ejbActivate() {
178: }
179:
180: public void ejbPassivate() {
181: }
182:
183: public void ejbRemove() {
184: }
185:
186: public void setSessionContext(SessionContext ctx) {
187: this .ctx = ctx;
188: }
189:
190: private CLocalHome getCLocalHome(String jndiName) {
191: if (ch == null) {
192: ch = (CLocalHome) getHome(jndiName);
193: }
194: return ch;
195: }
196:
197: private ALocalHome getALocalHome(String jndiName) {
198: if (ah == null) {
199: ah = (ALocalHome) getHome(jndiName);
200: }
201: return ah;
202: }
203:
204: private Object getHome(String jndiName) {
205: try {
206: InitialContext ctx = new InitialContext();
207: return ctx.lookup(jndiName);
208: } catch (NamingException e) {
209: throw new IllegalStateException("Failed to look up home "
210: + jndiName + ": " + e.getMessage());
211: }
212: }
213: }
|