001/* 002 * (C) Copyright 2016 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.importer.stream.producer; 020 021import java.io.File; 022import java.io.IOException; 023import java.nio.file.Files; 024import java.util.Iterator; 025 026import org.apache.commons.logging.Log; 027import org.apache.commons.logging.LogFactory; 028import org.nuxeo.importer.stream.message.BlobMessage; 029import org.nuxeo.lib.stream.pattern.producer.AbstractProducer; 030 031/** 032 * Use a listing file to generate BlobMessage. 033 * 034 * @since 9.1 035 */ 036public class FileBlobMessageProducer extends AbstractProducer<BlobMessage> { 037 private static final Log log = LogFactory.getLog(FileBlobMessageProducer.class); 038 039 protected final File listFile; 040 041 protected int count = 0; 042 043 protected Iterator<String> fileIterator; 044 045 public FileBlobMessageProducer(int producerId, File listFile) { 046 super(producerId); 047 this.listFile = listFile; 048 log.info("Producer using file list: " + listFile.getAbsolutePath()); 049 try { 050 fileIterator = Files.lines(listFile.toPath()).iterator(); 051 } catch (IOException e) { 052 String msg = "Failed to read file: " + listFile.getAbsolutePath(); 053 log.error(msg, e); 054 throw new IllegalArgumentException(e); 055 } 056 057 } 058 059 @Override 060 public int getPartition(BlobMessage message, int partitions) { 061 return count % partitions; 062 } 063 064 @Override 065 public void close() throws Exception { 066 super.close(); 067 fileIterator = null; 068 } 069 070 @Override 071 public boolean hasNext() { 072 return fileIterator.hasNext(); 073 } 074 075 @Override 076 public BlobMessage next() { 077 String filePath = fileIterator.next(); 078 count += 1; 079 // TODO: guess mimetype, length ? 080 return new BlobMessage.FileMessageBuilder(filePath).build(); 081 } 082}