View Javadoc
1   /*
2    * The baseCode project
3    *
4    * Copyright (c) 2009 University of British Columbia
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *       http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   *
18   */
19  package ubic.basecode.util;
20  
21  import java.util.Iterator;
22  
23  import org.apache.commons.configuration2.CompositeConfiguration;
24  import org.apache.commons.configuration2.PropertiesConfiguration;
25  import org.apache.commons.configuration2.SystemConfiguration;
26  import org.apache.commons.configuration2.ex.ConfigurationException;
27  import org.apache.commons.configuration2.io.FileHandler;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * Configuration of ontology services and other things.
33   *
34   * @author paul
35   *
36   */
37  public class Configuration {
38  
39      private static CompositeConfiguration config;
40  
41      /**
42       * Name of the resource containing defaults
43       */
44      private static final String DEFAULT_CONFIGURATION = "ontology.properties";
45  
46      private static Logger log = LoggerFactory.getLogger( Configuration.class );
47  
48      /**
49       * The name of the file users can use to customize.
50       */
51      private static final String USER_CONFIGURATION = "basecode.properties";
52  
53      static {
54  
55          config = new CompositeConfiguration();
56          config.addConfiguration( new SystemConfiguration() );
57  
58          /*
59           * the order matters - first come, first serve. Items added later do not overwrite items defined earlier. Thus
60           * the user configuration has to be listed first.
61           */
62  
63          try {
64              // purely for backwards compatibility, if the user hasn't set up ontology.properties.
65              PropertiesConfiguration pc = new PropertiesConfiguration();
66              FileHandler handler = new FileHandler( pc );
67              handler.setFileName( "Gemma.properties" );
68              handler.load();
69              config.addConfiguration( pc );
70          } catch ( ConfigurationException e ) {
71          }
72  
73          try {
74              PropertiesConfiguration pc = new PropertiesConfiguration();
75              FileHandler handler = new FileHandler( pc );
76              handler.setFileName( USER_CONFIGURATION );
77              handler.load();
78              config.addConfiguration( pc );
79          } catch ( ConfigurationException e ) {
80          }
81  
82          try {
83              PropertiesConfiguration pc = new PropertiesConfiguration();
84              FileHandler handler = new FileHandler( pc );
85              handler.setFileName( DEFAULT_CONFIGURATION );
86              handler.load();
87              config.addConfiguration( pc );
88          } catch ( ConfigurationException e ) {
89              log.error( DEFAULT_CONFIGURATION + " is missing, ontology loading may fail" );
90          }
91  
92          // step through the result and do a final round of variable substitution
93          for ( Iterator<String> it = config.getKeys(); it.hasNext(); ) {
94              String key = it.next();
95              String property = config.getString( key );
96              if ( property != null && property.startsWith( "${" ) && property.endsWith( "}" ) ) {
97                  String keyToSubstitute = property.substring( 2, property.length() - 1 );
98                  String valueToSubstitute = config.getString( keyToSubstitute );
99                  log.debug( key + "=" + property + " -> " + valueToSubstitute );
100                 config.setProperty( key, valueToSubstitute );
101             }
102         }
103 
104     }
105 
106     /**
107      * @param key
108      * @return
109      */
110     public static boolean getBoolean( String key ) {
111         return config.getBoolean( key, false );
112     }
113 
114     /**
115      * @param key
116      * @return
117      */
118     public static String getString( String key ) {
119         return config.getString( key );
120     }
121 
122     /**
123      * @param key
124      * @return
125      */
126     public static void setString( String key, Object value ) {
127         config.setProperty( key, value );
128     }
129 }