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 connectIfNecessary(); 048 if (!loggedIn) { 049 String message = "Could not connect/login to: " + remoteServer() + ". Will skip this poll."; 050 log.warn(message); 051 return false; 052 } 053 return true; 054 } 055 056 @Override 057 protected void postPollCheck() { 058 if (getEndpoint().isDisconnect()) { 059 if (log.isTraceEnabled()) { 060 log.trace("postPollCheck disconnect from: " + getEndpoint()); 061 } 062 disconnect(); 063 } 064 } 065 066 @Override 067 protected void doStop() throws Exception { 068 super.doStop(); 069 disconnect(); 070 } 071 072 protected void disconnect() { 073 // disconnect when stopping 074 try { 075 if (getOperations().isConnected()) { 076 loggedIn = false; 077 if (log.isDebugEnabled()) { 078 log.debug("Disconnecting from: " + remoteServer()); 079 } 080 getOperations().disconnect(); 081 } 082 } catch (GenericFileOperationFailedException e) { 083 // ignore just log a warning 084 log.warn(e.getMessage()); 085 } 086 } 087 088 protected void connectIfNecessary() throws IOException { 089 if (!loggedIn) { 090 if (log.isDebugEnabled()) { 091 log.debug("Not connected/logged in, connecting to: " + remoteServer()); 092 } 093 loggedIn = getOperations().connect((RemoteFileConfiguration) endpoint.getConfiguration()); 094 if (loggedIn) { 095 log.info("Connected and logged in to: " + remoteServer()); 096 } 097 } 098 } 099 100 /** 101 * Returns human readable server information for logging purpose 102 */ 103 protected String remoteServer() { 104 return ((RemoteFileEndpoint) endpoint).remoteServerInformation(); 105 } 106 }