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.ObjectHelper;
027    
028    /**
029     * File endpoint.
030     */
031    public class FileEndpoint extends GenericFileEndpoint<File> {
032    
033        private FileOperations operations = new FileOperations(this);
034        private File file;
035    
036        public FileEndpoint() {
037            // use marker file as default exclusive read locks
038            this.readLock = "markerFile";
039        }
040    
041        public FileEndpoint(String endpointUri, Component component) {
042            super(endpointUri, component);
043            // use marker file as default exclusive read locks
044            this.readLock = "markerFile";
045        }
046    
047        public FileConsumer createConsumer(Processor processor) throws Exception {
048            ObjectHelper.notNull(operations, "operations");
049            ObjectHelper.notNull(file, "file");
050    
051            // we assume its a file if the name has a dot in it (eg foo.txt)
052            if (file.getName().contains(".")) {
053                throw new IllegalArgumentException("Only directory is supported. Endpoint must be configured with a valid starting directory: " + file);
054            }
055    
056            // auto create starting directory if needed
057            if (!file.exists() && !file.isDirectory()) {
058                if (isAutoCreate()) {
059                    if (log.isDebugEnabled()) {
060                        log.debug("Creating non existing starting directory: " + file);
061                    }
062                    boolean absolute = file.isAbsolute();
063                    operations.buildDirectory(file.getPath(), absolute);
064                }
065            }
066    
067            FileConsumer result = new FileConsumer(this, 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() && !isIdempotent()) {
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 (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    
088            configureConsumer(result);
089            return result;
090        }
091    
092        public GenericFileProducer<File> createProducer() throws Exception {
093            ObjectHelper.notNull(operations, "operations");
094            return new GenericFileProducer<File>(this, operations);
095        }
096    
097        public Exchange createExchange(GenericFile<File> file) {
098            Exchange exchange = new DefaultExchange(this);
099            if (file != null) {
100                file.bindToExchange(exchange);
101            }
102            return exchange;
103        }
104    
105        public File getFile() {
106            return file;
107        }
108    
109        public void setFile(File file) {
110            this.file = file;
111            // update configuration as well
112            getConfiguration().setDirectory(file.getPath());
113        }
114    
115        @Override
116        public String getScheme() {
117            return "file";
118        }
119    
120        @Override
121        protected String createEndpointUri() {
122            return "file://" + getFile().getAbsolutePath();
123        }
124    
125        @Override
126        public char getFileSeparator() {       
127            return File.separatorChar;
128        }
129    
130        @Override
131        public boolean isAbsolute(String name) {
132            // relative or absolute path?
133            File file = new File(name);
134            return file.isAbsolute();
135        }
136    
137    }