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 */ 017 package org.apache.camel.component.file; 018 019 import java.io.File; 020 import java.io.FileNotFoundException; 021 022 import org.apache.camel.Component; 023 import org.apache.camel.Exchange; 024 import org.apache.camel.Processor; 025 import org.apache.camel.processor.idempotent.MemoryIdempotentRepository; 026 import org.apache.camel.spi.UriEndpoint; 027 import org.apache.camel.spi.UriParam; 028 import org.apache.camel.spi.UriPath; 029 import org.apache.camel.util.FileUtil; 030 import org.apache.camel.util.ObjectHelper; 031 032 /** 033 * File endpoint. 034 */ 035 @UriEndpoint(scheme = "file", consumerClass = FileConsumer.class) 036 public class FileEndpoint extends GenericFileEndpoint<File> { 037 038 private final FileOperations operations = new FileOperations(this); 039 @UriPath 040 private File file; 041 @UriParam 042 private boolean copyAndDeleteOnRenameFail = true; 043 @UriParam 044 private boolean forceWrites = true; 045 046 public FileEndpoint() { 047 // use marker file as default exclusive read locks 048 this.readLock = "markerFile"; 049 } 050 051 public FileEndpoint(String endpointUri, Component component) { 052 super(endpointUri, component); 053 // use marker file as default exclusive read locks 054 this.readLock = "markerFile"; 055 } 056 057 public FileConsumer createConsumer(Processor processor) throws Exception { 058 ObjectHelper.notNull(operations, "operations"); 059 ObjectHelper.notNull(file, "file"); 060 061 // auto create starting directory if needed 062 if (!file.exists() && !file.isDirectory()) { 063 if (isAutoCreate()) { 064 log.debug("Creating non existing starting directory: {}", file); 065 boolean absolute = FileUtil.isAbsolute(file); 066 boolean created = operations.buildDirectory(file.getPath(), absolute); 067 if (!created) { 068 log.warn("Cannot auto create starting directory: {}", file); 069 } 070 } else if (isStartingDirectoryMustExist()) { 071 throw new FileNotFoundException("Starting directory does not exist: " + file); 072 } 073 } 074 075 FileConsumer result = newFileConsumer(processor, operations); 076 077 if (isDelete() && getMove() != null) { 078 throw new IllegalArgumentException("You cannot set both delete=true and move options"); 079 } 080 081 // if noop=true then idempotent should also be configured 082 if (isNoop() && !isIdempotentSet()) { 083 log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well"); 084 setIdempotent(true); 085 } 086 087 // if idempotent and no repository set then create a default one 088 if (isIdempotentSet() && isIdempotent() && idempotentRepository == null) { 089 log.info("Using default memory based idempotent repository with cache max size: " + DEFAULT_IDEMPOTENT_CACHE_SIZE); 090 idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE); 091 } 092 093 // set max messages per poll 094 result.setMaxMessagesPerPoll(getMaxMessagesPerPoll()); 095 result.setEagerLimitMaxMessagesPerPoll(isEagerMaxMessagesPerPoll()); 096 097 configureConsumer(result); 098 return result; 099 } 100 101 public GenericFileProducer<File> createProducer() throws Exception { 102 ObjectHelper.notNull(operations, "operations"); 103 104 // you cannot use temp file and file exists append 105 if (getFileExist() == GenericFileExist.Append && ((getTempPrefix() != null) || (getTempFileName() != null))) { 106 throw new IllegalArgumentException("You cannot set both fileExist=Append and tempPrefix/tempFileName options"); 107 } 108 109 // ensure fileExist and moveExisting is configured correctly if in use 110 if (getFileExist() == GenericFileExist.Move && getMoveExisting() == null) { 111 throw new IllegalArgumentException("You must configure moveExisting option when fileExist=Move"); 112 } else if (getMoveExisting() != null && getFileExist() != GenericFileExist.Move) { 113 throw new IllegalArgumentException("You must configure fileExist=Move when moveExisting has been set"); 114 } 115 116 return new GenericFileProducer<File>(this, operations); 117 } 118 119 public Exchange createExchange(GenericFile<File> file) { 120 Exchange exchange = createExchange(); 121 if (file != null) { 122 file.bindToExchange(exchange); 123 } 124 return exchange; 125 } 126 127 /** 128 * Strategy to create a new {@link FileConsumer} 129 * 130 * @param processor the given processor 131 * @param operations file operations 132 * @return the created consumer 133 */ 134 protected FileConsumer newFileConsumer(Processor processor, GenericFileOperations<File> operations) { 135 return new FileConsumer(this, processor, operations); 136 } 137 138 public File getFile() { 139 return file; 140 } 141 142 public void setFile(File file) { 143 this.file = file; 144 // update configuration as well 145 getConfiguration().setDirectory(FileUtil.isAbsolute(file) ? file.getAbsolutePath() : file.getPath()); 146 } 147 148 @Override 149 public String getScheme() { 150 return "file"; 151 } 152 153 @Override 154 protected String createEndpointUri() { 155 return getFile().toURI().toString(); 156 } 157 158 @Override 159 public char getFileSeparator() { 160 return File.separatorChar; 161 } 162 163 @Override 164 public boolean isAbsolute(String name) { 165 // relative or absolute path? 166 return FileUtil.isAbsolute(new File(name)); 167 } 168 169 public boolean isCopyAndDeleteOnRenameFail() { 170 return copyAndDeleteOnRenameFail; 171 } 172 173 public void setCopyAndDeleteOnRenameFail(boolean copyAndDeleteOnRenameFail) { 174 this.copyAndDeleteOnRenameFail = copyAndDeleteOnRenameFail; 175 } 176 177 public boolean isForceWrites() { 178 return forceWrites; 179 } 180 181 public void setForceWrites(boolean forceWrites) { 182 this.forceWrites = forceWrites; 183 } 184 }