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 036 public RemoteFileEndpoint() { 037 // no args constructor for spring bean endpoint configuration 038 } 039 040 public RemoteFileEndpoint(String uri, RemoteFileComponent<T> component, RemoteFileConfiguration configuration) { 041 super(uri, component); 042 this.configuration = configuration; 043 } 044 045 @Override 046 @SuppressWarnings("unchecked") 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 // we assume its a file if the name has a dot in it (eg foo.txt) 067 if (configuration.getDirectory().contains(".")) { 068 throw new IllegalArgumentException("Only directory is supported. Endpoint must be configured with a valid directory: " 069 + configuration.getDirectory()); 070 } 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 if (config.getPort() <= 0) { 104 throw new IllegalArgumentException("port is not assigned to a positive value"); 105 } 106 } 107 108 /** 109 * Remote File Endpoints, impl this method to create a custom consumer specific to their "protocol" etc. 110 * 111 * @param processor the processor 112 * @return the created consumer 113 */ 114 protected abstract RemoteFileConsumer<T> buildConsumer(Processor processor); 115 116 /** 117 * Remote File Endpoints, impl this method to create a custom producer specific to their "protocol" etc. 118 * 119 * @return the created producer 120 */ 121 protected abstract GenericFileProducer<T> buildProducer(); 122 123 /** 124 * Returns human readable server information for logging purpose 125 */ 126 public String remoteServerInformation() { 127 return ((RemoteFileConfiguration) configuration).remoteServerInformation(); 128 } 129 130 @Override 131 public char getFileSeparator() { 132 return '/'; 133 } 134 135 @Override 136 public boolean isAbsolute(String name) { 137 return name.startsWith("/"); 138 } 139 140 public int getMaximumReconnectAttempts() { 141 return maximumReconnectAttempts; 142 } 143 144 public void setMaximumReconnectAttempts(int maximumReconnectAttempts) { 145 this.maximumReconnectAttempts = maximumReconnectAttempts; 146 } 147 148 public long getReconnectDelay() { 149 return reconnectDelay; 150 } 151 152 public void setReconnectDelay(long reconnectDelay) { 153 this.reconnectDelay = reconnectDelay; 154 } 155 }