KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > conf > DBCPDataSourceFactory


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.conf;
21
22 import javax.sql.DataSource JavaDoc;
23
24 import org.apache.cayenne.util.ResourceLocator;
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 /**
29  * A DataSourceFactory that creates a connection pool based on Apache Jakarta <a
30  * HREF="http://jakarta.apache.org/commons/dbcp/">commons-dbcp</a>. If you are using this
31  * factory, commons-pool and commons-dbcp jars must be present in runtime. <p/>
32  * DBCPDataSourceFactory can be selected in the Modeler for a DataNode. DBCP pool
33  * configuration is done via a properties file that is specified in the modeler. See this
34  * <a HREF="http://cwiki.apache.org/CAYDOC/DBCPDataSourceFactory">wiki page</a> for the
35  * list of supported properties.
36  *
37  * @since 1.2
38  * @author Andrus Adamchik
39  */

40 public class DBCPDataSourceFactory implements DataSourceFactory {
41
42     private static final Log logger = LogFactory.getLog(DBCPDataSourceFactory.class);
43
44     /**
45      * @deprecated since 2.0 - this information is now private.
46      */

47     public static final String JavaDoc PROPERTY_PREFIX = "cayenne.dbcp.";
48
49     /**
50      * @deprecated since 2.0 - this information is now private.
51      */

52     public static final String JavaDoc PS_PROPERTY_PREFIX = PROPERTY_PREFIX + "ps.";
53
54     protected Configuration parentConfiguration;
55
56     /**
57      * Stores parent configuration in an ivar, using it later to resolve resources.
58      */

59     public void initializeWithParentConfiguration(Configuration parentConfiguration) {
60         this.parentConfiguration = parentConfiguration;
61     }
62
63     /**
64      * Creates and returns a {{org.apache.commons.dbcp.PoolingDataSource}} instance.
65      */

66     public DataSource JavaDoc getDataSource(String JavaDoc location) throws Exception JavaDoc {
67
68         ResourceLocator resourceLocator;
69
70         if (parentConfiguration != null) {
71             resourceLocator = parentConfiguration.getResourceLocator();
72         }
73         else {
74             resourceLocator = new ResourceLocator();
75             resourceLocator.setSkipAbsolutePath(false);
76             resourceLocator.setSkipHomeDirectory(true);
77             resourceLocator.setSkipClasspath(false);
78             resourceLocator.setSkipCurrentDirectory(false);
79         }
80
81         DBCPDataSourceProperties properties = new DBCPDataSourceProperties(
82                 resourceLocator,
83                 location);
84
85         if (logger.isDebugEnabled()) {
86             logger.debug("DBCP Properties: " + properties.getProperties());
87         }
88
89         DBCPDataSourceBuilder builder = new DBCPDataSourceBuilder(properties);
90         return builder.createDataSource();
91     }
92 }
93
Popular Tags