Source Code Cross Referenced for DriverManagerDataSource.java in  » Search-Engine » compass-2.0 » org » compass » gps » device » jdbc » 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 » Search Engine » compass 2.0 » org.compass.gps.device.jdbc.datasource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2004-2006 the original author or authors.
003:         * 
004:         * Licensed under the Apache License, Version 2.0 (the "License");
005:         * you may not use this file except in compliance with the License.
006:         * You may obtain a copy of the License at
007:         * 
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         * 
010:         * Unless required by applicable law or agreed to in writing, software
011:         * distributed under the License is distributed on an "AS IS" BASIS,
012:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         * See the License for the specific language governing permissions and
014:         * limitations under the License.
015:         */
016:
017:        package org.compass.gps.device.jdbc.datasource;
018:
019:        import java.sql.Connection;
020:        import java.sql.DriverManager;
021:        import java.sql.SQLException;
022:
023:        import org.compass.gps.device.jdbc.CannotGetJdbcConnectionException;
024:
025:        /**
026:         * Simple implementation of the standard JDBC DataSource interface, configuring
027:         * a plain old JDBC Driver via bean properties, and returning a new Connection
028:         * for every <code>getConnection</code> call.
029:         * <p>
030:         * Useful for test or standalone environments outside of a J2EE container.
031:         * Pool-assuming <code>Connection.close()</code> calls will simply close the
032:         * Connection, so any DataSource-aware persistence code should work.
033:         * <p>
034:         * In a J2EE container, it is recommended to use a JNDI DataSource provided by
035:         * the container.
036:         * <p>
037:         * If you need a "real" connection pool outside of a J2EE container, consider <a
038:         * href="http://jakarta.apache.org/commons/dbcp">Apache's Jakarta Commons DBCP</a>.
039:         * Its BasicDataSource is a full connection pool bean, supporting the same basic
040:         * properties as this class plus specific settings.
041:         * <p>
042:         * Taken from Spring.
043:         * 
044:         * @author kimchy
045:         */
046:        public class DriverManagerDataSource extends AbstractDataSource {
047:
048:            private String driverClassName;
049:
050:            private String url;
051:
052:            private String username;
053:
054:            private String password;
055:
056:            /**
057:             * Constructor for bean-style configuration.
058:             */
059:            public DriverManagerDataSource() {
060:            }
061:
062:            /**
063:             * Create a new DriverManagerDataSource with the given standard
064:             * DriverManager parameters.
065:             * 
066:             * @param driverClassName
067:             *            the JDBC driver class name
068:             * @param url
069:             *            the JDBC URL to use for accessing the DriverManager
070:             * @param username
071:             *            the JDBC username to use for accessing the DriverManager
072:             * @param password
073:             *            the JDBC password to use for accessing the DriverManager
074:             * @see java.sql.DriverManager#getConnection(String, String, String)
075:             */
076:            public DriverManagerDataSource(String driverClassName, String url,
077:                    String username, String password)
078:                    throws CannotGetJdbcConnectionException {
079:                setDriverClassName(driverClassName);
080:                setUrl(url);
081:                setUsername(username);
082:                setPassword(password);
083:            }
084:
085:            /**
086:             * Create a new DriverManagerDataSource with the given standard
087:             * DriverManager parameters.
088:             * 
089:             * @param url
090:             *            the JDBC URL to use for accessing the DriverManager
091:             * @param username
092:             *            the JDBC username to use for accessing the DriverManager
093:             * @param password
094:             *            the JDBC password to use for accessing the DriverManager
095:             * @see java.sql.DriverManager#getConnection(String, String, String)
096:             */
097:            public DriverManagerDataSource(String url, String username,
098:                    String password) throws CannotGetJdbcConnectionException {
099:                setUrl(url);
100:                setUsername(username);
101:                setPassword(password);
102:            }
103:
104:            /**
105:             * Create a new DriverManagerDataSource with the given JDBC URL, not
106:             * specifying a username or password for JDBC access.
107:             * 
108:             * @param url
109:             *            the JDBC URL to use for accessing the DriverManager
110:             * @see java.sql.DriverManager#getConnection(String)
111:             */
112:            public DriverManagerDataSource(String url)
113:                    throws CannotGetJdbcConnectionException {
114:                setUrl(url);
115:            }
116:
117:            /**
118:             * Set the JDBC driver class name. This driver will get initialized on
119:             * startup, registering itself with the JDK's DriverManager.
120:             * <p>
121:             * Alternatively, consider initializing the JDBC driver yourself before
122:             * instantiating this DataSource.
123:             * 
124:             * @see Class#forName(String)
125:             * @see java.sql.DriverManager#registerDriver(java.sql.Driver)
126:             */
127:            public void setDriverClassName(String driverClassName)
128:                    throws CannotGetJdbcConnectionException {
129:                this .driverClassName = driverClassName;
130:                try {
131:                    Class.forName(this .driverClassName, true, Thread
132:                            .currentThread().getContextClassLoader());
133:                } catch (ClassNotFoundException ex) {
134:                    throw new CannotGetJdbcConnectionException(
135:                            "Could not load JDBC driver class ["
136:                                    + this .driverClassName + "]", ex);
137:                }
138:                if (log.isInfoEnabled()) {
139:                    log.info("Loaded JDBC driver [" + this .driverClassName
140:                            + "]");
141:                }
142:            }
143:
144:            /**
145:             * Return the JDBC driver class name, if any.
146:             */
147:            public String getDriverClassName() {
148:                return driverClassName;
149:            }
150:
151:            /**
152:             * Set the JDBC URL to use for accessing the DriverManager.
153:             * 
154:             * @see java.sql.DriverManager#getConnection(String, String, String)
155:             */
156:            public void setUrl(String url) {
157:                this .url = url;
158:            }
159:
160:            /**
161:             * Return the JDBC URL to use for accessing the DriverManager.
162:             */
163:            public String getUrl() {
164:                return url;
165:            }
166:
167:            /**
168:             * Set the JDBC username to use for accessing the DriverManager.
169:             * 
170:             * @see java.sql.DriverManager#getConnection(String, String, String)
171:             */
172:            public void setUsername(String username) {
173:                this .username = username;
174:            }
175:
176:            /**
177:             * Return the JDBC username to use for accessing the DriverManager.
178:             */
179:            public String getUsername() {
180:                return username;
181:            }
182:
183:            /**
184:             * Set the JDBC password to use for accessing the DriverManager.
185:             * 
186:             * @see java.sql.DriverManager#getConnection(String, String, String)
187:             */
188:            public void setPassword(String password) {
189:                this .password = password;
190:            }
191:
192:            /**
193:             * Return the JDBC password to use for accessing the DriverManager.
194:             */
195:            public String getPassword() {
196:                return password;
197:            }
198:
199:            /**
200:             * This implementation delegates to
201:             * <code>getConnectionFromDriverManager</code>, using the default
202:             * username and password of this DataSource.
203:             * 
204:             * @see #getConnectionFromDriverManager()
205:             */
206:            public Connection getConnection() throws SQLException {
207:                return getConnectionFromDriverManager();
208:            }
209:
210:            /**
211:             * This implementation delegates to
212:             * <code>getConnectionFromDriverManager</code>, using the given username
213:             * and password.
214:             * 
215:             * @see #getConnectionFromDriverManager(String, String, String)
216:             */
217:            public Connection getConnection(String username, String password)
218:                    throws SQLException {
219:                return getConnectionFromDriverManager(getUrl(), username,
220:                        password);
221:            }
222:
223:            /**
224:             * Get a Connection from the DriverManager, using the default username and
225:             * password of this DataSource.
226:             * 
227:             * @see #getConnectionFromDriverManager(String, String, String)
228:             */
229:            protected Connection getConnectionFromDriverManager()
230:                    throws SQLException {
231:                return getConnectionFromDriverManager(getUrl(), getUsername(),
232:                        getPassword());
233:            }
234:
235:            /**
236:             * Getting a connection using the nasty static from DriverManager is
237:             * extracted into a protected method to allow for easy unit testing.
238:             * 
239:             * @see java.sql.DriverManager#getConnection(String, String, String)
240:             */
241:            protected Connection getConnectionFromDriverManager(String url,
242:                    String username, String password) throws SQLException {
243:
244:                if (log.isDebugEnabled()) {
245:                    log.debug("Creating new JDBC connection to [" + url + "]");
246:                }
247:                return DriverManager.getConnection(url, username, password);
248:            }
249:
250:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.