001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.dataset; 018 019import java.io.BufferedReader; 020import java.io.File; 021import java.io.FileReader; 022import java.io.IOException; 023import java.util.LinkedList; 024import java.util.List; 025import java.util.Scanner; 026 027/** 028 * A DataSet that reads payloads from a file that are used to create each message exchange 029 * along with using a pluggable transformer to customize the messages. The file contents may optionally 030 * be split using a supplied token. 031 * 032 * @version 033 */ 034public class FileDataSet extends ListDataSet { 035 private File sourceFile; 036 private String delimiter = "\\z"; 037 038 private List<Object> defaultBodies; 039 040 public FileDataSet(String sourceFileName) throws IOException { 041 this(new File(sourceFileName)); 042 } 043 044 public FileDataSet(File sourceFile) throws IOException { 045 this(sourceFile, "\\z"); 046 } 047 048 public FileDataSet(String sourceFileName, String delimiter) throws IOException { 049 this(new File(sourceFileName), delimiter); 050 } 051 052 public FileDataSet(File sourceFile, String delimiter) throws IOException { 053 setSourceFile(sourceFile, delimiter); 054 } 055 056 // Properties 057 //------------------------------------------------------------------------- 058 059 public File getSourceFile() { 060 return sourceFile; 061 } 062 063 public void setSourceFile(File sourceFile) throws IOException { 064 this.sourceFile = sourceFile; 065 readSourceFile(); 066 } 067 068 public void setSourceFile(File sourceFile, String delimiter) throws IOException { 069 this.sourceFile = sourceFile; 070 this.delimiter = delimiter; 071 readSourceFile(); 072 } 073 074 public String getDelimiter() { 075 return delimiter; 076 } 077 078 // Implementation methods 079 //------------------------------------------------------------------------- 080 081 private void readSourceFile() throws IOException { 082 List<Object> bodies = new LinkedList<>(); 083 try (BufferedReader br = new BufferedReader(new FileReader(sourceFile))) { 084 Scanner scanner = new Scanner(br); 085 scanner.useDelimiter(delimiter); 086 while (scanner.hasNext()) { 087 String nextPayload = scanner.next(); 088 if ((nextPayload != null) && (nextPayload.length() > 0)) { 089 bodies.add(nextPayload); 090 } 091 } 092 setDefaultBodies(bodies); 093 } 094 } 095}