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 Exchange createExchange(GenericFile<T> file) {
048            Exchange answer = new DefaultExchange(this);
049            if (file != null) {
050                file.bindToExchange(answer);
051            }
052            return answer;
053        }
054    
055        @Override
056        public GenericFileProducer<T> createProducer() throws Exception {
057            afterPropertiesSet();
058            return buildProducer();
059        }
060    
061        @Override
062        public RemoteFileConsumer<T> createConsumer(Processor processor) throws Exception {
063            afterPropertiesSet();
064            RemoteFileConsumer<T> consumer = buildConsumer(processor);
065    
066            if (isDelete() && getMove() != null) {
067                throw new IllegalArgumentException("You cannot both set delete=true and move options");
068            }
069            // if noop=true then idempotent should also be configured
070            if (isNoop() && !isIdempotent()) {
071                log.info("Endpoint is configured with noop=true so forcing endpoint to be idempotent as well");
072                setIdempotent(true);
073            }
074    
075            // if idempotent and no repository set then create a default one
076            if (isIdempotent() && idempotentRepository == null) {
077                log.info("Using default memory based idempotent repository with cache max size: " + DEFAULT_IDEMPOTENT_CACHE_SIZE);
078                idempotentRepository = MemoryIdempotentRepository.memoryIdempotentRepository(DEFAULT_IDEMPOTENT_CACHE_SIZE);
079            }
080    
081            // set max messages per poll
082            consumer.setMaxMessagesPerPoll(getMaxMessagesPerPoll());
083    
084            configureConsumer(consumer);
085            return consumer;
086        }
087    
088        /**
089         * Validates this endpoint if its configured properly.
090         *
091         * @throws Exception is thrown if endpoint is invalid configured for its mandatory options
092         */
093        protected void afterPropertiesSet() throws Exception {
094            RemoteFileConfiguration config = (RemoteFileConfiguration) getConfiguration();
095            ObjectHelper.notEmpty(config.getHost(), "host");
096            ObjectHelper.notEmpty(config.getProtocol(), "protocol");
097        }
098    
099        /**
100         * Remote File Endpoints, impl this method to create a custom consumer specific to their "protocol" etc.
101         *
102         * @param processor  the processor
103         * @return the created consumer
104         */
105        protected abstract RemoteFileConsumer<T> buildConsumer(Processor processor);
106    
107        /**
108         * Remote File Endpoints, impl this method to create a custom producer specific to their "protocol" etc.
109         *
110         * @return the created producer
111         */
112        protected abstract GenericFileProducer<T> buildProducer();
113    
114        /**
115         * Returns human readable server information for logging purpose
116         */
117        public String remoteServerInformation() {
118            return ((RemoteFileConfiguration) configuration).remoteServerInformation();
119        }
120        
121        @Override
122        public char getFileSeparator() {       
123            return '/';
124        }
125        
126        @Override
127        public boolean isAbsolute(String name) {        
128            return name.startsWith("/");
129        }
130    
131        public int getMaximumReconnectAttempts() {
132            return maximumReconnectAttempts;
133        }
134    
135        public void setMaximumReconnectAttempts(int maximumReconnectAttempts) {
136            this.maximumReconnectAttempts = maximumReconnectAttempts;
137        }
138    
139        public long getReconnectDelay() {
140            return reconnectDelay;
141        }
142    
143        public void setReconnectDelay(long reconnectDelay) {
144            this.reconnectDelay = reconnectDelay;
145        }
146    
147        public boolean isDisconnect() {
148            return disconnect;
149        }
150    
151        public void setDisconnect(boolean disconnect) {
152            this.disconnect = disconnect;
153        }
154    }