Source Code Cross Referenced for PoolDataSource.java in  » JMX » jfoxmx » org » huihoo » jfox » datasource » 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.datasource 
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.datasource;
009:
010:        import java.sql.Connection;
011:        import java.sql.SQLException;
012:        import java.sql.DriverManager;
013:        import java.io.PrintWriter;
014:        import java.util.Hashtable;
015:
016:        import javax.sql.DataSource;
017:        import javax.naming.spi.ObjectFactory;
018:        import javax.naming.Referenceable;
019:        import javax.naming.Reference;
020:        import javax.naming.NamingException;
021:        import javax.naming.StringRefAddr;
022:        import javax.naming.Name;
023:        import javax.naming.Context;
024:
025:        import org.huihoo.jfox.pool.connection.SimpleConnectionPool;
026:        import org.huihoo.jfox.service.ComponentSupport;
027:
028:        /**
029:         *
030:         * @author <a href="mailto:kelvin_wym@hotmail.com">Kelvin Wu</a>
031:         */
032:        public class PoolDataSource extends ComponentSupport implements 
033:                DataSource, Referenceable, ObjectFactory, PoolDataSourceMBean {
034:            protected SimpleConnectionPool pool = null; // The connection pool used the get connections.
035:            private String dbDriver; // database driver class name.
036:            private String dbURL; // database url
037:            private String user; // database user
038:            private String password; //database password
039:
040:            /**
041:             * Default constructor used by JNDI to get a new Instance without any parameters.
042:             */
043:            public PoolDataSource() {
044:                //    System.out.println("constructing PoolDataSource...");
045:            }
046:
047:            /**
048:             * Constructor specify database connection parameters to generate a valid and usable datasource.
049:             * @param dbDriver database class name according your dabase
050:             * @param dbURL database url used to access database
051:             * @param user valid database user
052:             * @param password // corresponding database password
053:             * @throws Exception any exception who cares?
054:             */
055:            public PoolDataSource(String dbDriver, String dbURL, String user,
056:                    String password) throws Exception {
057:                this .dbDriver = dbDriver;
058:                this .dbURL = dbURL;
059:                this .user = user;
060:                this .password = password;
061:                pool = new SimpleConnectionPool(dbDriver, dbURL, user, password);
062:            }
063:
064:            /**
065:             * Get usable connection from datasource
066:             * @return Connection the valid database connection can used by user
067:             * @throws SQLException
068:             */
069:            public Connection getConnection() throws SQLException {
070:                // since the pool.getConnection() throws out exception, so I have to catch it.
071:                // Please Young consider about the excpetion handler meshiasm.
072:                try {
073:                    return pool.getConnection();
074:                } catch (SQLException sqle) {
075:                    throw new SQLException(sqle.getLocalizedMessage(), sqle
076:                            .getSQLState());
077:                } catch (Exception e) {
078:                    throw new SQLException(e.getMessage());
079:                }
080:            }
081:
082:            /**
083:             * Get connection by specific user name and password, the connection is not pooled by
084:             * Connection Pool
085:             * @param username
086:             * @param password
087:             * @return
088:             * @throws SQLException
089:             */
090:            public Connection getConnection(String username, String password)
091:                    throws SQLException {
092:                return DriverManager.getConnection(dbURL, username, password);
093:
094:            }
095:
096:            /**
097:             * Get logger writer
098:             * @return
099:             * @throws SQLException
100:             */
101:            public PrintWriter getLogWriter() throws SQLException {
102:                return DriverManager.getLogWriter();
103:            }
104:
105:            /**
106:             *
107:             * @param out
108:             * @throws SQLException
109:             */
110:            public void setLogWriter(PrintWriter out) throws SQLException {
111:                DriverManager.setLogWriter(out);
112:            }
113:
114:            /**
115:             *
116:             * @param seconds
117:             * @throws SQLException
118:             */
119:            public void setLoginTimeout(int seconds) throws SQLException {
120:                DriverManager.setLoginTimeout(seconds);
121:            }
122:
123:            /**
124:             *
125:             * @return
126:             * @throws SQLException
127:             */
128:            public int getLoginTimeout() throws SQLException {
129:                return DriverManager.getLoginTimeout();
130:            }
131:
132:            /**
133:             * Method that inherited from Referenceable.
134:             * @return
135:             * @throws javax.naming.NamingException
136:             */
137:            public Reference getReference() throws NamingException {
138:                Reference ref = new Reference(getClass().getName(), getClass()
139:                        .getName(), null);
140:                ref.add(new StringRefAddr("dbDriver", dbDriver));
141:                ref.add(new StringRefAddr("dbURL", dbURL));
142:                ref.add(new StringRefAddr("user", user));
143:                ref.add(new StringRefAddr("password", password));
144:                return ref;
145:            }
146:
147:            /**
148:             * Method that inherited from ObjectFactory.
149:             * @param obj
150:             * @param name
151:             * @param nameCtx
152:             * @param environment
153:             * @return
154:             * @throws Exception
155:             */
156:            public Object getObjectInstance(Object obj, Name name,
157:                    Context nameCtx, Hashtable environment) throws Exception {
158:                Object result = null;
159:                Reference ref = (Reference) obj;
160:                if (ref.getClassName().equals(getClass().getName())) {
161:                    String _dbDriver = (String) ref.get("dbDriver")
162:                            .getContent();
163:                    String _dbURL = (String) ref.get("dbURL").getContent();
164:                    String _user = (String) ref.get("user").getContent();
165:                    String _password = (String) ref.get("password")
166:                            .getContent();
167:                    PoolDataSource pds = new PoolDataSource(_dbDriver, _dbURL,
168:                            _user, _password);
169:                    result = pds;
170:                }
171:                return result;
172:            }
173:
174:            public String getDbDriver() {
175:                return dbDriver;
176:            }
177:
178:            public void setDbDriver(String dbDriver) {
179:                this .dbDriver = dbDriver;
180:                pool.setDbDriver(dbDriver);
181:            }
182:
183:            public String getDbURL() {
184:                return dbURL;
185:            }
186:
187:            public void setDbURL(String dbURL) {
188:                this .dbURL = dbURL;
189:                pool.setDbURL(dbURL);
190:            }
191:
192:            public String getUser() {
193:                return user;
194:            }
195:
196:            public void setUser(String user) {
197:                this .user = user;
198:                pool.setUser(user);
199:            }
200:
201:            public String getPassword() {
202:                return password;
203:            }
204:
205:            public void setPassword(String password) {
206:                this .password = password;
207:                pool.setPassword(password);
208:            }
209:
210:            protected void doInit() throws Exception {
211:                pool.init();
212:            }
213:
214:            protected void doDestroy() throws Exception {
215:                pool.destroy();
216:            }
217:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.