001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.render.web;
029:
030: import java.util.*;
031: import java.util.logging.*;
032:
033: /**
034: * @author Joshua J. Gertzen
035: */
036: class EventProcessorPool {
037: private static final Logger log = Logger
038: .getLogger(EventProcessorPool.class.getName());
039: private static final Level LEVEL = Level.FINER;
040:
041: static final EventProcessorPool INSTANCE = new EventProcessorPool();
042:
043: private List<EventProcessor> pool = new LinkedList<EventProcessor>();
044: private Map<WebApplication, EventProcessor> appToProcessor = new HashMap<WebApplication, EventProcessor>();
045:
046: private EventProcessorPool() {
047: }
048:
049: EventProcessor getProcessor(WebApplication app) {
050: synchronized (appToProcessor) {
051: EventProcessor proc = appToProcessor.get(app);
052:
053: if (proc == null) {
054: if (pool.size() > 0) {
055: proc = pool.remove(0);
056: if (log.isLoggable(LEVEL))
057: log.log(LEVEL, "Allocating " + proc.getName()
058: + " from pool");
059: } else {
060: proc = new EventProcessor(this );
061: proc.start();
062: if (log.isLoggable(LEVEL))
063: log.log(LEVEL, "New " + proc.getName()
064: + " from pool");
065: }
066:
067: proc.app = app;
068: appToProcessor.put(app, proc);
069: } else {
070: if (log.isLoggable(LEVEL))
071: log.log(LEVEL, "Existing " + proc.getName()
072: + " allocated to app");
073: }
074:
075: return proc;
076: }
077: }
078:
079: void returnToPool(EventProcessor proc) {
080: synchronized (appToProcessor) {
081: if (proc.app == null) {
082: if (log.isLoggable(LEVEL))
083: log
084: .log(
085: LEVEL,
086: "No application tied to EventProcessor "
087: + proc.getName()
088: + ", probably removed from pool in processUserActionEvents finally block");
089: return;
090: }
091:
092: if (!proc.isInUse()) {
093: if (log.isLoggable(LEVEL))
094: log.log(LEVEL, "Returning " + proc.getName()
095: + " to pool");
096: appToProcessor.remove(proc.app);
097: proc.app = null;
098: pool.add(proc);
099: } else {
100: if (log.isLoggable(LEVEL))
101: log
102: .log(
103: LEVEL,
104: "Cannot return "
105: + proc.getName()
106: + " because thread is waiting in unreturnable call");
107: }
108: }
109: }
110:
111: void removeFromPool(EventProcessor proc) {
112: synchronized (appToProcessor) {
113: if (log.isLoggable(LEVEL))
114: log.log(LEVEL, "Removing " + proc.getName()
115: + " from pool - (proc.app == null) == "
116: + (proc.app == null));
117: if (proc.app != null)
118: appToProcessor.remove(proc.app);
119: proc.app = null;
120: pool.remove(proc);
121: }
122: }
123: }
|