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.File; 020import java.io.IOException; 021import java.util.LinkedList; 022import java.util.List; 023 024import org.apache.camel.util.Scanner; 025 026/** 027 * A DataSet that reads payloads from a file that are used to create each message exchange 028 * along with using a pluggable transformer to customize the messages. The file contents may optionally 029 * be split using a supplied token. 030 * 031 * @version 032 */ 033public class FileDataSet extends ListDataSet { 034 private File sourceFile; 035 private String delimiter = "\\z"; 036 037 private List<Object> defaultBodies; 038 039 public FileDataSet(String sourceFileName) throws IOException { 040 this(new File(sourceFileName)); 041 } 042 043 public FileDataSet(File sourceFile) throws IOException { 044 this(sourceFile, "\\z"); 045 } 046 047 public FileDataSet(String sourceFileName, String delimiter) throws IOException { 048 this(new File(sourceFileName), delimiter); 049 } 050 051 public FileDataSet(File sourceFile, String delimiter) throws IOException { 052 setSourceFile(sourceFile, delimiter); 053 } 054 055 // Properties 056 //------------------------------------------------------------------------- 057 058 public File getSourceFile() { 059 return sourceFile; 060 } 061 062 public void setSourceFile(File sourceFile) throws IOException { 063 this.sourceFile = sourceFile; 064 readSourceFile(); 065 } 066 067 public void setSourceFile(File sourceFile, String delimiter) throws IOException { 068 this.sourceFile = sourceFile; 069 this.delimiter = delimiter; 070 readSourceFile(); 071 } 072 073 public String getDelimiter() { 074 return delimiter; 075 } 076 077 // Implementation methods 078 //------------------------------------------------------------------------- 079 080 private void readSourceFile() throws IOException { 081 List<Object> bodies = new LinkedList<>(); 082 try (Scanner scanner = new Scanner(sourceFile, null, delimiter)) { 083 while (scanner.hasNext()) { 084 String nextPayload = scanner.next(); 085 if ((nextPayload != null) && (nextPayload.length() > 0)) { 086 bodies.add(nextPayload); 087 } 088 } 089 setDefaultBodies(bodies); 090 } 091 } 092}