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.Expression;
020 import org.apache.camel.Message;
021 import org.apache.camel.component.file.FileComponent;
022 import org.apache.camel.impl.DefaultProducer;
023 import org.apache.camel.language.simple.FileLanguage;
024 import org.apache.commons.logging.Log;
025 import org.apache.commons.logging.LogFactory;
026
027 public abstract class RemoteFileProducer<T extends RemoteFileExchange> extends DefaultProducer<T> {
028 protected final transient Log log = LogFactory.getLog(getClass());
029 protected RemoteFileEndpoint<T> endpoint;
030
031 protected RemoteFileProducer(RemoteFileEndpoint<T> endpoint) {
032 super(endpoint);
033 this.endpoint = endpoint;
034 }
035
036 protected String createFileName(Message message, RemoteFileConfiguration fileConfig) {
037 String answer;
038
039 String name = message.getHeader(FileComponent.HEADER_FILE_NAME, String.class);
040
041 // expression support
042 Expression expression = endpoint.getConfiguration().getExpression();
043 if (name != null) {
044 // the header name can be an expression too, that should override whatever configured on the endpoint
045 if (name.indexOf("${") > -1) {
046 if (log.isDebugEnabled()) {
047 log.debug(FileComponent.HEADER_FILE_NAME + " contains a FileLanguage expression: " + name);
048 }
049 expression = FileLanguage.file(name);
050 }
051 }
052 if (expression != null) {
053 if (log.isDebugEnabled()) {
054 log.debug("Filename evaluated as expression: " + expression);
055 }
056 Object result = expression.evaluate(message.getExchange());
057 name = message.getExchange().getContext().getTypeConverter().convertTo(String.class, result);
058 }
059
060 String endpointFile = fileConfig.getFile();
061 if (fileConfig.isDirectory()) {
062 // If the path isn't empty, we need to add a trailing / if it isn't already there
063 String baseDir = "";
064 if (endpointFile.length() > 0) {
065 baseDir = endpointFile + (endpointFile.endsWith("/") ? "" : "/");
066 }
067 String fileName = (name != null) ? name : endpoint.getGeneratedFileName(message);
068 answer = baseDir + fileName;
069 } else {
070 answer = endpointFile;
071 }
072
073 // lets store the name we really used in the header, so end-users can retrieve it
074 message.setHeader(FileComponent.HEADER_FILE_NAME_PRODUCED, answer);
075
076 return answer;
077 }
078
079 protected String remoteServer() {
080 return endpoint.getConfiguration().remoteServerInformation();
081 }
082
083 @Override
084 protected void doStart() throws Exception {
085 log.info("Starting");
086 // do not connect when component starts, just wait until we process as we will
087 // connect at that time if needed
088 super.doStart();
089 }
090
091 @Override
092 protected void doStop() throws Exception {
093 log.info("Stopping");
094 // disconnect when stopping
095 try {
096 disconnect();
097 } catch (Exception e) {
098 // ignore just log a warning
099 log.warn("Exception occured during disconecting from " + remoteServer() + ". "
100 + e.getClass().getCanonicalName() + " message: " + e.getMessage());
101 }
102 super.doStop();
103 }
104
105 protected abstract void connectIfNecessary() throws Exception;
106
107 protected abstract void disconnect() throws Exception;
108 }