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.io.File;
023    import java.io.FileReader;
024    import java.util.Properties;
025    import org.apache.log4j.Logger;
026    import uk.ac.roslin.ensembl.config.DBConnection.DataSource;
027    import uk.ac.roslin.ensembl.exception.ConfigurationException;
028    
029    public class RegistryConfiguration {
030    
031        private DataSource type;
032        private DBConnection db = null;
033        private SchemaVersion schema = null;
034    
035        final static Logger LOGGER = Logger.getLogger(RegistryConfiguration.class);
036    
037        /**
038         * Constructor for a  non-configured RegistryConfiguration object. This is not a valid
039         * Configuration until a DBConnection object is set.
040         */
041        public RegistryConfiguration() {
042        }
043    
044        /**
045         * Constructs a RegistryConfiguration object after calling setType, which will
046         * set the DBConnection if DataSource is of a known valid type. If no 
047         * further setting of schemaProperties this object will return 
048         * default configuration values read from properties files in ensembl-config jar. 
049         * @param type DataSource
050         * @throws ConfigurationException
051         */
052        public RegistryConfiguration(DataSource type) throws ConfigurationException {
053            this.setType(type);
054        }
055    
056        /**
057         * Sets the DBConnection on this RegistryConfiguration object
058         * if DataSource is of a known valid type.
059         * @param type
060         * @throws ConfigurationException
061         */
062        private void setType(DataSource type) throws ConfigurationException {
063            this.type = type;
064            if (this.type == DataSource.ENSEMBLDB) {
065                db = new DBConnection(DataSource.ENSEMBLDB);
066            } else if (this.type == DataSource.ENSEMBLDB_ARCHIVES) {
067                db = new DBConnection(DataSource.ENSEMBLDB_ARCHIVES);
068            } else if (this.type == DataSource.ENSEMBLGENOMES) {
069                db = new DBConnection(DataSource.ENSEMBLGENOMES);
070            }
071        }
072    
073        /**
074         * Sets DBConnection on this RegistryConfiguration object
075         * from a given set of Properties.
076         * @param dbProperties Properties
077         * @throws ConfigurationException
078         */
079        public void setDBProperties(Properties dbProperties) throws ConfigurationException {
080            this.type = DataSource.LOCAL;
081            db = new DBConnection(type, dbProperties);
082        }
083    
084        /**
085         * Sets the SchemaVersion object on this RegistryConfiguration object
086         * using the given Properties.
087         * @param schemaProperties Properties
088         * @throws ConfigurationException
089         */
090        public void setSchemaProperties(Properties schemaProperties) throws ConfigurationException {
091            schema = new SchemaVersion(schemaProperties);
092        }
093    
094        /**
095         * Sets DBConnection on this RegistryConfiguration object
096         * from a given File of Properties.
097         * @param dbProperties File
098         * @throws ConfigurationException
099         */
100        public void setDBByFile(File dbProperties) throws ConfigurationException {
101            this.type = DataSource.LOCAL;
102            if (dbProperties == null || !dbProperties.canRead()) {
103                throw new ConfigurationException("Failure to read properties from local DB configuration file");
104            }
105    
106            Properties props = new Properties();
107            try {
108                props.load(new FileReader(dbProperties));
109                if (props == null || props.isEmpty()) {
110                    throw new Exception();
111                }
112                db = new DBConnection(type, props);
113            } catch (Exception ex) {
114                throw new ConfigurationException("Failure to read properties from local DB configuration file", ex);
115            }
116        }
117    
118        /**
119         * Sets the SchemaVersion object on this RegistryConfiguration object
120         * using a File with Properties values.
121         * @param schemaMappings File
122         * @throws ConfigurationException
123         */
124        public void setSchemaByFile(File schemaMappings) throws ConfigurationException {
125    
126            if (schemaMappings == null || !schemaMappings.canRead()) {
127                throw new ConfigurationException("Failure to read schema mappings from local configuration file");
128            }
129    
130            Properties props = new Properties();
131            try {
132                props.load(new FileReader(schemaMappings));
133                if (props == null || props.isEmpty()) {
134                    throw new Exception();
135                }
136                schema = new SchemaVersion(props);
137            } catch (Exception ex) {
138                throw new ConfigurationException("Failure to read properties from local mapping rules configuration file", ex);
139            }
140        }
141    
142        /**
143         * Returns the DBCOnnection object set  on this RegistryConfigurationobject,
144         * or throws an ConfigurationException if there is no DBConnection set.
145         * @return DBConnection
146         * @throws ConfigurationException
147         */
148        public DBConnection getDb() throws ConfigurationException {
149            if (db==null) {
150                 throw new ConfigurationException("No DB connection properties set");
151            }
152            return db;
153        }
154    
155    
156        /**
157         * Returns the SchemaVersionobject set on this RegistryConfiguration object.
158         * If SchemaVersion is unset, it is first initialized to a default version
159         * (read from properties file in ensembl-config artifact).
160         * @return SchemaVersion
161         * @throws ConfigurationException
162         */
163        public SchemaVersion getSchema() throws ConfigurationException {
164            if (schema==null) {
165                schema = new SchemaVersion();
166            }
167            if (schema==null) {
168                throw new ConfigurationException("No schema mapping rules set");
169            }
170            return schema;
171        }
172    
173        /**
174         * Returns the current DataSource type of this RegistryConfiguration object.
175         * @return DataSource
176         */
177        public DataSource getType() {
178            return type;
179        }
180    
181    
182    
183    }