/* Copyright 2013 Nationale-Nederlanden Licensed 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 nl.nn.adapterframework.pipes; import java.io.File; import java.io.FileReader; import java.io.Reader; import java.util.Map; import nl.nn.adapterframework.core.IPipeLineSession; import nl.nn.adapterframework.core.PipeRunException; import nl.nn.adapterframework.core.PipeRunResult; import nl.nn.adapterframework.core.SenderException; import nl.nn.adapterframework.util.ClassUtils; import org.apache.commons.lang.StringUtils; /** * Sends a message to a Sender for each line of the file that the input message refers to. * * <p><b>Configuration </b><i>(where deviating from IteratingPipe)</i><b>:</b> * <table border="1"> * <tr><th>attributes</th><th>description</th><th>default</th></tr> * <tr><td>classname</td><td>nl.nn.adapterframework.pipes.FileLineIteratorPipe</td><td> </td></tr> * <tr><td>{@link #setMove2dirAfterTransform(String) move2dirAfterTransform}</td><td>Directory in which the transformed file(s) is stored</td><td> </td></tr> * <tr><td>{@link #setMove2dirAfterError(String) move2dirAfterError}</td><td>Directory to which the inputfile is moved in case an error occurs</td><td> </td></tr> * </table> * </p> * * @author Gerrit van Brakel */ public class FileLineIteratorPipe extends StreamLineIteratorPipe { private String move2dirAfterTransform; private String move2dirAfterError; protected Reader getReader(Object input, IPipeLineSession session, String correlationID, Map threadContext) throws SenderException { if (input==null) { throw new SenderException("got null input instead of String containing filename"); } if (!(input instanceof File)) { throw new SenderException("expected File as input, got ["+ClassUtils.nameOf(input)+"], value ["+input+"]"); } File file = (File)input; try { return new FileReader(file); } catch (Exception e) { throw new SenderException("cannot open file ["+file.getPath()+"]",e); } } /** * Open a reader for the file named according the input messsage and * transform it. * Move the input file to a done directory when transformation is finished * and return the names of the generated files. * * @see nl.nn.adapterframework.core.IPipe#doPipe(java.lang.Object, nl.nn.adapterframework.core.IPipeLineSession) */ public PipeRunResult doPipe(Object input, IPipeLineSession session) throws PipeRunException { if (input==null) { throw new PipeRunException(this,"got null input instead of String containing filename"); } if (!(input instanceof String)) { throw new PipeRunException(this,"expected String containing filename as input, got ["+ClassUtils.nameOf(input)+"], value ["+input+"]"); } String filename = input.toString(); File file = new File(filename); try { PipeRunResult result = super.doPipe(file,session); if (! StringUtils.isEmpty(getMove2dirAfterTransform())) { File move2 = new File(getMove2dirAfterTransform(), file.getName()); file.renameTo(move2); } return result; } catch (PipeRunException e) { if (! StringUtils.isEmpty(getMove2dirAfterError())) { File move2 = new File(getMove2dirAfterError(), file.getName()); file.renameTo(move2); } throw e; } } /** * @param readyDir directory where input file is moved to in case of a succesful transformation */ public void setMove2dirAfterTransform(String readyDir) { move2dirAfterTransform = readyDir; } public String getMove2dirAfterTransform() { return move2dirAfterTransform; } /** * @param errorDir directory where input file is moved to in case of an error */ public void setMove2dirAfterError(String errorDir) { move2dirAfterError = errorDir; } public String getMove2dirAfterError() { return move2dirAfterError; } }