Source Code Cross Referenced for TestJDBCDataSource.java in  » Wiki-Engine » JSPWiki » com » ecyrd » jspwiki » 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 » Wiki Engine » JSPWiki » com.ecyrd.jspwiki 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.ecyrd.jspwiki;
002:
003:        import java.io.File;
004:        import java.io.FileInputStream;
005:        import java.io.PrintWriter;
006:        import java.net.URL;
007:        import java.net.URLClassLoader;
008:        import java.security.AccessController;
009:        import java.security.PrivilegedAction;
010:        import java.sql.Connection;
011:        import java.sql.Driver;
012:        import java.sql.SQLException;
013:        import java.util.Properties;
014:
015:        import javax.sql.DataSource;
016:
017:        /**
018:         * Mock JDBC DataSource class that manages JDBC connections to a database whose
019:         * driver class, JDBC JAR location and connection details are specified in an
020:         * arbitrary propreties file. Gemerally, we pass on any exceptions encountered
021:         * as unchecked, since it means that the test case that references this class is
022:         * failing somehow.
023:         * @author Andrew R. Jaquith
024:         */
025:        public class TestJDBCDataSource implements  DataSource {
026:            private static Driver m_driver;
027:
028:            protected static final String PROPERTY_DRIVER_CLASS = "jdbc.driver.class";
029:
030:            protected static final String PROPERTY_DRIVER_JAR = "jdbc.driver.jar";
031:
032:            protected static final String PROPERTY_DRIVER_URL = "jdbc.driver.url";
033:
034:            protected static final String PROPERTY_USER_ID = "jdbc.user.id";
035:
036:            protected static final String PROPERTY_USER_PASSWORD = "jdbc.user.password";
037:
038:            protected String m_jdbcPassword = null;
039:
040:            protected String m_jdbcURL = null;
041:
042:            protected String m_jdbcUser = null;
043:
044:            protected int m_timeout = 0;
045:
046:            protected PrintWriter m_writer = null;
047:
048:            /**
049:             * Constructs a new instance of this class, using a supplied properties
050:             * File as the source for JDBC driver properties.
051:             * @param file the properties file containing JDBC properties
052:             * @throws Exception
053:             */
054:            public TestJDBCDataSource(File file) throws Exception {
055:                super ();
056:                initializeJDBC(file);
057:            }
058:
059:            /**
060:             * Returns a JDBC connection using the specified username and password.
061:             * @return the database connection
062:             * @see javax.sql.DataSource#getConnection()
063:             */
064:            public Connection getConnection() throws SQLException {
065:                return getConnection(m_jdbcUser, m_jdbcPassword);
066:            }
067:
068:            /**
069:             * Returns a JDBC connection to the database.
070:             * @return the database connection
071:             * @see javax.sql.DataSource#getConnection(java.lang.String,
072:             * java.lang.String)
073:             */
074:            public Connection getConnection(String username, String password)
075:                    throws SQLException {
076:                Properties connProperties = new Properties();
077:                connProperties.put("user", m_jdbcUser);
078:                connProperties.put("password", m_jdbcPassword);
079:                Connection connection = m_driver.connect(m_jdbcURL,
080:                        connProperties);
081:                return connection;
082:            }
083:
084:            /**
085:             * Returns the login timeout for the data source.
086:             * @return the login timeout, in seconds
087:             * @see javax.sql.DataSource#getLoginTimeout()
088:             */
089:            public int getLoginTimeout() throws SQLException {
090:                return m_timeout;
091:            }
092:
093:            /**
094:             * Returns the log writer for the data source.
095:             * @return the log writer
096:             * @see javax.sql.DataSource#getLogWriter()
097:             */
098:            public PrintWriter getLogWriter() throws SQLException {
099:                return m_writer;
100:            }
101:
102:            /**
103:             * Sets the login timeout for the data source. Doesn't do anything, really.
104:             * @param seconds the login timeout, in seconds
105:             * @see javax.sql.DataSource#setLoginTimeout(int)
106:             */
107:            public void setLoginTimeout(int seconds) throws SQLException {
108:                this .m_timeout = seconds;
109:            }
110:
111:            /**
112:             * Sets the log writer for the data source. Isn't used for anything, really.
113:             * @param out the log writer
114:             * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
115:             */
116:            public void setLogWriter(PrintWriter out) throws SQLException {
117:                this .m_writer = out;
118:            }
119:
120:            /**
121:             * Initialization method that reads a File, and attempts to locate and load
122:             * the JDBC driver from properties specified therein.
123:             * @throws SQLException
124:             * @param file the file containing the JDBC properties
125:             */
126:            protected void initializeJDBC(File file) throws Exception {
127:                // Load the properties JDBC properties file
128:                Properties properties;
129:                properties = new Properties();
130:                FileInputStream is = new FileInputStream(file);
131:                properties.load(is);
132:                is.close();
133:                m_jdbcURL = properties.getProperty(PROPERTY_DRIVER_URL);
134:                m_jdbcUser = properties.getProperty(PROPERTY_USER_ID);
135:                m_jdbcPassword = properties.getProperty(PROPERTY_USER_PASSWORD);
136:
137:                // Identifiy the class and JAR we need to load
138:                String clazz = properties.getProperty(PROPERTY_DRIVER_CLASS);
139:                String driverFile = properties.getProperty(PROPERTY_DRIVER_JAR);
140:
141:                // Construct an URL for loading the file
142:                final URL driverURL = new URL("file:" + driverFile);
143:
144:                // Load the driver using the sytem class loader
145:                final ClassLoader parent = ClassLoader.getSystemClassLoader();
146:                URLClassLoader loader = (URLClassLoader) AccessController
147:                        .doPrivileged(new PrivilegedAction() {
148:                            public Object run() {
149:                                return new URLClassLoader(
150:                                        new URL[] { driverURL }, parent);
151:                            }
152:                        });
153:                Class driverClass = loader.loadClass(clazz);
154:
155:                // Cache the driver
156:                m_driver = (Driver) driverClass.newInstance();
157:            }
158:
159:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.