Project

General

Profile

Download (5.54 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 EDIT
3
* European Distributed Institute of Taxonomy 
4
* http://www.e-taxonomy.eu
5
* 
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9

    
10
package eu.etaxonomy.cdm.database;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14

    
15
import javax.sql.DataSource;
16

    
17
import org.apache.log4j.Logger;
18

    
19
import eu.etaxonomy.cdm.database.types.H2DatabaseType;
20
import eu.etaxonomy.cdm.database.types.HSqlDbDatabaseType;
21
import eu.etaxonomy.cdm.database.types.IDatabaseType;
22
import eu.etaxonomy.cdm.database.types.MySQLDatabaseType;
23
import eu.etaxonomy.cdm.database.types.OdbcDatabaseType;
24
import eu.etaxonomy.cdm.database.types.OracleDatabaseType;
25
import eu.etaxonomy.cdm.database.types.PostgreSQLDatabaseType;
26
import eu.etaxonomy.cdm.database.types.SqlServer2005DatabaseType;
27
import eu.etaxonomy.cdm.database.types.SybaseDatabaseType;
28

    
29
/**
30
 * @author a.mueller
31
 *
32
 */
33
public enum DatabaseTypeEnum {
34
	HSqlDb(1),
35
	MySQL(2),
36
	ODBC(3),
37
	PostgreSQL(4),
38
	Oracle(5),
39
	//SqlServer2000(6),
40
	SqlServer2005(7),
41
	Sybase(8),
42
	H2(9)
43
	;
44

    
45
	/**
46
	 * 
47
	 */
48
	private static final String P6SPY_DRIVER_CLASS_NAME = "com.p6spy.engine.spy.P6SpyDriver";
49
	private boolean useP6Spy = false;
50
	
51
	
52
	/**
53
	 * @return the useP6Spy
54
	 */
55
	public boolean isUseP6Spy() {
56
		return useP6Spy;
57
	}
58

    
59
	/**
60
	 * @param useP6Spy the useP6Spy to set
61
	 */
62
	public void setUseP6Spy(boolean useP6Spy) {
63
		this.useP6Spy = useP6Spy;
64
	}
65

    
66
	/**
67
	 * Constructor
68
	 * @param i
69
	 */
70
	private DatabaseTypeEnum(int i) {
71
		switch(i)
72
        {
73
        	case 1:
74
        		this.dbType = new HSqlDbDatabaseType(); break;
75
        	case 2:
76
        		this.dbType = new MySQLDatabaseType(); break;
77
        	case 3:
78
        		this.dbType = new OdbcDatabaseType(); break;
79
        	case 4:
80
            	this.dbType = new PostgreSQLDatabaseType(); break;
81
        	case 5:
82
             	this.dbType = new OracleDatabaseType(); break;
83
//            case 6:
84
//            	this.dbType = new SqlServer2000DatabaseType(); break;
85
            case 7:
86
            	this.dbType = new SqlServer2005DatabaseType(); break;
87
            case 8:
88
            	this.dbType = new SybaseDatabaseType(); break;
89
            case 9:
90
            	this.dbType = new H2DatabaseType(); break;
91
            default:
92
                //TODO Exception
93
        }
94
	}
95
	
96
	public IDatabaseType getDatabaseType(){
97
		return dbType;
98
	}
99
	
100
 	//Logger
101
	private static final Logger logger = Logger.getLogger(DatabaseTypeEnum.class);
102
	protected IDatabaseType dbType;
103
	
104
	   
105
    /**
106
     * @return
107
     */
108
    public String getName(){
109
    	return dbType.getName();
110
    }
111
    
112
	/**
113
	 * @return
114
	 */
115
	public String getDriverClassName(){
116
		if(useP6Spy){
117
			return P6SPY_DRIVER_CLASS_NAME;
118
			
119
		} else {
120
			return dbType.getClassString();			
121
		}
122
	}
123
    
124
	/**
125
	 * Returns the DataSource class that that the datasource needs to create a spring bean
126
	 * @return the DataSource class
127
	 */
128
	public Class<? extends DataSource> getDataSourceClass(){
129
		return dbType.getDataSourceClass();
130
	}
131
	
132
	/**
133
	 * @return
134
	 */
135
	public String getUrl(){
136
		return dbType.getUrlString();
137
	}
138
	
139
	/**
140
	 * @return
141
	 */
142
	public String getHibernateDialect(){
143
		return dbType.getHibernateDialect();
144
	}
145
	   
146
    /**
147
     * @return
148
     */
149
    public int getDefaultPort(){
150
    	return dbType.getDefaultPort();
151
    }
152

    
153
	/**
154
     * returns the connection string 
155
     * @param server the server, e.g. IP-Address
156
     * @param database the database name on the server (e.g. "testDB")
157
     * @param port the port number
158
     * @return the connection string
159
     */
160
    public String getConnectionString(ICdmDataSource cdmDataSource){
161
    	String result = dbType.getConnectionString(cdmDataSource);
162
    	logger.debug("Connection String: " + result);	
163
        return result;
164
    }
165
    
166

    
167

    
168
	/**
169
     * Returns the Name of the initialization method to be used when a hibernate datasource is created for this database
170
	 * @return String name of the init method
171
	 */
172
    public String getInitMethod(){
173
    	String result = dbType.getInitMethod();
174
    	logger.debug("InitMethod: " + result);	
175
        return result;
176
    }
177
    
178
	/**
179
	 * Returns the Name of the destroying method to be used when a hibernate datasource representing this database is destroyed
180
	 * @return String name of the destroy method
181
	 */
182
    public String getDestroyMethod(){
183
    	String result = dbType.getDestroyMethod();
184
    	logger.debug("DestroyMethod: " + result);	
185
        return result;
186
    }
187
    
188
    /**
189
     * Returns a List of all available DatabaseEnums.
190
     * @return List of DatabaseEnums
191
     */
192
    public static List<DatabaseTypeEnum> getAllTypes(){
193
    	List<DatabaseTypeEnum> result = new ArrayList<DatabaseTypeEnum>();
194
    	for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
195
    		result.add(dbEnum);
196
    	}
197
    	return result;
198
    }
199

    
200
    /**
201
     * Returns the DatabaseTypeEnum to a given DriverClass
202
     * @param strDriverClass
203
     * @return the according DatabaseTypeEnum. Null if the driver class does not exist.
204
     */
205
    public static DatabaseTypeEnum getDatabaseEnumByDriverClass(String strDriverClass){
206
    	for (DatabaseTypeEnum dbEnum : DatabaseTypeEnum.values()){
207
    		if (dbEnum.getDriverClassName().equals(strDriverClass)){
208
    			return dbEnum;
209
    		}
210
    	}
211
    	logger.warn("Unknown driver class " + strDriverClass==null ? "null" : strDriverClass);
212
    	return null;
213
    }
214
    
215
    
216

    
217
 
218
}
219

    
(9-9/20)