This project has retired. For details please refer to its Attic page.
DsDirectory 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.extraction.engine.datasource;
20  
21  
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  import org.apache.hadoop.chukwa.inputtools.mdl.DataConfig;
26  import org.apache.hadoop.conf.Configuration;
27  import org.apache.hadoop.fs.FileStatus;
28  import org.apache.hadoop.fs.FileSystem;
29  import org.apache.hadoop.fs.Path;
30  
31  public class DsDirectory {
32    private static Object lock = new Object();
33    private static DsDirectory dsDirectory = null;
34    private static final String[] emptyArray = new String[0];
35  
36    private String rootFolder = null;
37    private DataConfig dataConfig = null;
38  
39    private static FileSystem fs = null;
40    private static Configuration conf = null;
41  
42    private DsDirectory() {
43      dataConfig = new DataConfig();
44      conf = new Configuration();
45      try {
46        fs = FileSystem.get(conf);
47      } catch (IOException e) {
48        e.printStackTrace();
49      }
50      rootFolder = dataConfig.get("chukwa.engine.dsDirectory.rootFolder");
51      if (!rootFolder.endsWith("/")) {
52        rootFolder += "/";
53      }
54    }
55  
56    public static DsDirectory getInstance() {
57      synchronized (lock) {
58        if (dsDirectory == null) {
59          dsDirectory = new DsDirectory();
60        }
61      }
62      return dsDirectory;
63    }
64  
65    public String[] list(String cluster) throws DataSourceException {
66      List<String> datasources = new ArrayList<String>();
67      try {
68        FileStatus[] fileStat = fs.listStatus(new Path(rootFolder + cluster));
69  
70        for (FileStatus fstat : fileStat) {
71          if (fstat.isDir()) {
72            datasources.add(fstat.getPath().getName());
73          }
74        }
75      } catch (IOException e) {
76        e.printStackTrace();
77        throw new DataSourceException(e);
78      }
79      return datasources.toArray(emptyArray);
80    }
81  
82    public static void main(String[] args) throws DataSourceException {
83      DsDirectory dsd = DsDirectory.getInstance();
84      String[] dss = dsd.list("unknown");
85      for (String d : dss) {
86        System.out.println(d);
87      }
88    }
89  }