001/* 002 003 gl-service-jdbc Implementation of persistent cache for gl-service using JDBC. 004 Copyright (c) 2012-2015 National Marrow Donor Program (NMDP) 005 006 This library is free software; you can redistribute it and/or modify it 007 under the terms of the GNU Lesser General Public License as published 008 by the Free Software Foundation; either version 3 of the License, or (at 009 your option) any later version. 010 011 This library is distributed in the hope that it will be useful, but WITHOUT 012 ANY WARRANTY; with out even the implied warranty of MERCHANTABILITY or 013 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 014 License for more details. 015 016 You should have received a copy of the GNU Lesser General Public License 017 along with this library; if not, write to the Free Software Foundation, 018 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. 019 020 > http://www.fsf.org/licensing/licenses/lgpl.html 021 > http://www.opensource.org/licenses/lgpl-license.php 022 023*/ 024package org.nmdp.gl.service.jdbc; 025 026import java.io.IOException; 027import java.io.InputStream; 028import java.util.Properties; 029 030import javax.sql.DataSource; 031 032import org.nmdp.gl.Allele; 033import org.nmdp.gl.Locus; 034 035import org.nmdp.gl.service.GlRegistry; 036import org.nmdp.gl.service.GlstringResolver; 037import org.nmdp.gl.service.IdResolver; 038 039import com.google.common.cache.Cache; 040import com.google.common.cache.CacheBuilder; 041 042import com.google.inject.AbstractModule; 043import com.google.inject.Provides; 044import com.google.inject.Singleton; 045import com.google.inject.name.Named; 046import com.google.inject.name.Names; 047 048import com.jolbox.bonecp.BoneCPDataSource; 049 050/** 051 * JDBC module. 052 */ 053public final class JdbcModule extends AbstractModule { 054 055 @Override 056 protected void configure() { 057 Names.bindProperties(binder(), loadProperties()); 058 059 bind(IdResolver.class).to(JdbcIdResolver.class); 060 bind(GlstringResolver.class).to(JdbcGlstringResolver.class); 061 bind(GlRegistry.class).to(JdbcGlRegistry.class); 062 } 063 064 @Provides 065 Cache<String, String> createIdCache() { 066 return CacheBuilder.newBuilder().maximumSize(10000).build(); 067 } 068 069 @Provides 070 Cache<String, Locus> createLociCache() { 071 return CacheBuilder.newBuilder().maximumSize(1000).build(); 072 } 073 074 @Provides 075 Cache<String, Allele> createAlleleCache() { 076 return CacheBuilder.newBuilder().maximumSize(10000).build(); 077 } 078 079 @Provides @Singleton 080 DataSource createDataSource(@Named("jdbcDriver") final String driver, 081 @Named("jdbcUrl") final String url, 082 @Named("jdbcUsername") final String username, 083 @Named("jdbcPassword") final String password) { 084 085 try { 086 Class.forName(driver); 087 } 088 catch (ClassNotFoundException e) { 089 // ignore 090 } 091 BoneCPDataSource dataSource = new BoneCPDataSource(); 092 dataSource.setJdbcUrl(url); 093 dataSource.setUsername(username); 094 dataSource.setPassword(password); 095 // todo: configure pool 096 return dataSource; 097 } 098 099 private Properties loadProperties() { 100 Properties properties = new Properties(); 101 InputStream inputStream = null; 102 try { 103 inputStream = getClass().getResourceAsStream("/gl-service.properties"); 104 properties.load(inputStream); 105 } 106 catch (IOException e) { 107 // ignore 108 } 109 finally { 110 try { 111 inputStream.close(); 112 } 113 catch (Exception e) { 114 // ignore 115 } 116 } 117 return properties; 118 } 119}