001/** 002 * Copyright (C) 2010-2015 The Roslin Institute <contact andy.law@roslin.ed.ac.uk> 003 * 004 * This file is part of JEnsembl: a Java API to Ensembl data sources developed by the 005 * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of 006 * Veterinary Studies, University of Edinburgh. 007 * 008 * Project hosted at: http://jensembl.sourceforge.net 009 * 010 * This is free software: you can redistribute it and/or modify 011 * it under the terms of the GNU General Public License (version 3) as published by 012 * the Free Software Foundation. 013 * 014 * This software is distributed in the hope that it will be useful, 015 * but WITHOUT ANY WARRANTY; without even the implied warranty of 016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 017 * GNU General Public License for more details. 018 * 019 * You should have received a copy of the GNU General Public License 020 * in this software distribution. If not, see: http://opensource.org/licenses/gpl-3.0.html 021 */ 022package uk.ac.roslin.ensembl.config; 023 024import java.io.File; 025import java.io.FileReader; 026import java.util.Properties; 027import org.slf4j.Logger; 028import org.slf4j.LoggerFactory; 029import uk.ac.roslin.ensembl.config.DBConnection.DataSource; 030import uk.ac.roslin.ensembl.exception.ConfigurationException; 031 032public class RegistryConfiguration { 033 034 private DataSource type; 035 private DBConnection db = null; 036 private SchemaVersion schema = null; 037 038 final static Logger LOGGER = LoggerFactory.getLogger(RegistryConfiguration.class); 039 040 /** 041 * Constructor for a non-configured RegistryConfiguration object. This is not a valid 042 * Configuration until a DBConnection object is set. 043 */ 044 public RegistryConfiguration() { 045 } 046 047 /** 048 * Constructs a RegistryConfiguration object after calling setType, which will 049 * set the DBConnection if DataSource is of a known valid type. If no 050 * further setting of schemaProperties this object will return 051 * default configuration values read from properties files in ensembl-config jar. 052 * @param type DataSource 053 * @throws ConfigurationException 054 */ 055 public RegistryConfiguration(DataSource type) throws ConfigurationException { 056 this.setType(type); 057 } 058 059 /** 060 * Sets the DBConnection on this RegistryConfiguration object 061 * if DataSource is of a known valid type. 062 * @param type 063 * @throws ConfigurationException 064 */ 065 private void setType(DataSource type) throws ConfigurationException { 066 this.type = type; 067 if (this.type == DataSource.ENSEMBLDB) { 068 db = new DBConnection(DataSource.ENSEMBLDB); 069 } else if (this.type == DataSource.ENSEMBLDB_ARCHIVES) { 070 db = new DBConnection(DataSource.ENSEMBLDB_ARCHIVES); 071 } else if (this.type == DataSource.ENSEMBLGENOMES) { 072 db = new DBConnection(DataSource.ENSEMBLGENOMES); 073 } else if (this.type == DataSource.ENSEMBLBACTERIA) { 074 db = new DBConnection(DataSource.ENSEMBLBACTERIA); 075 } else { 076 throw new ConfigurationException("Unknown Datasource.type: "+type); 077 } 078 } 079 080 /** 081 * Sets DBConnection on this RegistryConfiguration object 082 * from a given set of Properties. 083 * @param dbProperties Properties 084 * @throws ConfigurationException 085 */ 086 public void setDBProperties(Properties dbProperties) throws ConfigurationException { 087 this.type = DataSource.LOCAL; 088 db = new DBConnection(type, dbProperties); 089 } 090 091 /** 092 * Sets the SchemaVersion object on this RegistryConfiguration object 093 * using the given Properties. 094 * @param schemaProperties Properties 095 * @throws ConfigurationException 096 */ 097 public void setSchemaProperties(Properties schemaProperties) throws ConfigurationException { 098 schema = new SchemaVersion(schemaProperties); 099 } 100 101 /** 102 * Sets DBConnection on this RegistryConfiguration object 103 * from a given File of Properties. 104 * @param dbProperties File 105 * @throws ConfigurationException 106 */ 107 public void setDBByFile(File dbProperties) throws ConfigurationException { 108 this.type = DataSource.LOCAL; 109 if (dbProperties == null || !dbProperties.canRead()) { 110 throw new ConfigurationException("Failure to read properties from local DB configuration file"); 111 } 112 113 Properties props = new Properties(); 114 try { 115 props.load(new FileReader(dbProperties)); 116 if (props == null || props.isEmpty()) { 117 throw new Exception(); 118 } 119 db = new DBConnection(type, props); 120 } catch (Exception ex) { 121 throw new ConfigurationException("Failure to read properties from local DB configuration file", ex); 122 } 123 } 124 125 /** 126 * Sets the SchemaVersion object on this RegistryConfiguration object 127 * using a File with Properties values. 128 * @param schemaMappings File 129 * @throws ConfigurationException 130 */ 131 public void setSchemaByFile(File schemaMappings) throws ConfigurationException { 132 133 if (schemaMappings == null || !schemaMappings.canRead()) { 134 throw new ConfigurationException("Failure to read schema mappings from local configuration file"); 135 } 136 137 Properties props = new Properties(); 138 try { 139 props.load(new FileReader(schemaMappings)); 140 if (props == null || props.isEmpty()) { 141 throw new Exception(); 142 } 143 schema = new SchemaVersion(props); 144 } catch (Exception ex) { 145 throw new ConfigurationException("Failure to read properties from local mapping rules configuration file", ex); 146 } 147 } 148 149 /** 150 * Returns the DBCOnnection object set on this RegistryConfigurationobject, 151 * or throws an ConfigurationException if there is no DBConnection set. 152 * @return DBConnection 153 * @throws ConfigurationException 154 */ 155 public DBConnection getDb() throws ConfigurationException { 156 if (db==null) { 157 throw new ConfigurationException("No DB connection properties set"); 158 } 159 return db; 160 } 161 162 163 /** 164 * Returns the SchemaVersionobject set on this RegistryConfiguration object. 165 * If SchemaVersion is unset, it is first initialized to a default version 166 * (read from properties file in ensembl-config artifact). 167 * @return SchemaVersion 168 * @throws ConfigurationException 169 */ 170 public SchemaVersion getSchema() throws ConfigurationException { 171 if (schema==null) { 172 schema = new SchemaVersion(); 173 } 174 return schema; 175 } 176 177 /** 178 * Returns the current DataSource type of this RegistryConfiguration object. 179 * @return DataSource 180 */ 181 public DataSource getType() { 182 return type; 183 } 184 185 186 187}