001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.camel.component.file;
019    
020    import org.apache.camel.Consumer;
021    import org.apache.camel.Processor;
022    import org.apache.camel.Producer;
023    import org.apache.camel.impl.ScheduledPollEndpoint;
024    
025    import java.io.File;
026    
027    /**
028     * A <a href="http://activemq.apache.org/camel/file.html">File Endpoint</a> for working with file systems
029     *
030     * @version $Revision: 523016 $
031     */
032    public class FileEndpoint extends ScheduledPollEndpoint<FileExchange> {
033        private File file;
034        private boolean autoCreate=true;
035    
036        protected FileEndpoint(File file, String endpointUri, FileComponent component) {
037            super(endpointUri, component);
038            this.file = file;
039        }
040    
041        /**
042         * @return a Producer
043         * @throws Exception
044         * @see org.apache.camel.Endpoint#createProducer()
045         */
046        public Producer<FileExchange> createProducer() throws Exception {
047            Producer<FileExchange> result = new FileProducer(this);
048            return result;
049        }
050    
051        /**
052         * @param file
053         * @return a Consumer
054         * @throws Exception
055         * @see org.apache.camel.Endpoint#createConsumer(org.apache.camel.Processor)
056         */
057        public Consumer<FileExchange> createConsumer(Processor file) throws Exception {
058            Consumer<FileExchange> result = new FileConsumer(this, file);
059            configureConsumer(result);
060            return result;
061        }
062    
063        /**
064         * @param file
065         * @return a FileExchange
066         * @see org.apache.camel.Endpoint#createExchange()
067         */
068        public FileExchange createExchange(File file) {
069            return new FileExchange(getContext(), file);
070        }
071    
072        /**
073         * @return an Exchange
074         * @see org.apache.camel.Endpoint#createExchange()
075         */
076        public FileExchange createExchange() {
077            return createExchange(getFile());
078        }
079    
080        public File getFile() {
081            if (autoCreate && !file.exists()) {
082                file.mkdirs();
083            }
084            return file;
085        }
086    
087        public boolean isSingleton() {
088            return true;
089        }
090    
091        
092        /**
093         * @return the autoCreate
094         */
095        public boolean isAutoCreate(){
096            return this.autoCreate;
097        }
098    
099        
100        /**
101         * @param autoCreate the autoCreate to set
102         */
103        public void setAutoCreate(boolean autoCreate){
104            this.autoCreate=autoCreate;
105        }
106    }