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.FileInputStream;
023import java.io.IOException;
024import java.io.InputStreamReader;
025import java.io.Reader;
026import java.nio.charset.StandardCharsets;
027import java.util.ArrayList;
028import java.util.Arrays;
029import java.util.HashSet;
030import java.util.List;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034
035/**
036 * FileBasedIPList loads a list of subnets in CIDR format and ip addresses from
037 * a file.
038 *
039 * Given an ip address, isIn  method returns true if ip belongs to one of the
040 * subnets.
041 *
042 * Thread safe.
043 */
044public class FileBasedIPList implements IPList {
045
046  private static final Log LOG = LogFactory.getLog(FileBasedIPList.class);
047
048  private final String fileName;
049  private final MachineList addressList;
050
051  public FileBasedIPList(String fileName) {
052    this.fileName = fileName;
053    String[] lines;
054    try {
055      lines = readLines(fileName);
056    } catch (IOException e) {
057      lines = null;
058    }
059    if (lines != null) {
060      addressList = new MachineList(new HashSet<String>(Arrays.asList(lines)));
061    } else {
062      addressList = null;
063    }
064  }
065
066  public FileBasedIPList reload() {
067    return new FileBasedIPList(fileName);
068  }
069
070  @Override
071  public  boolean isIn(String ipAddress) {
072    if (ipAddress == null || addressList == null) {
073      return false;
074    }
075    return addressList.includes(ipAddress);
076  }
077
078  /**
079   * Reads the lines in a file.
080   * @param fileName
081   * @return lines in a String array; null if the file does not exist or if the
082   * file name is null
083   * @throws IOException
084   */
085  private static String[] readLines(String fileName) throws IOException {
086    try {
087      if (fileName != null) {
088        File file = new File (fileName);
089        if (file.exists()) {
090          try (
091              Reader fileReader = new InputStreamReader(
092                  new FileInputStream(file), StandardCharsets.UTF_8);
093              BufferedReader bufferedReader = new BufferedReader(fileReader)) {
094            List<String> lines = new ArrayList<String>();
095            String line = null;
096            while ((line = bufferedReader.readLine()) != null) {
097              lines.add(line);
098            }
099            if (LOG.isDebugEnabled()) {
100              LOG.debug("Loaded IP list of size = " + lines.size() +
101                  " from file = " + fileName);
102            }
103            return (lines.toArray(new String[lines.size()]));
104          }
105        } else {
106          LOG.debug("Missing ip list file : "+ fileName);
107        }
108      }
109    } catch (IOException ioe) {
110      LOG.error(ioe);
111      throw ioe;
112    }
113    return null;
114  }
115}