KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > net > FTPSender


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.servicemix.components.net;
18
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStream JavaDoc;
21
22 import javax.jbi.JBIException;
23 import javax.jbi.messaging.MessageExchange;
24 import javax.jbi.messaging.NormalizedMessage;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.commons.net.SocketClient;
29 import org.apache.commons.net.ftp.FTPClient;
30 import org.apache.servicemix.components.util.DefaultFileMarshaler;
31 import org.apache.servicemix.components.util.FileMarshaler;
32 import org.apache.servicemix.components.util.OutBinding;
33
34 /**
35  * A component which sends a message to a file using FTP via the
36  * <a HREF="http://jakarta.apache.org/commons/net.html">Jakarta Commons Net</a> library
37  *
38  * @version $Revision: 426415 $
39  */

40 public class FTPSender extends OutBinding {
41     private static final Log log = LogFactory.getLog(FTPSender.class);
42
43     private FTPClientPool clientPool;
44     private FileMarshaler marshaler = new DefaultFileMarshaler();
45     private String JavaDoc uniqueFileName = "ServiceMix";
46     private boolean overwrite = false;
47
48
49     // Properties
50
//-------------------------------------------------------------------------
51
public FTPClientPool getClientPool() {
52         return clientPool;
53     }
54
55     public void setClientPool(FTPClientPool clientPool) {
56         this.clientPool = clientPool;
57     }
58
59     public FileMarshaler getMarshaler() {
60         return marshaler;
61     }
62
63     public void setMarshaler(FileMarshaler marshaler) {
64         this.marshaler = marshaler;
65     }
66
67     public String JavaDoc getUniqueFileName() {
68         return uniqueFileName;
69     }
70
71     /**
72      * Sets the name used to make a unique name if no file name is available on the message.
73      *
74      * @param uniqueFileName the new value of the unique name to use for generating unique names
75      */

76     public void setUniqueFileName(String JavaDoc uniqueFileName) {
77         this.uniqueFileName = uniqueFileName;
78     }
79
80     public boolean isOverwrite() {
81         return overwrite;
82     }
83
84     public void setOverwrite(boolean overwrite) {
85         this.overwrite = overwrite;
86     }
87
88     // Implementation methods
89
//-------------------------------------------------------------------------
90
protected void init() throws JBIException {
91         if (clientPool == null) {
92             throw new IllegalArgumentException JavaDoc("You must initialise the clientPool property");
93         }
94         super.init();
95     }
96
97     protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception JavaDoc {
98         FTPClient client = null;
99         OutputStream JavaDoc out = null;
100         try {
101             client = (FTPClient) getClientPool().borrowClient();
102
103             String JavaDoc name = marshaler.getOutputName(exchange, message);
104             if (name == null) {
105                 if (uniqueFileName != null) {
106                     out = client.storeUniqueFileStream(uniqueFileName);
107                 }
108                 else {
109                     out = client.storeUniqueFileStream();
110                 }
111             }
112             else {
113                 out = client.storeFileStream(name);
114                 if (out == null) {
115                     // lets try overwrite the previous file?
116
if (overwrite) {
117                         client.deleteFile(name);
118                     }
119                     out = client.storeFileStream(name);
120                 }
121             }
122             if (out == null) {
123                 throw new IOException JavaDoc("No output stream available for output name: " + name + ". Maybe the file already exists?");
124             }
125             marshaler.writeMessage(exchange, message, out, name);
126             done(exchange);
127         }
128         finally {
129             returnClient(client);
130             if (out != null) {
131                 try {
132                     out.close();
133                 }
134                 catch (IOException JavaDoc e) {
135                     log.error("Caught exception while closing stream on error: " + e, e);
136                 }
137             }
138         }
139     }
140
141     protected void returnClient(SocketClient client) {
142         if (client != null) {
143             try {
144                 getClientPool().returnClient(client);
145             }
146             catch (Exception JavaDoc e) {
147                 log.error("Failed to return client to pool: " + e, e);
148             }
149         }
150     }
151
152 }
153
Popular Tags