/**
 * Copyright 2008 The Apache Software Foundation
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.katta.index.indexer.merge;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.fs.PathFilter;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapred.FileInputFormat;
import org.apache.hadoop.mapred.FileSplit;
import org.apache.hadoop.mapred.InputSplit;
import org.apache.hadoop.mapred.JobConf;
import org.apache.hadoop.mapred.RecordReader;
import org.apache.hadoop.mapred.Reporter;
import org.apache.log4j.Logger;

public class DfsIndexInputFormat extends FileInputFormat<Text, DocumentInformation> {

  @SuppressWarnings("hiding")
  private final static Logger LOG = Logger.getLogger(DfsIndexInputFormat.class);

  public static final String DOCUMENT_INFORMATION = "document.duplicate.information.class";

  public RecordReader getRecordReader(final InputSplit inputSplit, final JobConf jobConf, Reporter reporter)
      throws IOException {
    IDocumentDuplicateInformation duplicateInformation = null;
    String className = jobConf.get(DOCUMENT_INFORMATION);
    try {
      Class<?> byName = jobConf.getClassByName(className);
      duplicateInformation = (IDocumentDuplicateInformation) byName.newInstance();
    } catch (Exception e) {
      LOG.error("can not load class: " + className, e);
      throw new IOException(e.getMessage());
    }
    reporter.setStatus(((FileSplit) inputSplit).getPath().toString());
    return new DfsIndexRecordReader(jobConf, inputSplit, duplicateInformation);
  }

  public InputSplit[] getSplits(JobConf jobConf, int numSplits) throws IOException {
    FileSystem fileSystem = FileSystem.get(jobConf);
    Path[] indices = getZipIndices(jobConf, fileSystem);
    InputSplit[] splits = new InputSplit[indices.length];
    for (int i = 0; i < splits.length; i++) {
      splits[i] = new FileSplit(indices[i], 0, Integer.MAX_VALUE, jobConf);
    }
    return splits;
  }

  private Path[] getZipIndices(JobConf jobConf, FileSystem fileSystem) throws IOException {
    Path[] inputPaths = jobConf.getInputPaths();
    List<Path> list = new ArrayList<Path>();
    for (int i = 0; i < inputPaths.length; i++) {
      Path inputPath = inputPaths[i];
      getChilds(fileSystem, list, inputPath);
    }
    return list.toArray(new Path[list.size()]);
  }

  private void getChilds(final FileSystem fileSystem, List<Path> list, Path inputPath) throws IOException {
    FileStatus[] fileStatuses = fileSystem.listStatus(inputPath, new PathFilter() {
      public boolean accept(Path path) {
        boolean ret = false;
        try {
          boolean isFile = fileSystem.isFile(path);
          boolean isZip = path.getName().endsWith(".zip");
          ret = !isFile || isZip;
        } catch (IOException e) {
          LOG.warn("can not get child directory", e);
        }
        return ret;
      }
    });
    for (int j = 0; j < fileStatuses.length; j++) {
      FileStatus fileStatuse = fileStatuses[j];
      Path path = fileStatuse.getPath();
      if (!fileSystem.isFile(path)) {
        getChilds(fileSystem, list, path);
      } else {
        list.add(path);
      }
    }
  }

}

@@ -1582,21 +1582,23 @@
 g%22)%0A  pr
-ivate
+otected
  final s
@@ -2202,100 +2202,110 @@
     
-LOG.error(%22can not load class: %22 + className, e);%0A      throw new IOException(e.getMessage()
+throw new RuntimeException(%22could not instantiate %22 + IDocumentDuplicateInformation.class.getName(), e
 );%0A 

improve exception message on missing property in merge job