This project has retired. For details please refer to its Attic page.
DataConfig xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  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 org.apache.hadoop.chukwa.inputtools.mdl;
20  
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.fs.Path;
26  import java.util.Iterator;
27  import java.util.HashMap;
28  import java.util.Map;
29  import java.io.File;
30  import java.io.FilenameFilter;
31  
32  public class DataConfig {
33    private static Configuration config;
34    final static String MDL_XML = "mdl.xml";
35    private Log log = LogFactory.getLog(DataConfig.class);
36  
37    public DataConfig(String path) {
38      Path fileResource = new Path(path);
39      config = new Configuration();
40      config.addResource(fileResource);
41    }
42  
43    public DataConfig() {
44      String dataConfig = System.getenv("CHUKWA_CONF_DIR");
45      if (dataConfig == null) {
46        dataConfig = MDL_XML;
47      } else {
48        dataConfig += File.separator + MDL_XML;
49      }
50      log.debug("DATACONFIG=" + dataConfig);
51      if (config == null) {
52        try {
53          Path fileResource = new Path(dataConfig);
54          config = new Configuration();
55          config.addResource(fileResource);
56        } catch (Exception e) {
57          log.debug("Error reading configuration file:" + dataConfig);
58        }
59      }
60  
61      if (System.getenv("CHUKWA_CONF_DIR") != null) {
62        // Allow site-specific MDL files to be included in the
63        // configuration so as to keep the "main" mdl.xml pure.
64        File confDir = new File(System.getenv("CHUKWA_CONF_DIR"));
65        File[] confFiles = confDir.listFiles(new FilenameFilter() {
66  
67          @Override
68          public boolean accept(File dir, String name) {
69            // Implements a naming convention of ending with "mdl.xml"
70            // but is careful not to pick up mdl.xml itself again.
71            return name.endsWith(MDL_XML) && !name.equals(MDL_XML);
72          }
73  
74        });
75  
76        if (confFiles != null) {
77          for (File confFile : confFiles)
78            config.addResource(new Path(confFile.getAbsolutePath()));
79        }
80      }  
81    }
82  
83    public String get(String key) {
84      return config.get(key);
85    }
86  
87    public void put(String key, String value) {
88      config.set(key, value);
89    }
90  
91    public Iterator<Map.Entry<String, String>> iterator() {
92      return config.iterator();
93    }
94  
95    public HashMap<String, String> startWith(String key) {
96      HashMap<String, String> transformer = new HashMap<String, String>();
97      Iterator<Map.Entry<String, String>> entries = config.iterator();
98      while (entries.hasNext()) {
99        String entry = entries.next().toString();
100       if (entry.startsWith(key)) {
101         String[] metrics = entry.split("=");
102         transformer.put(metrics[0], metrics[1]);
103       }
104     }
105     return transformer;
106   }
107 }