chris@3: /*
chris@3:  *   SONEWS News Server
chris@3:  *   see AUTHORS for the list of contributors
chris@3:  *
chris@3:  *   This program is free software: you can redistribute it and/or modify
chris@3:  *   it under the terms of the GNU General Public License as published by
chris@3:  *   the Free Software Foundation, either version 3 of the License, or
chris@3:  *   (at your option) any later version.
chris@3:  *
chris@3:  *   This program is distributed in the hope that it will be useful,
chris@3:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
chris@3:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
chris@3:  *   GNU General Public License for more details.
chris@3:  *
chris@3:  *   You should have received a copy of the GNU General Public License
chris@3:  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
chris@3:  */
chris@3: package org.sonews.storage.impl;
chris@3: 
chris@3: import java.sql.SQLException;
chris@3: import java.util.Map;
chris@3: import java.util.concurrent.ConcurrentHashMap;
cli@30: import org.sonews.storage.Storage;
cli@30: import org.sonews.storage.StorageBackendException;
cli@30: import org.sonews.storage.StorageProvider;
chris@3: 
chris@3: /**
chris@3:  *
chris@3:  * @author Christian Lins
chris@3:  * @since sonews/1.0
chris@3:  */
cli@42: public class JDBCDatabaseProvider implements StorageProvider {
chris@3: 
cli@45: 	protected static final Map<Thread, JDBCDatabase> instances =
cli@45: 			new ConcurrentHashMap<Thread, JDBCDatabase>();
chris@3: 
cli@37: 	@Override
cli@42: 	public boolean isSupported(String uri) {
cli@44: 		return uri.startsWith("jdbc:mysql") || uri.startsWith("jdbc:postgresql");
cli@37: 	}
chris@3: 
cli@37: 	@Override
cli@37: 	public Storage storage(Thread thread)
cli@42: 			throws StorageBackendException {
cli@37: 		try {
cli@37: 			if (!instances.containsKey(Thread.currentThread())) {
cli@37: 				JDBCDatabase db = new JDBCDatabase();
cli@37: 				db.arise();
cli@37: 				instances.put(Thread.currentThread(), db);
cli@37: 				return db;
cli@37: 			} else {
cli@37: 				return instances.get(Thread.currentThread());
cli@37: 			}
cli@37: 		} catch (SQLException ex) {
cli@37: 			throw new StorageBackendException(ex);
cli@37: 		}
cli@37: 	}
chris@3: }