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            // set max messages per poll
073            result.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
074    
075            configureConsumer(result);
076            return result;
077        }
078    
079        public GenericFileProducer<File> createProducer() throws Exception {
080            ObjectHelper.notNull(operations, "operations");
081            return new GenericFileProducer<File>(this, operations);
082        }
083    
084        public GenericFileExchange<File> createExchange(GenericFile<File> file) {
085            GenericFileExchange<File> exchange = new GenericFileExchange<File>(this);
086            exchange.setGenericFile(file);
087            return exchange;
088        }
089    
090        public GenericFileExchange createExchange() {
091            return new GenericFileExchange(this);
092        }
093    
094        public File getFile() {
095            return file;
096        }
097    
098        public void setFile(File file) {
099            this.file = file;
100            // update configuration as well
101            getConfiguration().setDirectory(file.getPath());
102        }
103    
104        @Override
105        public String getScheme() {
106            return "file";
107        }
108    
109        @Override
110        protected String createEndpointUri() {
111            return "file://" + getFile().getAbsolutePath();
112        }
113    
114        @Override
115        public char getFileSeparator() {       
116            return File.separatorChar;
117        }
118    
119        @Override
120        public boolean isAbsolute(String name) {
121            // relative or absolute path?
122            File file = new File(name);
123            return file.isAbsolute();
124        }
125    
126    }