1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * 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 and16 * limitations under the License.17 */1819package org.apache.hadoop.chukwa.util;
202122import java.io.*;
23import java.nio.charset.Charset;
24import java.util.*;
2526publicclassClusterConfig {
27private HashMap<String, String> clusterMap = new HashMap<String, String>();
28private String path = System.getenv("CHUKWA_CONF_DIR") + File.separator;
2930staticpublic String getContents(File aFile) {
31// ...checks on aFile are elided32 StringBuffer contents = new StringBuffer();
3334try {
35 BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream(aFile.getAbsolutePath()), Charset.forName("UTF-8")));
36try {
37 String line = null; // not declared within while loop38/*39 * readLine is a bit quirky : it returns the content of a line MINUS the40 * newline. it returns null only for the END of the stream. it returns41 * an empty String if two newlines appear in a row.42 */43while ((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 }
5354return contents.toString();
55 }
5657publicClusterConfig() {
58 File cc = new File(path + "jdbc.conf");
59 String buffer = getContents(cc);
60 String[] lines = buffer.split("\n");
61for (String line : lines) {
62 String[] data = line.split("=", 2);
63 clusterMap.put(data[0], data[1]);
64 }
65 }
6667public String getURL(String cluster) {
68 String url = clusterMap.get(cluster);
69return url;
70 }
7172public Iterator<String> getClusters() {
73 Set<String> keys = clusterMap.keySet();
74 Iterator<String> i = keys.iterator();
75return i;
76 }
77 }