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 java.io.IOException; 020 021 import org.apache.camel.Processor; 022 import org.apache.camel.component.file.GenericFileConsumer; 023 import org.apache.camel.component.file.GenericFileOperationFailedException; 024 025 /** 026 * Base class for remote file consumers. 027 */ 028 public abstract class RemoteFileConsumer<T> extends GenericFileConsumer<T> { 029 protected boolean loggedIn; 030 031 public RemoteFileConsumer(RemoteFileEndpoint<T> endpoint, Processor processor, RemoteFileOperations<T> operations) { 032 super(endpoint, processor, operations); 033 this.setPollStrategy(new RemoteFilePollingConsumerPollStrategy()); 034 } 035 036 @Override 037 @SuppressWarnings("unchecked") 038 public RemoteFileEndpoint<T> getEndpoint() { 039 return (RemoteFileEndpoint<T>) super.getEndpoint(); 040 } 041 042 protected RemoteFileOperations getOperations() { 043 return (RemoteFileOperations) operations; 044 } 045 046 protected boolean prePollCheck() throws Exception { 047 try { 048 if (getEndpoint().getMaximumReconnectAttempts() > 0) { 049 // only use recoverable if we are allowed any re-connect attempts 050 recoverableConnectIfNecessary(); 051 } else { 052 connectIfNecessary(); 053 } 054 } catch (Exception e) { 055 loggedIn = false; 056 057 // login failed should we thrown exception 058 if (getEndpoint().getConfiguration().isThrowExceptionOnConnectFailed()) { 059 throw e; 060 } 061 } 062 063 if (!loggedIn) { 064 String message = "Cannot connect/login to: " + remoteServer() + ". Will skip this poll."; 065 log.warn(message); 066 return false; 067 } 068 069 return true; 070 } 071 072 @Override 073 protected void postPollCheck() { 074 if (getEndpoint().isDisconnect()) { 075 if (log.isTraceEnabled()) { 076 log.trace("postPollCheck disconnect from: " + getEndpoint()); 077 } 078 disconnect(); 079 } 080 } 081 082 @Override 083 protected void doStop() throws Exception { 084 super.doStop(); 085 disconnect(); 086 } 087 088 protected void disconnect() { 089 // disconnect when stopping 090 try { 091 if (getOperations().isConnected()) { 092 loggedIn = false; 093 if (log.isDebugEnabled()) { 094 log.debug("Disconnecting from: " + remoteServer()); 095 } 096 getOperations().disconnect(); 097 } 098 } catch (GenericFileOperationFailedException e) { 099 // ignore just log a warning 100 log.warn(e.getMessage()); 101 } 102 } 103 104 protected void recoverableConnectIfNecessary() throws Exception { 105 try { 106 connectIfNecessary(); 107 } catch (Exception e) { 108 if (log.isDebugEnabled()) { 109 log.debug("Could not connect to: " + getEndpoint() + ". Will try to recover.", e); 110 } 111 loggedIn = false; 112 } 113 114 // recover by re-creating operations which should most likely be able to recover 115 if (!loggedIn) { 116 if (log.isDebugEnabled()) { 117 log.debug("Trying to recover connection to: " + getEndpoint() + " with a fresh client."); 118 } 119 setOperations(getEndpoint().createRemoteFileOperations()); 120 connectIfNecessary(); 121 } 122 } 123 124 protected void connectIfNecessary() throws IOException { 125 if (!loggedIn) { 126 if (log.isDebugEnabled()) { 127 log.debug("Not connected/logged in, connecting to: " + remoteServer()); 128 } 129 loggedIn = getOperations().connect((RemoteFileConfiguration) endpoint.getConfiguration()); 130 if (loggedIn) { 131 log.info("Connected and logged in to: " + remoteServer()); 132 } 133 } 134 } 135 136 /** 137 * Returns human readable server information for logging purpose 138 */ 139 protected String remoteServer() { 140 return ((RemoteFileEndpoint) endpoint).remoteServerInformation(); 141 } 142 }