Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *    Purpose: A Class that implements database utility methods 
4
 *  Copyright: 2008 Regents of the University of California and the
5
 *             National Center for Ecological Analysis and Synthesis
6
 *    Authors: Michael Daigle
7
 * 
8
 *   '$Author: daigle $'
9
 *     '$Date: 2008-08-22 16:23:38 -0700 (Fri, 22 Aug 2008) $'
10
 * '$Revision: 4297 $'
11
 *
12
 * This program is free software; you can redistribute it and/or modify
13
 * it under the terms of the GNU General Public License as published by
14
 * the Free Software Foundation; either version 2 of the License, or
15
 * (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU General Public License
23
 * along with this program; if not, write to the Free Software
24
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25
 */
26

    
27
package edu.ucsb.nceas.metacat.service;
28

    
29
import org.apache.log4j.Logger;
30

    
31
import edu.ucsb.nceas.dbadapter.AbstractDatabase;
32

    
33
public class DatabaseService extends BaseService {
34
	
35
	private static DatabaseService databaseService = null;
36
	private static AbstractDatabase dbAdapter;	
37
	private static Logger logMetacat = Logger.getLogger(DatabaseService.class);
38

    
39
	/**
40
	 * private constructor since this is a singleton
41
	 */
42
	private DatabaseService() {
43
    	// Determine our db adapter class and create an instance of that class
44
        try {
45
            dbAdapter = (AbstractDatabase) createObject(PropertyService.getProperty("database.adapter"));
46
        } catch (Exception e) {
47
            logMetacat.error("Could not create dbAdaptor" + e.getMessage());
48
            e.printStackTrace();
49
        }
50
	}
51
	
52
	/**
53
	 * Get the single instance of DatabaseService.
54
	 * 
55
	 * @return the single instance of DatabaseService
56
	 */
57
	public static DatabaseService getInstance() {
58
		if (databaseService == null) {
59
			databaseService = new DatabaseService();
60
		}
61
		return databaseService;
62
	}
63
	
64
	public boolean refreshable() {
65
		return false;
66
	}
67
	
68
	protected void doRefresh() {
69
		return;
70
	}
71
	
72
    /**
73
	 * Instantiate a class using the name of the class at runtime
74
	 *
75
	 * @param className the fully qualified name of the class to instantiate
76
	 */
77
	public static Object createObject(String className) throws Exception {
78

    
79
		Object object = null;
80
		try {
81
			Class classDefinition = Class.forName(className);
82
			object = classDefinition.newInstance();
83
		} catch (InstantiationException e) {
84
			throw e;
85
		} catch (IllegalAccessException e) {
86
			throw e;
87
		} catch (ClassNotFoundException e) {
88
			throw e;
89
		}
90
		return object;
91
	}
92
	
93
	/**
94
	 * gets the database adapter
95
	 * 
96
	 * @return AbstractDatabase object for this application's database adapter.
97
	 */
98
	public static AbstractDatabase getDBAdapter() {
99
		return dbAdapter;
100
	}
101

    
102
}
(2-2/9)