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
021 import org.apache.camel.Component;
022 import org.apache.camel.Processor;
023 import org.apache.camel.processor.idempotent.MemoryIdempotentRepository;
024 import org.apache.camel.util.ObjectHelper;
025
026 /**
027 * File endpoint.
028 */
029 public class FileEndpoint extends GenericFileEndpoint<File> {
030
031 private FileOperations operations = new FileOperations(this);
032 private File file;
033
034 public FileEndpoint() {
035 // use marker file as default exclusive read locks
036 this.readLock = "markerFile";
037 }
038
039 public FileEndpoint(String endpointUri, Component component) {
040 super(endpointUri, component);
041 // use marker file as default exclusive read locks
042 this.readLock = "markerFile";
043 }
044
045 public FileConsumer createConsumer(Processor processor) throws Exception {
046 ObjectHelper.notNull(operations, "operations");
047 ObjectHelper.notNull(file, "file");
048
049 // we assume its a file if the name has a dot in it (eg foo.txt)
050 if (file.getName().contains(".")) {
051 throw new IllegalArgumentException("Only directory is supported. Endpoint must be configured with a valid starting directory: " + file);
052 }
053
054 FileConsumer result = new FileConsumer(this, processor, operations);
055
056 if (isDelete() && getMove() != null) {
057 throw new IllegalArgumentException("You cannot set both delete=true and move options");
058 }
059
060 // if noop=true then idempotent should also be configured
061 if (isNoop() && !isIdempotent()) {
062 log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
063 setIdempotent(true);
064 }
065
066 // if idempotent and no repository set then create a default one
067 if (isIdempotent() && idempotentRepository == null) {
068 log.info("Using default memory based idempotent repository with cache max size: " + DEFAULT_IDEMPOTENT_CACHE_SIZE);
069 idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
070 }
071
072 configureConsumer(result);
073 return result;
074 }
075
076 public GenericFileProducer<File> createProducer() throws Exception {
077 ObjectHelper.notNull(operations, "operations");
078 return new GenericFileProducer<File>(this, operations);
079 }
080
081 public GenericFileExchange<File> createExchange(GenericFile<File> file) {
082 GenericFileExchange<File> exchange = new GenericFileExchange<File>(getCamelContext());
083 exchange.setGenericFile(file);
084 return exchange;
085 }
086
087 public GenericFileExchange createExchange() {
088 return new GenericFileExchange(getCamelContext());
089 }
090
091 public FileOperations getOperations() {
092 return operations;
093 }
094
095 public void setOperations(FileOperations operations) {
096 this.operations = operations;
097 }
098
099 public File getFile() {
100 return file;
101 }
102
103 public void setFile(File file) {
104 this.file = file;
105 // update configuration as well
106 getConfiguration().setDirectory(file.getPath());
107 }
108
109 @Override
110 public String getScheme() {
111 return "file";
112 }
113
114 @Override
115 protected String createEndpointUri() {
116 return "file://" + getFile().getAbsolutePath();
117 }
118
119 @Override
120 public char getFileSeparator() {
121 return File.separatorChar;
122 }
123
124 @Override
125 public boolean isAbsolute(String name) {
126 // relative or absolute path?
127 File file = new File(name);
128 return file.isAbsolute();
129 }
130
131 }