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.cts.ejb;
023:
024: import javax.ejb.EJBException;
025: import javax.ejb.SessionContext;
026: import javax.ejb.SessionBean;
027: import javax.ejb.CreateException;
028: import javax.naming.InitialContext;
029: import javax.naming.NamingException;
030: import org.jboss.logging.Logger;
031:
032: /** A session bean that tests the behavior of a session bean with strict
033: * pooliing that throws an exception from ejbCreate
034: *
035: * @author Scott.Stark@jboss.org
036: * @version $Revision: 57211 $
037: */
038: public class StrictlyPooledCreateExceptionBean implements SessionBean {
039: private static Logger log = Logger
040: .getLogger(StatelessSessionBean.class);
041: /** The class wide max count of instances allows */
042: private static boolean ejbCreateFailed = false;
043: /** The class wide max count of instances allows */
044: private static int maxActiveCount = 5;
045: /** The class wide count of instances active in business code */
046: private static int activeCount;
047:
048: private SessionContext ctx;
049:
050: private static synchronized int incActiveCount() {
051: return activeCount++;
052: }
053:
054: private static synchronized int decActiveCount() {
055: return activeCount--;
056: }
057:
058: public void ejbCreate() throws CreateException {
059: log.info("ejbCreate");
060: // Fail the first caller
061: if (ejbCreateFailed == false) {
062: ejbCreateFailed = true;
063: throw new CreateException("First ejbCreate Failure");
064: }
065: }
066:
067: public void ejbActivate() {
068: }
069:
070: public void ejbPassivate() {
071: }
072:
073: public void ejbRemove() {
074: }
075:
076: public void setSessionContext(SessionContext ctx) {
077: this .ctx = ctx;
078: try {
079: InitialContext iniCtx = new InitialContext();
080: Integer i = (Integer) iniCtx
081: .lookup("java:comp/env/maxActiveCount");
082: maxActiveCount = i.intValue();
083: } catch (NamingException e) {
084: // Use default count of 5
085: }
086: }
087:
088: public void methodA() {
089: int count = incActiveCount();
090: log.debug("Begin methodA, activeCount=" + count + ", ctx="
091: + ctx);
092: try {
093: if (count > maxActiveCount) {
094: String msg = "IllegalState, activeCount > maxActiveCount, "
095: + count + " > " + maxActiveCount;
096: throw new EJBException(msg);
097: }
098: // Sleep to let the client thread pile up
099: Thread.sleep(1000);
100: } catch (InterruptedException e) {
101: } finally {
102: count = decActiveCount();
103: log.debug("End methodA, activeCount=" + count + ", ctx="
104: + ctx);
105: }
106: }
107: }
|