KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > AbstractPool


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.ejb3;
23
24 import org.jboss.injection.Injector;
25 import org.jboss.ejb3.stateful.StatefulBeanContext;
26 import org.jboss.logging.Logger;
27 import org.jboss.util.id.GUID;
28
29 /**
30  * Comment
31  *
32  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
33  * @version $Revision: 58478 $
34  */

35 public abstract class AbstractPool implements Pool
36 {
37    private static final Logger log = Logger.getLogger(EJBContainer.class);
38
39    protected Class JavaDoc beanClass;
40    protected Class JavaDoc contextClass;
41    protected Injector[] injectors;
42    protected Container container;
43
44    public AbstractPool()
45    {
46
47    }
48
49    public void initialize(Container container, Class JavaDoc contextClass, Class JavaDoc beanClass, int maxSize, long timeout)
50    {
51       this.beanClass = beanClass;
52       this.contextClass = contextClass;
53       this.container = container;
54    }
55
56    public void setMaxSize(int maxSize)
57    {
58    }
59
60    protected BeanContext create()
61    {
62       Object JavaDoc bean;
63       BeanContext ctx;
64       try
65       {
66          bean = container.construct();
67          ctx = (BeanContext) contextClass.newInstance();
68          ctx.setContainer(container);
69          ctx.setInstance(bean);
70       }
71       catch (InstantiationException JavaDoc e)
72       {
73          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
74
}
75       catch (IllegalAccessException JavaDoc e)
76       {
77          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
78
}
79       if (ctx instanceof StatefulBeanContext)
80       {
81          // this is for propagated extended PC's
82
StatefulBeanContext sfctx = (StatefulBeanContext) ctx;
83          sfctx.setId(new GUID());
84          ctx = sfctx = sfctx.pushContainedIn();
85       }
86       try
87       {
88          if (injectors != null)
89          {
90             for (int i = 0; i < injectors.length; i++)
91             {
92                injectors[i].inject(ctx);
93             }
94          }
95
96          ctx.initialiseInterceptorInstances();
97
98       }
99       finally
100       {
101          if (ctx instanceof StatefulBeanContext)
102          {
103             // this is for propagated extended PC's
104
StatefulBeanContext sfctx = (StatefulBeanContext) ctx;
105             sfctx.popContainedIn();
106          }
107       }
108
109       //TODO This needs to be reimplemented as replacement for create() on home interface
110
container.invokeInit(bean);
111
112       container.invokePostConstruct(ctx);
113       return ctx;
114    }
115
116    protected BeanContext create(Class JavaDoc[] initTypes, Object JavaDoc[] initValues)
117    {
118       Object JavaDoc bean;
119       BeanContext ctx;
120       try
121       {
122          bean = container.construct();
123          ctx = (BeanContext) contextClass.newInstance();
124          ctx.setContainer(container);
125          ctx.setInstance(bean);
126       }
127       catch (InstantiationException JavaDoc e)
128       {
129          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
130
}
131       catch (IllegalAccessException JavaDoc e)
132       {
133          throw new RuntimeException JavaDoc(e); //To change body of catch statement use Options | File Templates.
134
}
135       if (ctx instanceof StatefulBeanContext)
136       {
137          // this is for propagated extended PC's
138
StatefulBeanContext sfctx = (StatefulBeanContext) ctx;
139          sfctx.setId(new GUID());
140          ctx = sfctx = sfctx.pushContainedIn();
141       }
142       try
143       {
144          if (injectors != null)
145          {
146             for (int i = 0; i < injectors.length; i++)
147             {
148                injectors[i].inject(ctx);
149             }
150          }
151
152          ctx.initialiseInterceptorInstances();
153
154       }
155       finally
156       {
157          if (ctx instanceof StatefulBeanContext)
158          {
159             // this is for propagated extended PC's
160
StatefulBeanContext sfctx = (StatefulBeanContext) ctx;
161             sfctx.popContainedIn();
162          }
163       }
164       //TODO This needs to be reimplemented as replacement for create() on home interface
165
container.invokeInit(bean, initTypes, initValues);
166
167       container.invokePostConstruct(ctx);
168       return ctx;
169    }
170
171    public void remove(BeanContext ctx)
172    {
173       try
174       {
175          container.invokePreDestroy(ctx);
176       }
177       finally
178       {
179          ctx.remove();
180       }
181    }
182
183    public void discard(BeanContext ctx)
184    {
185       remove(ctx);
186    }
187
188    public void setInjectors(Injector[] injectors)
189    {
190       this.injectors = injectors;
191    }
192 }
193
Popular Tags