This project has retired. For details please refer to its Attic page.
ClusterConfig 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.util;
20  
21  
22  import java.io.*;
23  import java.nio.charset.Charset;
24  import java.util.*;
25  
26  public class ClusterConfig {
27    private HashMap<String, String> clusterMap = new HashMap<String, String>();
28    private String path = System.getenv("CHUKWA_CONF_DIR") + File.separator;
29  
30    static public String getContents(File aFile) {
31      // ...checks on aFile are elided
32      StringBuffer contents = new StringBuffer();
33  
34      try {
35        BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(aFile.getAbsolutePath()), Charset.forName("UTF-8")));
36        try {
37          String line = null; // not declared within while loop
38          /*
39           * readLine is a bit quirky : it returns the content of a line MINUS the
40           * newline. it returns null only for the END of the stream. it returns
41           * an empty String if two newlines appear in a row.
42           */
43          while ((line = input.readLine()) != null) {
44            contents.append(line);
45            contents.append(System.getProperty("line.separator"));
46          }
47        } finally {
48          input.close();
49        }
50      } catch (IOException ex) {
51        ex.printStackTrace();
52      }
53  
54      return contents.toString();
55    }
56  
57    public ClusterConfig() {
58      File cc = new File(path + "jdbc.conf");
59      String buffer = getContents(cc);
60      String[] lines = buffer.split("\n");
61      for (String line : lines) {
62        String[] data = line.split("=", 2);
63        clusterMap.put(data[0], data[1]);
64      }
65    }
66  
67    public String getURL(String cluster) {
68      String url = clusterMap.get(cluster);
69      return url;
70    }
71  
72    public Iterator<String> getClusters() {
73      Set<String> keys = clusterMap.keySet();
74      Iterator<String> i = keys.iterator();
75      return i;
76    }
77  }