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    
037        public RemoteFileEndpoint() {
038            // no args constructor for spring bean endpoint configuration
039        }
040    
041        public RemoteFileEndpoint(String uri, RemoteFileComponent<T> component, RemoteFileConfiguration configuration) {
042            super(uri, component);
043            this.configuration = configuration;
044        }
045    
046        @Override
047        public RemoteFileConfiguration getConfiguration() {
048            return (RemoteFileConfiguration) this.configuration;
049        }
050    
051        @Override
052        public Exchange createExchange(GenericFile<T> file) {
053            Exchange answer = new DefaultExchange(this);
054            if (file != null) {
055                file.bindToExchange(answer);
056            }
057            return answer;
058        }
059    
060        @Override
061        public GenericFileProducer<T> createProducer() throws Exception {
062            afterPropertiesSet();
063            return buildProducer();
064        }
065    
066        @Override
067        public RemoteFileConsumer<T> createConsumer(Processor processor) throws Exception {
068            afterPropertiesSet();
069            RemoteFileConsumer<T> consumer = buildConsumer(processor);
070    
071            if (isDelete() && getMove() != null) {
072                throw new IllegalArgumentException("You cannot both set delete=true and move options");
073            }
074            // if noop=true then idempotent should also be configured
075            if (isNoop() && !isIdempotent()) {
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 (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            consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
088    
089            configureConsumer(consumer);
090            return consumer;
091        }
092    
093        /**
094         * Validates this endpoint if its configured properly.
095         *
096         * @throws Exception is thrown if endpoint is invalid configured for its mandatory options
097         */
098        protected void afterPropertiesSet() throws Exception {
099            RemoteFileConfiguration config = getConfiguration();
100            ObjectHelper.notEmpty(config.getHost(), "host");
101            ObjectHelper.notEmpty(config.getProtocol(), "protocol");
102        }
103    
104        /**
105         * Remote File Endpoints, impl this method to create a custom consumer specific to their "protocol" etc.
106         *
107         * @param processor  the processor
108         * @return the created consumer
109         */
110        protected abstract RemoteFileConsumer<T> buildConsumer(Processor processor);
111    
112        /**
113         * Remote File Endpoints, impl this method to create a custom producer specific to their "protocol" etc.
114         *
115         * @return the created producer
116         */
117        protected abstract GenericFileProducer<T> buildProducer();
118    
119        /**
120         * Creates the operations to be used by the consumer or producer.
121         *
122         * @return a new created operations
123         * @throws Exception is thrown if error creating operations.
124         */
125        public abstract RemoteFileOperations<T> createRemoteFileOperations() throws Exception;
126    
127        /**
128         * Returns human readable server information for logging purpose
129         */
130        public String remoteServerInformation() {
131            return ((RemoteFileConfiguration) configuration).remoteServerInformation();
132        }
133        
134        @Override
135        public char getFileSeparator() {       
136            return '/';
137        }
138        
139        @Override
140        public boolean isAbsolute(String name) {        
141            return name.startsWith("/");
142        }
143    
144        public int getMaximumReconnectAttempts() {
145            return maximumReconnectAttempts;
146        }
147    
148        public void setMaximumReconnectAttempts(int maximumReconnectAttempts) {
149            this.maximumReconnectAttempts = maximumReconnectAttempts;
150        }
151    
152        public long getReconnectDelay() {
153            return reconnectDelay;
154        }
155    
156        public void setReconnectDelay(long reconnectDelay) {
157            this.reconnectDelay = reconnectDelay;
158        }
159    
160        public boolean isDisconnect() {
161            return disconnect;
162        }
163    
164        public void setDisconnect(boolean disconnect) {
165            this.disconnect = disconnect;
166        }
167    }