View Javadoc

1   /*--------------------------------------------------------------------------
2    *  Copyright 2008 utgenome.org
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   *--------------------------------------------------------------------------*/
16  //--------------------------------------
17  // utgb-core Project
18  //
19  // JDBCService.java
20  // Since: Jan 16, 2008
21  //
22  // $URL$ 
23  // $Author$
24  //--------------------------------------
25  package org.utgenome.gwt.utgb.server;
26  
27  import java.io.File;
28  
29  import org.utgenome.UTGBErrorCode;
30  import org.utgenome.UTGBException;
31  import org.utgenome.config.UTGBConfig;
32  import org.utgenome.config.UTGBConfig.Database;
33  import org.xerial.db.DBException;
34  import org.xerial.db.sql.ConnectionPoolImpl;
35  import org.xerial.db.sql.DatabaseAccess;
36  import org.xerial.db.sql.DatabaseAccessBase;
37  import org.xerial.db.sql.h2.H2Access;
38  import org.xerial.db.sql.mysql.MySQLAccess;
39  import org.xerial.db.sql.postgres.PostgresAccess;
40  import org.xerial.db.sql.sqlite.SQLiteAccess;
41  
42  /**
43   * A support class for acquiring JDBC connections.
44   * 
45   * @author leo
46   * 
47   */
48  public class JDBCService {
49  	public static DatabaseAccess getDatabaseAccess(UTGBConfig.Database db) throws UTGBException {
50  		return getDatabaseAccess(new File(".").getAbsolutePath(), db);
51  	}
52  
53  	public static DatabaseAccess getDatabaseAccess(String projectRootFolder, UTGBConfig.Database db) throws UTGBException {
54  		try {
55  			if (db.dbms.equals("sqlite")) {
56  				if (db.address.startsWith("/"))
57  					return new SQLiteAccess(db.address);
58  				else if (db.address.startsWith("file://"))
59  					return new SQLiteAccess(db.address.substring("file://".length()));
60  				else
61  					return new SQLiteAccess(projectRootFolder + "/" + db.address);
62  			}
63  			else if (db.dbms.equals("postgres")) {
64  				return new PostgresAccess(db.address, db.user, db.pass);
65  			}
66  			else if (db.dbms.equals("mysql")) {
67  				return new MySQLAccess(db.address, db.user, db.pass);
68  			}
69  			else if (db.dbms.equals("h2")) {
70  				return new H2Access(db.address);
71  			}
72  			else if (db.equals("hsqldb")) {
73  				return new HSQLDBAccess(db.address, db.user, db.pass);
74  			}
75  			else
76  				return new GenericDBAccess(db);
77  
78  		}
79  		catch (DBException e) {
80  			throw new UTGBException(UTGBErrorCode.DatabaseError, e);
81  		}
82  
83  	}
84  
85  	public static class GenericDBAccess extends DatabaseAccessBase {
86  		public GenericDBAccess(Database db) throws DBException {
87  			super(new ConnectionPoolImpl(db.driver, db.jdbcPrefix + db.address, db.user, db.pass));
88  		}
89  	}
90  
91  	public static class HSQLDBAccess extends DatabaseAccessBase {
92  		public static final String DRIVER_NAME = "org.hsqldb.jdbcDriver";
93  		public static final String ADDRESS_PREFIX = "jdbc:hsqldb:";
94  
95  		/**
96  		 * open memory database
97  		 * 
98  		 * @throws DBException
99  		 */
100 		public HSQLDBAccess() throws DBException {
101 			super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + "."));
102 		}
103 
104 		public HSQLDBAccess(String address) throws DBException {
105 			super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + address));
106 		}
107 
108 		public HSQLDBAccess(String address, String user, String pass) throws DBException {
109 			super(new ConnectionPoolImpl(DRIVER_NAME, ADDRESS_PREFIX + address, user, pass));
110 		}
111 
112 	}
113 
114 }