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