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 protected boolean prePollCheck() throws Exception { 037 connectIfNecessary(); 038 if (!loggedIn) { 039 String message = "Could not connect/login to: " + remoteServer() + ". Will skip this poll."; 040 log.warn(message); 041 return false; 042 } 043 return true; 044 } 045 046 @Override 047 protected void doStop() throws Exception { 048 super.doStop(); 049 disconnect(); 050 } 051 052 protected void disconnect() { 053 // disconnect when stopping 054 try { 055 if (((RemoteFileOperations) operations).isConnected()) { 056 loggedIn = false; 057 if (log.isDebugEnabled()) { 058 log.debug("Disconnecting from: " + remoteServer()); 059 } 060 ((RemoteFileOperations) operations).disconnect(); 061 } 062 } catch (GenericFileOperationFailedException e) { 063 // ignore just log a warning 064 log.warn(e.getMessage()); 065 } 066 } 067 068 protected void connectIfNecessary() throws IOException { 069 if (!((RemoteFileOperations) operations).isConnected() || !loggedIn) { 070 if (log.isDebugEnabled()) { 071 log.debug("Not connected/logged in, connecting to: " + remoteServer()); 072 } 073 loggedIn = ((RemoteFileOperations) operations).connect((RemoteFileConfiguration) endpoint.getConfiguration()); 074 if (loggedIn) { 075 log.info("Connected and logged in to: " + remoteServer()); 076 } 077 } 078 } 079 080 /** 081 * Returns human readable server information for logging purpose 082 */ 083 protected String remoteServer() { 084 return ((RemoteFileEndpoint) endpoint).remoteServerInformation(); 085 } 086 }