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.Exchange;
023    import org.apache.camel.Processor;
024    import org.apache.camel.impl.DefaultExchange;
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    
037        public FileEndpoint() {
038            // use marker file as default exclusive read locks
039            this.readLock = "markerFile";
040        }
041    
042        public FileEndpoint(String endpointUri, Component component) {
043            super(endpointUri, component);
044            // use marker file as default exclusive read locks
045            this.readLock = "markerFile";
046        }
047    
048        public FileConsumer createConsumer(Processor processor) throws Exception {
049            ObjectHelper.notNull(operations, "operations");
050            ObjectHelper.notNull(file, "file");
051    
052            // we assume its a file if the name has a dot in it (eg foo.txt)
053            if (file.getName().contains(".")) {
054                throw new IllegalArgumentException("Only directory is supported. Endpoint must be configured with a valid starting directory: " + file);
055            }
056    
057            // auto create starting directory if needed
058            if (!file.exists() && !file.isDirectory()) {
059                if (isAutoCreate()) {
060                    if (log.isDebugEnabled()) {
061                        log.debug("Creating non existing starting directory: " + file);
062                    }
063                    boolean absolute = FileUtil.isAbsolute(file);
064                    operations.buildDirectory(file.getPath(), absolute);
065                }
066            }
067    
068            FileConsumer result = new FileConsumer(this, processor, operations);
069    
070            if (isDelete() && getMove() != null) {
071                throw new IllegalArgumentException("You cannot set both delete=true and move options");
072            }
073    
074            // if noop=true then idempotent should also be configured
075            if (isNoop() && !isIdempotentSet()) {
076                log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
077                setIdempotent(true);
078            }
079    
080            // if idempotent and no repository set then create a default one
081            if (isIdempotentSet() && isIdempotent() && idempotentRepository == null) {
082                log.info("Using default memory based idempotent repository with cache max size: " + DEFAULT_IDEMPOTENT_CACHE_SIZE);
083                idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
084            }
085    
086            // set max messages per poll
087            result.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
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            return new GenericFileProducer<File>(this, operations);
102        }
103    
104        public Exchange createExchange(GenericFile<File> file) {
105            Exchange exchange = new DefaultExchange(this);
106            if (file != null) {
107                file.bindToExchange(exchange);
108            }
109            return exchange;
110        }
111    
112        public File getFile() {
113            return file;
114        }
115    
116        public void setFile(File file) {
117            this.file = file;
118            // update configuration as well
119            getConfiguration().setDirectory(file.getPath());
120        }
121    
122        @Override
123        public String getScheme() {
124            return "file";
125        }
126    
127        @Override
128        protected String createEndpointUri() {
129            return "file://" + getFile().getAbsolutePath();
130        }
131    
132        @Override
133        public char getFileSeparator() {       
134            return File.separatorChar;
135        }
136    
137        @Override
138        public boolean isAbsolute(String name) {
139            // relative or absolute path?
140            return FileUtil.isAbsolute(new File(name));
141        }
142    
143    }