KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb > plugins > SingletonStatelessSessionInstancePool


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.ejb.plugins;
23
24 import java.rmi.RemoteException JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26
27 import org.jboss.deployment.DeploymentException;
28 import org.jboss.ejb.EnterpriseContext;
29 import org.jboss.ejb.StatelessSessionEnterpriseContext;
30 import org.jboss.metadata.MetaData;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Singleton pool for session beans. This lets you have
35  * singletons in EJB!
36  *
37  * @author Rickard Oberg
38  * @version $Revision: 37459 $
39  */

40 public class SingletonStatelessSessionInstancePool extends AbstractInstancePool
41 {
42    // Constants -----------------------------------------------------
43

44    // Attributes ----------------------------------------------------
45
EnterpriseContext ctx;
46    boolean inUse = false;
47    boolean isSynchronized = true;
48
49    // Static --------------------------------------------------------
50

51    // Constructors --------------------------------------------------
52

53    // Public --------------------------------------------------------
54

55    public void create()
56       throws Exception JavaDoc
57    {
58    }
59
60    public void start()
61       throws Exception JavaDoc
62    {
63    }
64
65    public void stop()
66    {
67    }
68
69    public void destroy()
70    {
71    }
72
73    /**
74     * Get the singleton instance
75     *
76     * @return Context /w instance
77     * @exception Exception
78     */

79    public synchronized EnterpriseContext get()
80       throws Exception JavaDoc
81    {
82       // Wait while someone else is using it
83
while(inUse && isSynchronized)
84       {
85          try { this.wait(); } catch (InterruptedException JavaDoc e) {}
86       }
87
88       // Create if not already created (or it has been discarded)
89
if (ctx == null)
90       {
91          try
92          {
93             ctx = create(getContainer().createBeanClassInstance());
94          } catch (InstantiationException JavaDoc e)
95          {
96             throw new EJBException JavaDoc("Could not instantiate bean", e);
97          } catch (IllegalAccessException JavaDoc e)
98          {
99             throw new EJBException JavaDoc("Could not instantiate bean", e);
100          }
101       }
102       else
103       {
104       }
105
106       // Lock and return instance
107
inUse = true;
108       return ctx;
109    }
110
111    /**
112     * Return an instance after invocation.
113     *
114     * Called in 2 cases:
115     * a) Done with finder method
116     * b) Just removed
117     *
118     * @param ctx
119     */

120    public synchronized void free(EnterpriseContext ctx)
121    {
122       // Notify waiters
123
inUse = false;
124       this.notifyAll();
125    }
126
127    public synchronized void discard(EnterpriseContext ctx)
128    {
129       // Throw away
130
try
131       {
132          ctx.discard();
133       } catch (RemoteException JavaDoc e)
134       {
135          // DEBUG Logger.exception(e);
136
}
137
138       // Notify waiters
139
inUse = false;
140       this.notifyAll();
141    }
142
143    /**
144     * Add a instance in the pool
145     */

146    public void add()
147       throws Exception JavaDoc
148    {
149       // Empty
150
}
151
152    public int getCurrentSize()
153    {
154       return 1;
155    }
156
157    public int getMaxSize()
158    {
159       return 1;
160    }
161
162    // Z implementation ----------------------------------------------
163

164     // XmlLoadable implementation
165
public void importXml(Element JavaDoc element) throws DeploymentException
166     {
167       Element JavaDoc synch = MetaData.getUniqueChild(element, "Synchronized");
168       isSynchronized = Boolean.valueOf(MetaData.getElementContent(synch)).booleanValue();
169     }
170
171    // Package protected ---------------------------------------------
172

173    // Protected -----------------------------------------------------
174
protected EnterpriseContext create(Object JavaDoc instance)
175       throws Exception JavaDoc
176    {
177       // The instance is created by the caller and is a newInstance();
178
return new StatelessSessionEnterpriseContext(instance, getContainer());
179    }
180    // Private -------------------------------------------------------
181

182    // Inner classes -------------------------------------------------
183

184 }
185
Popular Tags