001    /**
002     * Copyright (C) 2010 The Roslin Institute <contact andy.law@roslin.ed.ac.uk>
003     *
004     * This file is part of the Ensembl Java API demonstration project developed by the
005     * Bioinformatics Group at The Roslin Institute, The Royal (Dick) School of
006     * Veterinary Studies, University of Edinburgh.
007     *
008     * This is free software: you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (version 3) as published by
010     * the Free Software Foundation.
011     *
012     * This software is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015     * GNU General Public License for more details.
016     *
017     * You should have received a copy of the GNU General Public License
018     * in this software distribution. If not, see <http://www.gnu.org/licenses/gpl-3.0.html/>.
019     */
020    package uk.ac.roslin.ensembl.config;
021    
022    import java.util.Properties;
023    import java.util.Enumeration;
024    import java.util.Locale;
025    import java.util.ResourceBundle;
026    import org.apache.log4j.Logger;
027    import uk.ac.roslin.ensembl.exception.ConfigurationException;
028    
029    /**
030     *
031     * @author paterson
032     */
033    public class SchemaVersion {
034    
035        final static Logger LOGGER = Logger.getLogger(SchemaVersion.class);
036    
037        private Properties schemaVersionProperties = null;
038        private String currentEnsemblVersion = null;
039        private String currentGenomesVersion = null;
040        private String[] registeredSchemaVersions = null;
041    
042        public SchemaVersion() throws ConfigurationException {
043            schemaVersionProperties = this.readResource("uk.ac.roslin.ensembl.configfiles.schema_version_mappings");
044            initialize();
045        }
046    
047        public SchemaVersion(Properties localMappingRules) throws ConfigurationException {
048            schemaVersionProperties = localMappingRules;
049            if (schemaVersionProperties == null || schemaVersionProperties.isEmpty()) {
050                throw new ConfigurationException("No Schema Mapping properties given");
051            }
052            initialize();
053        }
054    
055        private void initialize() {
056            currentEnsemblVersion = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("current_ensembl_release") : null;
057            currentGenomesVersion = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("current_genomes_release") : null;
058            registeredSchemaVersions = (schemaVersionProperties != null
059                    && schemaVersionProperties.getProperty("known_ensembl_schemas") != null)
060                    ? schemaVersionProperties.getProperty("known_ensembl_schemas").split(" ")
061                    : null;
062        }
063    
064        private Properties readResource(String id) throws ConfigurationException {
065            Properties p = null;
066            try {
067                //NB: need to pass in the classloader to get this to work in a test environment!
068                ResourceBundle rb = ResourceBundle.getBundle(id, Locale.getDefault(),
069                    this.getClass().getClassLoader());
070                p = new Properties();
071                for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
072                    final String key = (String) keys.nextElement();
073                    final String value = rb.getString(key);
074                    p.put(key, value);
075                }
076    
077            } catch (Exception ex) {
078                throw new ConfigurationException("System can't read the configuration file: " + id, ex);
079            }
080    
081            if (p == null || p.isEmpty()) {
082                throw new ConfigurationException("No Schema Mapping properties read from " + id);
083            }
084            return p;
085        }
086    
087        public String getMybatisSchemaPath(String database, String version) {
088            String out = null;
089            String location = null;
090            String sc = null;
091    
092            location = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty("schema_location") : null;
093            sc = (schemaVersionProperties != null) ? schemaVersionProperties.getProperty(database + "_" + version + "_schema") : null;
094    
095            out = (location != null && sc != null) ? location + sc + "Configuration.xml" : null;
096            return out;
097        }
098    
099        public Properties getConfigurationProperties() {
100            return schemaVersionProperties;
101        }
102    
103        public String getCurrentEnsemblVersion() {
104            return currentEnsemblVersion;
105    
106        }
107    
108        public String getCurrentGenomesVersion() {
109            return currentGenomesVersion;
110        }
111    
112        public String[] getRegisteredSchemas() {
113            return registeredSchemaVersions;
114        }
115    
116        public String getBaseMybatis() {
117            return schemaVersionProperties.getProperty("base_mybatis");
118        }
119    }