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.ejb.plugins;
023:
024: import java.rmi.RemoteException;
025: import javax.ejb.EJBException;
026:
027: import org.jboss.deployment.DeploymentException;
028: import org.jboss.ejb.EnterpriseContext;
029: import org.jboss.ejb.StatelessSessionEnterpriseContext;
030: import org.jboss.metadata.MetaData;
031: import org.w3c.dom.Element;
032:
033: /**
034: * Singleton pool for session beans. This lets you have
035: * singletons in EJB!
036: *
037: * @author Rickard Oberg
038: * @version $Revision: 57209 $
039: */
040: public class SingletonStatelessSessionInstancePool extends
041: AbstractInstancePool {
042: // Constants -----------------------------------------------------
043:
044: // Attributes ----------------------------------------------------
045: EnterpriseContext ctx;
046: boolean inUse = false;
047: boolean isSynchronized = true;
048:
049: // Static --------------------------------------------------------
050:
051: // Constructors --------------------------------------------------
052:
053: // Public --------------------------------------------------------
054:
055: public void create() throws Exception {
056: }
057:
058: public void start() throws Exception {
059: }
060:
061: public void stop() {
062: }
063:
064: public void destroy() {
065: }
066:
067: /**
068: * Get the singleton instance
069: *
070: * @return Context /w instance
071: * @exception Exception
072: */
073: public synchronized EnterpriseContext get() throws Exception {
074: // Wait while someone else is using it
075: while (inUse && isSynchronized) {
076: try {
077: this .wait();
078: } catch (InterruptedException e) {
079: }
080: }
081:
082: // Create if not already created (or it has been discarded)
083: if (ctx == null) {
084: try {
085: ctx = create(getContainer().createBeanClassInstance());
086: } catch (InstantiationException e) {
087: throw new EJBException("Could not instantiate bean", e);
088: } catch (IllegalAccessException e) {
089: throw new EJBException("Could not instantiate bean", e);
090: }
091: } else {
092: }
093:
094: // Lock and return instance
095: inUse = true;
096: return ctx;
097: }
098:
099: /**
100: * Return an instance after invocation.
101: *
102: * Called in 2 cases:
103: * a) Done with finder method
104: * b) Just removed
105: *
106: * @param ctx
107: */
108: public synchronized void free(EnterpriseContext ctx) {
109: // Notify waiters
110: inUse = false;
111: this .notifyAll();
112: }
113:
114: public synchronized void discard(EnterpriseContext ctx) {
115: // Throw away
116: try {
117: ctx.discard();
118: } catch (RemoteException e) {
119: // DEBUG Logger.exception(e);
120: }
121:
122: // Notify waiters
123: inUse = false;
124: this .notifyAll();
125: }
126:
127: /**
128: * Add a instance in the pool
129: */
130: public void add() throws Exception {
131: // Empty
132: }
133:
134: public int getCurrentSize() {
135: return 1;
136: }
137:
138: public int getMaxSize() {
139: return 1;
140: }
141:
142: // Z implementation ----------------------------------------------
143:
144: // XmlLoadable implementation
145: public void importXml(Element element) throws DeploymentException {
146: Element synch = MetaData
147: .getUniqueChild(element, "Synchronized");
148: isSynchronized = Boolean.valueOf(
149: MetaData.getElementContent(synch)).booleanValue();
150: }
151:
152: // Package protected ---------------------------------------------
153:
154: // Protected -----------------------------------------------------
155: protected EnterpriseContext create(Object instance)
156: throws Exception {
157: // The instance is created by the caller and is a newInstance();
158: return new StatelessSessionEnterpriseContext(instance,
159: getContainer());
160: }
161: // Private -------------------------------------------------------
162:
163: // Inner classes -------------------------------------------------
164:
165: }
|