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