Source Code Cross Referenced for BasicObjectPool.java in  » Net » QuickServer » org » quickserver » util » pool » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Net » QuickServer » org.quickserver.util.pool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * This file is part of the QuickServer library 
003:         * Copyright (C) 2003-2005 QuickServer.org
004:         *
005:         * Use, modification, copying and distribution of this software is subject to
006:         * the terms and conditions of the GNU Lesser General Public License. 
007:         * You should have received a copy of the GNU LGP License along with this 
008:         * library; if not, you can download a copy from <http://www.quickserver.org/>.
009:         *
010:         * For questions, suggestions, bug-reports, enhancement-requests etc.
011:         * visit http://www.quickserver.org
012:         *
013:         */
014:
015:        package org.quickserver.util.pool;
016:
017:        import java.util.*;
018:        import org.apache.commons.pool.*;
019:        import org.apache.commons.pool.impl.*;
020:        import java.util.logging.*;
021:
022:        /**
023:         * This class will maintain a simple pool of object instances.
024:         * It internally used a <code>HashSet</code>
025:         * @author Akshathkumar Shetty
026:         * @since 1.3
027:         */
028:        public class BasicObjectPool implements  QSObjectPool {
029:            private static final Logger logger = Logger
030:                    .getLogger(BasicObjectPool.class.getName());
031:
032:            private PoolableObjectFactory factory;
033:            private Config config;
034:            private Set activeObjects, idleObjects;
035:            private volatile boolean inMaintain = false;
036:
037:            public BasicObjectPool() {
038:                activeObjects = Collections.synchronizedSet(new HashSet());
039:                idleObjects = Collections.synchronizedSet(new HashSet());
040:                config = new Config();
041:            }
042:
043:            public BasicObjectPool(PoolableObjectFactory factory,
044:                    BasicObjectPool.Config config) {
045:                this ();
046:                this .factory = factory;
047:                if (config != null)
048:                    this .config = config;
049:            }
050:
051:            public void addObject() throws Exception {
052:                if (config.maxIdle == -1 || config.maxIdle > getNumIdle())
053:                    idleObjects.add(factory.makeObject());
054:                else
055:                    maintain();
056:            }
057:
058:            public Object borrowObject() throws Exception {
059:                if (getNumIdle() <= 0
060:                        && (config.maxActive == -1 || config.maxActive > getNumActive())) {
061:                    addObject();
062:                }
063:                if (getNumIdle() <= 0) {
064:                    throw new NoSuchElementException(
065:                            "No free objects! MaxActive:" + config.maxActive
066:                                    + ", NumActive:" + getNumActive());
067:                }
068:
069:                Object obj = null;
070:                synchronized (this ) {
071:                    obj = idleObjects.iterator().next();
072:                    idleObjects.remove(obj);
073:                    factory.activateObject(obj);
074:                    activeObjects.add(obj);
075:                }
076:                return obj;
077:            }
078:
079:            /**Clears any objects sitting idle in the pool*/
080:            public synchronized void clear() {
081:                Iterator iterator = idleObjects.iterator();
082:                while (iterator.hasNext()) {
083:                    try {
084:                        invalidateObject(iterator.next());
085:                    } catch (Exception e) {
086:                        logger.warning("Error in BasicObjectPool.clear : " + e);
087:                    }
088:                }
089:                idleObjects.clear();
090:            }
091:
092:            /**Close this pool, and free any resources associated with it.*/
093:            public void close() throws Exception {
094:                clear();
095:                /*
096:                Iterator iterator = activeObjects.iterator();
097:                while(iterator.hasNext()) {
098:                	try {
099:                		invalidateObject(iterator.next());
100:                	} catch(Exception e) {
101:                		logger.warning("Error in BasicObjectPool.close : "+e);
102:                	}
103:                }
104:                 */
105:                activeObjects.clear();
106:            }
107:
108:            /**Return the number of instances currently borrowed from my pool */
109:            public int getNumActive() {
110:                return activeObjects.size();
111:            }
112:
113:            /**Return the number of instances currently idle in my pool */
114:            public int getNumIdle() {
115:                return idleObjects.size();
116:            }
117:
118:            /**Invalidates an object from the pool */
119:            public void invalidateObject(Object obj) throws Exception {
120:                factory.destroyObject(obj);
121:            }
122:
123:            /**Return an instance to my pool*/
124:            public synchronized void returnObject(Object obj) throws Exception {
125:                activeObjects.remove(obj);
126:                if (factory.validateObject(obj) == false) {
127:                    logger.finer("Object not good for return: " + obj);
128:                    return;
129:                }
130:                factory.passivateObject(obj);
131:                idleObjects.add(obj);
132:                if (config.maxIdle != -1 && config.maxIdle < getNumIdle()) {
133:                    maintain();
134:                }
135:            }
136:
137:            /**Sets the factory I use to create new instances */
138:            public void setFactory(PoolableObjectFactory factory) {
139:                this .factory = factory;
140:            }
141:
142:            private void maintain() {
143:                if (inMaintain == true) {
144:                    return;
145:                }
146:                inMaintain = true;
147:                logger.finest("Starting maintain: " + getNumIdle());
148:                while (getNumIdle() > config.maxIdle) {
149:                    try {
150:                        synchronized (idleObjects) {
151:                            Object obj = idleObjects.iterator().next();
152:                            idleObjects.remove(obj);
153:                            invalidateObject(obj);
154:                        }
155:                    } catch (Exception e) {
156:                        logger.warning("Error in BasicObjectPool.maintain : "
157:                                + e);
158:                    }
159:                }
160:                inMaintain = false;
161:                logger.finest("Finished maintain: " + getNumIdle());
162:            }
163:
164:            public static class Config {
165:                public int maxActive = -1;
166:                public int maxIdle = 10;
167:            }
168:
169:            /**
170:             * Returns the iterator of all active objects
171:             * @since 1.3.1
172:             */
173:            public Iterator getAllActiveObjects() {
174:                List _list = new LinkedList();
175:                _list.addAll(activeObjects);
176:                return _list.iterator(); //*/activeObjects.iterator();
177:            }
178:
179:            public Object getObjectToSynchronize() {
180:                return activeObjects;
181:            }
182:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.