Source Code Cross Referenced for DBCPDatasourceComponent.java in  » Portal » jetspeed-2.1.3 » org » apache » jetspeed » components » 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 » Portal » jetspeed 2.1.3 » org.apache.jetspeed.components.datasource 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.jetspeed.components.datasource;
018:
019:        import java.sql.Connection;
020:        import java.sql.DriverManager;
021:
022:        import javax.sql.DataSource;
023:
024:        import org.apache.commons.dbcp.ConnectionFactory;
025:        import org.apache.commons.dbcp.DriverManagerConnectionFactory;
026:        import org.apache.commons.dbcp.PoolableConnectionFactory;
027:        import org.apache.commons.dbcp.PoolingDataSource;
028:        import org.apache.commons.logging.Log;
029:        import org.apache.commons.logging.LogFactory;
030:        import org.apache.commons.pool.ObjectPool;
031:        import org.apache.commons.pool.impl.GenericObjectPool;
032:
033:        /**
034:         * <p>
035:         * DBCPDatasourceComponent
036:         * </p>
037:         * 
038:         * 
039:         * @
040:         * @author <a href="mailto:weaver@apache.org">Scott T. Weaver</a>
041:         * @version $ $
042:         *
043:         */
044:        public class DBCPDatasourceComponent implements  DatasourceComponent {
045:
046:            private static final Log log = LogFactory
047:                    .getLog(DBCPDatasourceComponent.class);
048:
049:            protected PoolingDataSource dataSource;
050:            //protected DataSource dataSource;
051:
052:            private String user;
053:
054:            private String password;
055:
056:            private String driverName;
057:
058:            private String connectURI;
059:
060:            private int maxActive;
061:
062:            private int maxWait;
063:
064:            private byte whenExhausted;
065:
066:            private PoolableConnectionFactory dsConnectionFactory;
067:
068:            /**
069:             * 
070:             * Creates a simple commons DBCP connection pool using the following
071:             * parameters.
072:             * <p>
073:             * If you need to bind the datasource of this component to 
074:             * JNDI please @see org.apache.jetspeed.components.jndi.JNDIComponent
075:             * </p>
076:             * 
077:             * @param user User name that will be used to connect to the DB
078:             * @param password Password that will be used to connect to the DB
079:             * @param driverName Fully qualified driver to be used by the connection pool
080:             * @param connectURI Fully qualified URI to the DB.
081:             * @param maxActive Maximum active connection
082:             * @param maxWait if <code>whenExhausted</code> is set to GenericObjectPool.WHEN_EXHAUSTED_BLOCK
083:             * the length of time to block while waiting for a connection to become 
084:             * available.
085:             * @param whenExhausted GenericObjectPool.WHEN_EXHAUSTED_BLOCK, GenericObjectPool.WHEN_EXHAUSTED_GROW or
086:             * GenericObjectPool.WHEN_EXHAUSTED_FAIL. @see org.apache.commons.pooling.GenericObjectPool
087:             * for more information on these settings
088:             * @param autoCommit Whether or not this datasource will autocommit
089:             * @throws ClassNotFoundException If the <code>driverName</code> could not be
090:             * located within any classloaders.
091:             */
092:            public DBCPDatasourceComponent(String user, String password,
093:                    String driverName, String connectURI, int maxActive,
094:                    int maxWait, byte whenExhausted, boolean autoCommit)
095:
096:            {
097:
098:                log.info("Setting up data source pooling for " + driverName);
099:
100:                log.info("Max active connnections set to: " + maxActive);
101:
102:                log.info("Pool is set to \"" + whenExhausted
103:                        + "\" when all connections are exhausted.");
104:
105:                this .user = user;
106:                this .password = password;
107:                this .driverName = driverName;
108:                this .connectURI = connectURI;
109:                this .maxActive = maxActive;
110:                this .maxWait = maxWait;
111:            }
112:
113:            /**
114:             * 
115:             * <p>
116:             * getDatasource
117:             * </p>
118:             * 
119:             * <p>
120:             *   returns the datasource created by this component
121:             * </p>
122:             * @return
123:             *
124:             */
125:            public DataSource getDatasource() {
126:                return dataSource;
127:            }
128:
129:            /** 
130:             * <p>
131:             * start
132:             * </p>
133:             * 
134:             * @see org.picocontainer.Startable#start()
135:             * 
136:             */
137:            public void start() {
138:
139:                try {
140:                    log.info("Attempting to start DBCPCDatasourceComponent.");
141:                    Class.forName(driverName);
142:
143:                    // Validate the connection before we go any further
144:                    try {
145:                        Connection conn = DriverManager.getConnection(
146:                                connectURI, user, password);
147:                        conn.close();
148:                    } catch (Exception e) {
149:                        log.error(
150:                                "Unable to obtain a connection database via URI: "
151:                                        + connectURI, e);
152:                        throw e;
153:                    }
154:
155:                    ObjectPool connectionPool = new GenericObjectPool(null,
156:                            maxActive, whenExhausted, maxWait);
157:
158:                    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(
159:                            connectURI, user, password);
160:
161:                    dsConnectionFactory = new PoolableConnectionFactory(
162:                            connectionFactory, connectionPool, null, null,
163:                            false, true);
164:
165:                    dataSource = new PoolingDataSource(connectionPool);
166:
167:                    log.info("DBCPCDatasourceComponent successfuly started!");
168:                } catch (Throwable e) {
169:
170:                    String msg = "Unable to start DBCPCDatasourceComponent: "
171:                            + e.toString();
172:                    log.error(msg, e);
173:                    throw new IllegalStateException(msg);
174:                }
175:            }
176:
177:            /** 
178:             * <p>
179:             * stop
180:             * </p>
181:             * 
182:             * @see org.picocontainer.Startable#stop()
183:             * 
184:             */
185:            public void stop() {
186:                try {
187:                    dsConnectionFactory.getPool().close();
188:                } catch (Exception e) {
189:                    IllegalStateException ise = new IllegalStateException(
190:                            "Unable to sfaely shutdown the DBCPConnection pool: "
191:                                    + e.toString());
192:                    ise.initCause(e);
193:                }
194:            }
195:
196:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.