Source Code Cross Referenced for StandaloneConnectionFactory.java in  » Inversion-of-Control » carbon » org » sape » carbon » services » sql » connection » 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 » Inversion of Control » carbon » org.sape.carbon.services.sql.connection 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * The contents of this file are subject to the Sapient Public License
003:         * Version 1.0 (the "License"); you may not use this file except in compliance
004:         * with the License. You may obtain a copy of the License at
005:         * http://carbon.sf.net/License.html.
006:         *
007:         * Software distributed under the License is distributed on an "AS IS" basis,
008:         * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
009:         * the specific language governing rights and limitations under the License.
010:         *
011:         * The Original Code is The Carbon Component Framework.
012:         *
013:         * The Initial Developer of the Original Code is Sapient Corporation
014:         *
015:         * Copyright (C) 2003 Sapient Corporation. All Rights Reserved.
016:         */
017:
018:        package org.sape.carbon.services.sql.connection;
019:
020:        import java.sql.Connection;
021:        import java.sql.Driver;
022:        import java.sql.SQLException;
023:        import java.util.Map;
024:        import java.util.Properties;
025:
026:        import org.sape.carbon.core.component.ComponentConfiguration;
027:        import org.sape.carbon.core.component.lifecycle.Configurable;
028:        import org.sape.carbon.core.component.lifecycle.StateTransitionException;
029:
030:        import org.apache.commons.logging.Log;
031:        import org.apache.commons.logging.LogFactory;
032:
033:        /**
034:         * A Factory for JDBC Connections. Hides interaction with the JDBC driver.
035:         * Maintains a reference to a JDBC Driver, plus connection info (userId,
036:         * password, URL). Note that the Driver instance is discarded and recreated
037:         * each time the component is configured.
038:         *
039:         * Copyright 2002 Sapient
040:         * @see StandaloneConnectionFactoryConfiguration
041:         *
042:         * @since carbon 1.0
043:         * @author Chris Herron, March 2002
044:         * @version $Revision: 1.13 $($Author: araman $ / $Date: 2003/07/29 08:01:30 $)
045:         */
046:        public class StandaloneConnectionFactory implements  ConnectionFactory,
047:                Configurable {
048:
049:            /**
050:             * The environment key for the username to authenticate a database
051:             * connection with.
052:             */
053:            protected static final String USER_KEY = "user";
054:
055:            /**
056:             * The environment key for the password to authenticate a database
057:             * connection with.
058:             */
059:            protected static final String PASSWORD_KEY = "password";
060:
061:            /**
062:             * A reference to the database driver.
063:             * This gets reset after each call to <code>configure(...)</code>.
064:             */
065:            protected Driver driver = null;
066:
067:            /**
068:             * The JDBC connection URL for the database.
069:             */
070:            protected String dbUrl = null;
071:
072:            /**
073:             * The properties passed to the JDBC driver to obtain a connection.
074:             * Uses the keys "user" and "password" for the user and password values.
075:             * (Constants defined below).
076:             */
077:            protected Properties dbConnectionProperties;
078:
079:            /**
080:             * Provides a handle to Apache-commons logger
081:             */
082:            private Log log = LogFactory.getLog(this .getClass());
083:
084:            /**
085:             * Uses the configured JDBC Driver and connection parameters to return a
086:             * JDBC connection.
087:             *
088:             * @return Connection specified by this factory
089:             * @throws SQLException indicates an error creating the connection
090:             *         to the datasource
091:             */
092:            public Connection getConnection() throws SQLException {
093:                Connection con = driver.connect(dbUrl, dbConnectionProperties);
094:                return con;
095:            }
096:
097:            /**
098:             * Configure the component. This is preceded and followed by the suspend and
099:             * resume operations if they are available on the component.
100:             *
101:             * @param configuration A StandaloneConnectionFactoryConfiguration instance
102:             */
103:            public void configure(ComponentConfiguration configuration) {
104:
105:                try {
106:                    StandaloneConnectionFactoryConfiguration config = (StandaloneConnectionFactoryConfiguration) configuration;
107:                    Class driverClass = config.getDriverClass();
108:                    String dbUserId = config.getDbUserId();
109:                    String dbPassword = config.getDbPassword();
110:                    this .dbUrl = config.getDbUrl();
111:                    this .driver = (Driver) driverClass.newInstance();
112:
113:                    if (null == driver) {
114:                        throw new StateTransitionException(this .getClass(),
115:                                "Configuration Failed: Could not instantiate a Driver");
116:                    }
117:                    Properties props = new Properties();
118:
119:                    //Intentionally omitted check for empty string here since an error
120:                    //will be reported when the connection is attempted
121:                    if (null != dbUserId) {
122:                        props.put(StandaloneConnectionFactory.USER_KEY,
123:                                dbUserId);
124:                        if (null != dbPassword) {
125:                            props.put(StandaloneConnectionFactory.PASSWORD_KEY,
126:                                    dbPassword);
127:                        }
128:                    } else {
129:                        if (log.isInfoEnabled()) {
130:                            log
131:                                    .info("No username provided. Logins will be anonymous. "
132:                                            + "([user] and [password] keys/entries omitted from "
133:                                            + "connection properties object");
134:                        }
135:                    }
136:
137:                    //process additional properties set for the connection
138:                    Map connectionProperties = config.getConnectionProperties();
139:                    if (connectionProperties == null
140:                            || connectionProperties.size() == 0) {
141:                        if (log.isInfoEnabled()) {
142:                            log
143:                                    .info("No additional properties for the connection have been specified");
144:                        }
145:                    } else {
146:                        //properties have been specified, iterate and add it to the list
147:                        props.putAll(connectionProperties);
148:                        if (log.isInfoEnabled()) {
149:                            log.info(connectionProperties.size()
150:                                    + " additional properties "
151:                                    + connectionProperties
152:                                    + " for the connection has been set");
153:                        }
154:                    }
155:
156:                    this .dbConnectionProperties = props;
157:                } catch (InstantiationException ie) {
158:                    throw new StateTransitionException(
159:                            this .getClass(),
160:                            "Configuration Failed: Could not instantiate a Driver",
161:                            ie);
162:                } catch (IllegalAccessException ae) {
163:                    throw new StateTransitionException(
164:                            this .getClass(),
165:                            "Configuration Failed: Could not instantiate a Driver",
166:                            ae);
167:                }
168:            }
169:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.