KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > jpa > conf > DefaultDataSourceFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.jpa.conf;
21
22 import java.sql.SQLException JavaDoc;
23 import java.util.Properties JavaDoc;
24
25 import javax.naming.InitialContext JavaDoc;
26 import javax.naming.NamingException JavaDoc;
27 import javax.persistence.spi.PersistenceUnitInfo;
28 import javax.sql.DataSource JavaDoc;
29
30 import org.apache.cayenne.access.ConnectionLogger;
31 import org.apache.cayenne.access.QueryLogger;
32 import org.apache.cayenne.conn.PoolManager;
33 import org.apache.cayenne.jpa.JpaProviderException;
34 import org.apache.cayenne.jpa.Provider;
35 import org.apache.cayenne.util.Util;
36
37 /**
38  * A {@link JpaDataSourceFactory} that attempts to create a DataSource based on Cayenne
39  * provider-specific properties. If such properties are not present, a DataSource is
40  * obtained via JNDI.
41  * <p>
42  * Properties are specified in the corresponding section of the <em>persistence.xml</em>
43  * file. The following properties are supported:
44  * </p>
45  * <ul>
46  * <li>org.apache.cayenne.datasource.jdbc.driver - (required) JDBC driver class</li>
47  * <li>org.apache.cayenne.datasource.jdbc.url - (required) Database URL</li>
48  * <li>org.apache.cayenne.datasource.jdbc.username - Database login id</li>
49  * <li>org.apache.cayenne.datasource.jdbc.password - Database password</li>
50  * <li>org.apache.cayenne.datasource.jdbc.minConnections - (optional) Minimal pool size</li>
51  * <li>org.apache.cayenne.datasource..jdbc.maxConnections - (optional) Maximum pool size</li>
52  * </ul>
53  *
54  * @author Andrus Adamchik
55  */

56 public class DefaultDataSourceFactory implements JpaDataSourceFactory {
57
58     public DataSource JavaDoc getJtaDataSource(String JavaDoc name, PersistenceUnitInfo info) {
59         return getDataSource(name, info);
60     }
61
62     public DataSource JavaDoc getNonJtaDataSource(String JavaDoc name, PersistenceUnitInfo info) {
63         return getDataSource(name, info);
64     }
65
66     protected DataSource JavaDoc getJndiDataSource(String JavaDoc name, PersistenceUnitInfo info) {
67         if (name == null) {
68             return null;
69         }
70
71         try {
72             return (DataSource JavaDoc) new InitialContext JavaDoc().lookup(name);
73         }
74         catch (NamingException JavaDoc namingEx) {
75             return null;
76         }
77     }
78
79     protected DataSource JavaDoc getDataSource(String JavaDoc name, PersistenceUnitInfo info) {
80         if (name == null) {
81             return null;
82         }
83
84         DataSource JavaDoc ds = getCayenneDataSource(name, info.getProperties());
85         return ds != null ? ds : getJndiDataSource(name, info);
86     }
87
88     protected DataSource JavaDoc getCayenneDataSource(String JavaDoc name, Properties JavaDoc properties) {
89
90         String JavaDoc driverName = properties.getProperty(Provider.DATA_SOURCE_DRIVER_PROPERTY);
91         if (Util.isEmptyString(driverName)) {
92             return null;
93         }
94
95         String JavaDoc url = properties.getProperty(Provider.DATA_SOURCE_URL_PROPERTY);
96         if (Util.isEmptyString(url)) {
97             return null;
98         }
99
100         int minConnection;
101         try {
102             minConnection = Integer.parseInt(properties
103                     .getProperty(Provider.DATA_SOURCE_MIN_CONNECTIONS_PROPERTY));
104         }
105         catch (Exception JavaDoc e) {
106             minConnection = 1;
107         }
108
109         int maxConnection;
110         try {
111             maxConnection = Integer.parseInt(properties
112                     .getProperty(Provider.DATA_SOURCE_MAX_CONNECTIONS_PROPERTY));
113         }
114         catch (Exception JavaDoc e) {
115             maxConnection = 1;
116         }
117
118         // this code follows Cayenne DriverDataSourceFactory logic...
119
try {
120             return new PoolManager(
121                     driverName,
122                     url,
123                     minConnection,
124                     maxConnection,
125                     properties.getProperty(Provider.DATA_SOURCE_USER_NAME_PROPERTY),
126                     properties.getProperty(Provider.DATA_SOURCE_PASSWORD_PROPERTY),
127                     new ConnectionLogger());
128         }
129         catch (SQLException JavaDoc e) {
130             QueryLogger.logConnectFailure(e);
131             throw new JpaProviderException("Error creating connection pool", e);
132         }
133     }
134 }
135
Popular Tags