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.util.*;
2425publicclassClusterConfig {
26publicstaticfinal HashMap<String, String> clusterMap = new HashMap<String, String>();
27private String path = System.getenv("CHUKWA_CONF_DIR") + File.separator;
2829staticpublic String getContents(File aFile) {
30// ...checks on aFile are elided31 StringBuffer contents = new StringBuffer();
3233try {
34// use buffering, reading one line at a time35// FileReader always assumes default encoding is OK!36 BufferedReader input = new BufferedReader(new FileReader(aFile));
37try {
38 String line = null; // not declared within while loop39/*40 * readLine is a bit quirky : it returns the content of a line MINUS the41 * newline. it returns null only for the END of the stream. it returns42 * an empty String if two newlines appear in a row.43 */44while ((line = input.readLine()) != null) {
45 contents.append(line);
46 contents.append(System.getProperty("line.separator"));
47 }
48 } finally {
49 input.close();
50 }
51 } catch (IOException ex) {
52 ex.printStackTrace();
53 }
5455return contents.toString();
56 }
5758publicClusterConfig() {
59 File cc = new File(path + "jdbc.conf");
60 String buffer = getContents(cc);
61 String[] lines = buffer.split("\n");
62for (String line : lines) {
63 String[] data = line.split("=", 2);
64 clusterMap.put(data[0], data[1]);
65 }
66 }
6768public String getURL(String cluster) {
69 String url = clusterMap.get(cluster);
70return url;
71 }
7273public Iterator<String> getClusters() {
74 Set<String> keys = clusterMap.keySet();
75 Iterator<String> i = keys.iterator();
76return i;
77 }
78 }