001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.util;
019
020import java.io.BufferedReader;
021import java.io.File;
022import java.io.FileReader;
023import java.io.IOException;
024import java.util.ArrayList;
025import java.util.Arrays;
026import java.util.HashSet;
027import java.util.List;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031
032/**
033 * FileBasedIPList loads a list of subnets in CIDR format and ip addresses from a file.
034 *
035 * Given an ip address, isIn  method returns true if ip belongs to one of the subnets.
036 *
037 * Thread safe.
038 */
039
040public class FileBasedIPList implements IPList {
041
042  private static final Log LOG = LogFactory.getLog(FileBasedIPList.class);
043
044  private final String fileName;
045  private final MachineList addressList;
046
047  public FileBasedIPList(String fileName) {
048    this.fileName = fileName;
049    String[] lines = readLines(fileName);
050    if (lines != null) {
051      addressList = new MachineList(new HashSet<String>(Arrays.asList(lines)));
052    } else {
053      addressList = null;
054    }
055  }
056
057  public FileBasedIPList reload() {
058    return new FileBasedIPList(fileName);
059  }
060
061  @Override
062  public  boolean isIn(String ipAddress) {
063    if (ipAddress == null || addressList == null) {
064      return false;
065    }
066    return addressList.includes(ipAddress);
067  }
068
069  /**
070   * reads the lines in a file.
071   * @param fileName
072   * @return lines in a String array; null if the file does not exist or if the
073   * file name is null
074   * @throws IOException
075   */
076  private static String[] readLines(String fileName) {
077    try {
078      if (fileName != null) {
079        File file = new File (fileName);
080        if (file.exists()) {
081          FileReader fileReader = new FileReader(file);
082          BufferedReader bufferedReader = new BufferedReader(fileReader);
083          List<String> lines = new ArrayList<String>();
084          String line = null;
085          while ((line = bufferedReader.readLine()) != null) {
086            lines.add(line);
087          }
088          bufferedReader.close();
089          LOG.debug("Loaded IP list of size = " + lines.size() +" from file = " + fileName);
090          return(lines.toArray(new String[lines.size()]));
091        }
092        else {
093          LOG.debug("Missing ip list file : "+ fileName);
094        }
095      }
096    }
097    catch (Throwable t) {
098      LOG.error(t);
099    }
100    return null;
101  }
102}