Project

General

Profile

« Previous | Next » 

Revision 4245

use factory method for creating database connection pool
(stoopid subclipse!)

View differences:

src/edu/ucsb/nceas/metacat/dataquery/DatabaseConnectionPoolFactory.java
1
/**
2
 *    '$RCSfile: DatabaseConnectionPoolFactory.java,v $'
3
 *
4
 *     '$Author: leinfelder $'
5
 *       '$Date: 2007/10/05 21:10:17 $'
6
 *   '$Revision: 1.1 $'
7
 *
8
 *  For Details: http://kepler.ecoinformatics.org
9
 *
10
 * Copyright (c) 2003 The Regents of the University of California.
11
 * All rights reserved.
12
 * 
13
 * Permission is hereby granted, without written agreement and without
14
 * license or royalty fees, to use, copy, modify, and distribute this
15
 * software and its documentation for any purpose, provided that the
16
 * above copyright notice and the following two paragraphs appear in
17
 * all copies of this software.
18
 * 
19
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
20
 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
21
 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
22
 * IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY
23
 * OF SUCH DAMAGE.
24
 * 
25
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
26
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
28
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY
29
 * OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT,
30
 * UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
31
 */
32
package edu.ucsb.nceas.metacat.dataquery;
33

  
34
import edu.ucsb.nceas.metacat.service.PropertyService;
35

  
36
import java.sql.Connection;
37
import java.sql.SQLException;
38

  
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.ecoinformatics.datamanager.database.ConnectionNotAvailableException;
42
import org.ecoinformatics.datamanager.database.DatabaseConnectionPoolInterface;
43

  
44
/**
45
 * This class provides an implementation of DataConnetionPoolInterface 
46
 * Implementation and database information for this class will be read from property file.
47
 * 
48
 * @author leinfelder
49
 * 
50
 */
51
public class DatabaseConnectionPoolFactory {
52
	
53
	public static Log log = LogFactory.getLog(DatabaseConnectionPoolFactory.class);
54
	
55
	private static String implementationClass = null;
56
		
57
	static {
58
		loadOptions();
59
	}
60
	
61
	/**
62
	 * Constructor. Loading database parameter from property file
63
	 * 
64
	 */
65
	public DatabaseConnectionPoolFactory() {
66
	}
67

  
68
	/**
69
	 * Loads Data Manager options from a configuration file.
70
	 */
71
	private static void loadOptions() {
72
		try {
73
			implementationClass = PropertyService.getProperty("datamanager.implementation");
74
		} 
75
		catch (Exception e) {
76
			log.error("Error in loading options: " + e.getMessage());
77
		}
78
	}
79

  
80
	public static DatabaseConnectionPoolInterface getDatabaseConnectionPoolInterface() {
81
		DatabaseConnectionPoolInterface instance = null;
82
		try {
83
			instance = 
84
				(DatabaseConnectionPoolInterface) Class.forName(implementationClass).newInstance();
85
		} catch (Exception e) {
86
			log.error(e.getMessage() 
87
					+ ": could not create DatabaseConnectionPoolInterface implementation: " 
88
					+ implementationClass);
89
			e.printStackTrace();
90
		} 
91
		
92
		return instance;
93

  
94
	}
95

  
96
	public static void main(String arg[]) {
97
		Connection conn = null;
98
		try {
99
			conn = DatabaseConnectionPoolFactory.getDatabaseConnectionPoolInterface().getConnection();
100
			log.debug("conn=" + conn);
101
			
102
		} catch (SQLException e) {
103
			// TODO Auto-generated catch block
104
			e.printStackTrace();
105
		} catch (ConnectionNotAvailableException e) {
106
			// TODO Auto-generated catch block
107
			e.printStackTrace();
108
		}
109
		finally {
110
			try {
111
				conn.close();
112
			} catch (SQLException e) {
113
				// TODO Auto-generated catch block
114
				e.printStackTrace();
115
			}
116
		}
117
	}
118
}

Also available in: Unified diff