1 17 package org.apache.servicemix.components.net; 18 19 import java.io.IOException ; 20 import java.io.OutputStream ; 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 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 uniqueFileName = "ServiceMix"; 46 private boolean overwrite = false; 47 48 49 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 getUniqueFileName() { 68 return uniqueFileName; 69 } 70 71 76 public void setUniqueFileName(String 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 protected void init() throws JBIException { 91 if (clientPool == null) { 92 throw new IllegalArgumentException ("You must initialise the clientPool property"); 93 } 94 super.init(); 95 } 96 97 protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception { 98 FTPClient client = null; 99 OutputStream out = null; 100 try { 101 client = (FTPClient) getClientPool().borrowClient(); 102 103 String 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 if (overwrite) { 117 client.deleteFile(name); 118 } 119 out = client.storeFileStream(name); 120 } 121 } 122 if (out == null) { 123 throw new IOException ("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 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 e) { 147 log.error("Failed to return client to pool: " + e, e); 148 } 149 } 150 } 151 152 } 153 | Popular Tags |