KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > ejb > StrictlyPooledCreateExceptionBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cts.ejb;
23
24 import javax.ejb.EJBException JavaDoc;
25 import javax.ejb.SessionContext JavaDoc;
26 import javax.ejb.SessionBean JavaDoc;
27 import javax.ejb.CreateException JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import javax.naming.NamingException JavaDoc;
30 import org.jboss.logging.Logger;
31
32 /** A session bean that tests the behavior of a session bean with strict
33  * pooliing that throws an exception from ejbCreate
34  *
35  * @author Scott.Stark@jboss.org
36  * @version $Revision: 37406 $
37  */

38 public class StrictlyPooledCreateExceptionBean implements SessionBean JavaDoc
39 {
40    private static Logger log = Logger.getLogger(StatelessSessionBean.class);
41    /** The class wide max count of instances allows */
42    private static boolean ejbCreateFailed = false;
43    /** The class wide max count of instances allows */
44    private static int maxActiveCount = 5;
45    /** The class wide count of instances active in business code */
46    private static int activeCount;
47
48    private SessionContext JavaDoc ctx;
49
50    private static synchronized int incActiveCount()
51    {
52       return activeCount ++;
53    }
54    private static synchronized int decActiveCount()
55    {
56       return activeCount --;
57    }
58
59    public void ejbCreate() throws CreateException JavaDoc
60    {
61       log.info("ejbCreate");
62       // Fail the first caller
63
if( ejbCreateFailed == false )
64       {
65          ejbCreateFailed = true;
66          throw new CreateException JavaDoc("First ejbCreate Failure");
67       }
68    }
69    public void ejbActivate()
70    {
71    }
72
73    public void ejbPassivate()
74    {
75    }
76
77    public void ejbRemove()
78    {
79    }
80
81    public void setSessionContext(SessionContext JavaDoc ctx)
82    {
83       this.ctx = ctx;
84       try
85       {
86          InitialContext JavaDoc iniCtx = new InitialContext JavaDoc();
87          Integer JavaDoc i = (Integer JavaDoc) iniCtx.lookup("java:comp/env/maxActiveCount");
88          maxActiveCount = i.intValue();
89       }
90       catch(NamingException JavaDoc e)
91       {
92          // Use default count of 5
93
}
94    }
95
96    public void methodA()
97    {
98       int count = incActiveCount();
99       log.debug("Begin methodA, activeCount="+count+", ctx="+ctx);
100       try
101       {
102          if( count > maxActiveCount )
103          {
104             String JavaDoc msg = "IllegalState, activeCount > maxActiveCount, "
105                   + count + " > " + maxActiveCount;
106             throw new EJBException JavaDoc(msg);
107          }
108          // Sleep to let the client thread pile up
109
Thread.sleep(1000);
110       }
111       catch(InterruptedException JavaDoc e)
112       {
113       }
114       finally
115       {
116          count = decActiveCount();
117          log.debug("End methodA, activeCount="+count+", ctx="+ctx);
118       }
119    }
120 }
121
Popular Tags