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