View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.accumulo.core.conf;
18  
19  import java.util.Iterator;
20  import java.util.Map.Entry;
21  import java.util.TreeMap;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.log4j.Logger;
25  
26  public class SiteConfiguration extends AccumuloConfiguration {
27    private static final Logger log = Logger.getLogger(SiteConfiguration.class);
28    
29    private static AccumuloConfiguration parent = null;
30    private static SiteConfiguration instance = null;
31    
32    private static Configuration xmlConfig;
33    
34    private SiteConfiguration(AccumuloConfiguration parent) {
35      SiteConfiguration.parent = parent;
36    }
37    
38    synchronized public static SiteConfiguration getInstance(AccumuloConfiguration parent) {
39      if (instance == null) {
40        instance = new SiteConfiguration(parent);
41        ConfigSanityCheck.validate(instance);
42      }
43      return instance;
44    }
45    
46    synchronized private static Configuration getXmlConfig() {
47      String configFile = System.getProperty("org.apache.accumulo.config.file", "accumulo-site.xml");
48      if (xmlConfig == null) {
49        xmlConfig = new Configuration(false);
50        
51        if (SiteConfiguration.class.getClassLoader().getResource(configFile) == null)
52          log.warn(configFile + " not found on classpath");
53        else
54          xmlConfig.addResource(configFile);
55      }
56      return xmlConfig;
57    }
58    
59    @Override
60    public String get(Property property) {
61      String key = property.getKey();
62      
63      String value = getXmlConfig().get(key);
64      
65      if (value == null || !property.getType().isValidFormat(value)) {
66        if (value != null)
67          log.error("Using default value for " + key + " due to improperly formatted " + property.getType() + ": " + value);
68        value = parent.get(property);
69      }
70      return value;
71    }
72    
73    @Override
74    public Iterator<Entry<String,String>> iterator() {
75      TreeMap<String,String> entries = new TreeMap<String,String>();
76      
77      for (Entry<String,String> parentEntry : parent)
78        entries.put(parentEntry.getKey(), parentEntry.getValue());
79      
80      for (Entry<String,String> siteEntry : getXmlConfig())
81        entries.put(siteEntry.getKey(), siteEntry.getValue());
82      
83      return entries.entrySet().iterator();
84    }
85    
86    /**
87     * method here to support testing, do not call
88     */
89    public void clear() {
90      getXmlConfig().clear();
91    }
92    
93    /**
94     * method here to support testing, do not call
95     */
96    public void set(Property property, String value) {
97      set(property.getKey(), value);
98    }
99    
100   /**
101    * method here to support testing, do not call
102    */
103   public void set(String key, String value) {
104     getXmlConfig().set(key, value);
105   }
106 }