Source Code Cross Referenced for SimpleObjectPool.java in  » JMX » jfoxmx » org » huihoo » jfox » 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 » JMX » jfoxmx » org.huihoo.jfox.pool 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* JFox, the OpenSource J2EE Application Server
002:         *
003:         * Copyright (C) 2002 huihoo.org
004:         * Distributable under GNU LGPL license
005:         * See the GNU Lesser General Public License for more details.
006:         */
007:
008:        package org.huihoo.jfox.pool;
009:
010:        import java.util.Iterator;
011:        import java.util.List;
012:        import java.util.ArrayList;
013:        import java.lang.reflect.Proxy;
014:
015:        /**
016:         * 
017:         * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
018:         */
019:
020:        public class SimpleObjectPool extends AbstractObjectPool implements 
021:                SimpleObjectPoolMBean {
022:            //已从池中取出的对象数目
023:            private volatile int numWorking = 0;
024:            private List pool = new ArrayList();
025:
026:            /** the number of "sleeping" instances in the pool. */
027:            private int maxRest = 10;
028:
029:            private int initNum = 5;
030:
031:            public SimpleObjectPool(ObjectFactory factory) {
032:                this (factory, 5, 10);
033:            }
034:
035:            public SimpleObjectPool(ObjectFactory factory, int initNum,
036:                    int maxRest) {
037:                super (factory);
038:                this .factory = factory;
039:                this .initNum = initNum;
040:                this .maxRest = maxRest;
041:            }
042:
043:            public SimpleObjectPool(String objectFactoryClassName,
044:                    String poolableClassName) {
045:                super (objectFactoryClassName, poolableClassName);
046:            }
047:
048:            public SimpleObjectPool(String objectFactoryClassName,
049:                    String poolableClassName, int initNum, int maxRest) {
050:                super (objectFactoryClassName, poolableClassName);
051:                this .initNum = initNum;
052:                this .maxRest = maxRest;
053:            }
054:
055:            /**
056:             * retrieve Object from object pool
057:             * @return
058:             */
059:            public PoolableObject retrieveObject() throws Exception {
060:                PoolableObject obj = null;
061:                try {
062:                    synchronized (pool) {
063:                        obj = (PoolableObject) pool.remove(0);
064:                    }
065:                } catch (Exception e) {
066:                    obj = factory.makeObject();
067:                }
068:                if (obj != null) {
069:                    obj.activate();
070:                }
071:                numWorking++;
072:                return obj;
073:            }
074:
075:            /**
076:             * restore the retrived object to object pool
077:             * @return true if object passivate && push success, false if failed
078:             */
079:            public boolean restoreObject(PoolableObject obj) {
080:                numWorking--;
081:
082:                PoolableObject realObject = obj;
083:
084:                if (Proxy.isProxyClass(obj.getClass())) {
085:                    realObject = ((PoolableObjectInvocationHandler) Proxy
086:                            .getInvocationHandler(obj)).getPoolableObject();
087:                }
088:
089:                if (factory.validateObject(realObject)) {
090:                    try {
091:                        obj.passivate();
092:                        synchronized (pool) {
093:                            if (pool.size() < maxRest) {
094:                                pool.add(realObject);
095:                                return true;
096:                            }
097:                        }
098:                    } catch (Exception e) {
099:                        // do nothing , just jump to below
100:                    }
101:                }
102:
103:                // failed
104:                try {
105:                    factory.destroyObject(realObject);
106:                } catch (Exception e) {
107:                    // ignored
108:                }
109:                return false;
110:            }
111:
112:            public boolean removeObject(PoolableObject obj) {
113:                return pool.remove(obj);
114:            }
115:
116:            // clear the pool
117:            public void clear() {
118:                Iterator iter = pool.iterator();
119:                while (iter.hasNext()) {
120:                    try {
121:                        factory.destroyObject((PoolableObject) iter.next());
122:                    } catch (Exception e) {
123:                        // ignore error, keep destroying the rest
124:                    }
125:                }
126:                synchronized (pool) {
127:                    pool.clear();
128:                }
129:            }
130:
131:            public int getWorking() {
132:                return numWorking;
133:            }
134:
135:            public int getRest() {
136:                return pool.size();
137:            }
138:
139:            protected void doInit() throws Exception {
140:                int i = initNum;
141:                while (i-- > 0) {
142:                    try {
143:                        pool.add(factory.makeObject());
144:                    } catch (Exception e) {
145:                        throw new RuntimeException(e.getMessage());
146:                    }
147:                }
148:
149:            }
150:
151:            // just do clear
152:            protected void doDestroy() throws Exception {
153:                this .clear();
154:            }
155:
156:            public int getInitNum() {
157:                return initNum;
158:            }
159:
160:            public int getMaxRest() {
161:                return maxRest;
162:            }
163:
164:            public static void main(String[] args) {
165:
166:            }
167:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.