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.remote;
018    
019    import org.apache.camel.Exchange;
020    import org.apache.camel.Processor;
021    import org.apache.camel.component.file.GenericFile;
022    import org.apache.camel.component.file.GenericFileEndpoint;
023    import org.apache.camel.component.file.GenericFileProducer;
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     * Remote file endpoint.
030     */
031    public abstract class RemoteFileEndpoint<T> extends GenericFileEndpoint<T> {
032    
033        private int maximumReconnectAttempts = 3;
034        private long reconnectDelay = 1000;
035        private boolean disconnect;
036        private boolean fastExistsCheck;
037    
038        public RemoteFileEndpoint() {
039            // no args constructor for spring bean endpoint configuration
040            // for ftp we need to use higher interval/checkout that for files
041            setReadLockTimeout(20000);
042            setReadLockCheckInterval(5000);
043        }
044    
045        public RemoteFileEndpoint(String uri, RemoteFileComponent<T> component, RemoteFileConfiguration configuration) {
046            super(uri, component);
047            this.configuration = configuration;
048            // for ftp we need to use higher interval/checkout that for files
049            setReadLockTimeout(20000);
050            setReadLockCheckInterval(5000);
051        }
052    
053        @Override
054        public RemoteFileConfiguration getConfiguration() {
055            return (RemoteFileConfiguration) this.configuration;
056        }
057    
058        @Override
059        public Exchange createExchange(GenericFile<T> file) {
060            Exchange answer = new DefaultExchange(this);
061            if (file != null) {
062                file.bindToExchange(answer);
063            }
064            return answer;
065        }
066    
067        @Override
068        public GenericFileProducer<T> createProducer() throws Exception {
069            afterPropertiesSet();
070            return buildProducer();
071        }
072    
073        @Override
074        public RemoteFileConsumer<T> createConsumer(Processor processor) throws Exception {
075            afterPropertiesSet();
076            RemoteFileConsumer<T> consumer = buildConsumer(processor);
077    
078            if (isDelete() && getMove() != null) {
079                throw new IllegalArgumentException("You cannot both set delete=true and move options");
080            }
081            // if noop=true then idempotent should also be configured
082            if (isNoop() && !isIdempotent()) {
083                log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
084                setIdempotent(true);
085            }
086    
087            // if idempotent and no repository set then create a default one
088            if (isIdempotent() && idempotentRepository == null) {
089                log.info("Using default memory based idempotent repository with cache max size: " + DEFAULT_IDEMPOTENT_CACHE_SIZE);
090                idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
091            }
092    
093            // set max messages per poll
094            consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
095    
096            configureConsumer(consumer);
097            return consumer;
098        }
099    
100        /**
101         * Validates this endpoint if its configured properly.
102         *
103         * @throws Exception is thrown if endpoint is invalid configured for its mandatory options
104         */
105        protected void afterPropertiesSet() throws Exception {
106            RemoteFileConfiguration config = getConfiguration();
107            ObjectHelper.notEmpty(config.getHost(), "host");
108            ObjectHelper.notEmpty(config.getProtocol(), "protocol");
109        }
110    
111        /**
112         * Remote File Endpoints, impl this method to create a custom consumer specific to their "protocol" etc.
113         *
114         * @param processor  the processor
115         * @return the created consumer
116         */
117        protected abstract RemoteFileConsumer<T> buildConsumer(Processor processor);
118    
119        /**
120         * Remote File Endpoints, impl this method to create a custom producer specific to their "protocol" etc.
121         *
122         * @return the created producer
123         */
124        protected abstract GenericFileProducer<T> buildProducer();
125    
126        /**
127         * Creates the operations to be used by the consumer or producer.
128         *
129         * @return a new created operations
130         * @throws Exception is thrown if error creating operations.
131         */
132        public abstract RemoteFileOperations<T> createRemoteFileOperations() throws Exception;
133    
134        /**
135         * Returns human readable server information for logging purpose
136         */
137        public String remoteServerInformation() {
138            return ((RemoteFileConfiguration) configuration).remoteServerInformation();
139        }
140        
141        @Override
142        public char getFileSeparator() {       
143            return '/';
144        }
145        
146        @Override
147        public boolean isAbsolute(String name) {        
148            return name.startsWith("/");
149        }
150    
151        public int getMaximumReconnectAttempts() {
152            return maximumReconnectAttempts;
153        }
154    
155        public void setMaximumReconnectAttempts(int maximumReconnectAttempts) {
156            this.maximumReconnectAttempts = maximumReconnectAttempts;
157        }
158    
159        public long getReconnectDelay() {
160            return reconnectDelay;
161        }
162    
163        public void setReconnectDelay(long reconnectDelay) {
164            this.reconnectDelay = reconnectDelay;
165        }
166    
167        public boolean isDisconnect() {
168            return disconnect;
169        }
170    
171        public void setDisconnect(boolean disconnect) {
172            this.disconnect = disconnect;
173        }
174    
175        public boolean isFastExistsCheck() {
176            return fastExistsCheck;
177        }
178    
179        public void setFastExistsCheck(boolean fastExistsCheck) {
180            this.fastExistsCheck = fastExistsCheck;
181        }
182    
183    }