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