1 17 package org.apache.servicemix.components.vfs; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.commons.vfs.FileContent; 22 import org.apache.commons.vfs.FileObject; 23 import org.apache.commons.vfs.FileSystemManager; 24 import org.apache.servicemix.components.util.DefaultFileMarshaler; 25 import org.apache.servicemix.components.util.FileMarshaler; 26 import org.apache.servicemix.components.util.OutBinding; 27 28 import javax.jbi.JBIException; 29 import javax.jbi.messaging.MessageExchange; 30 import javax.jbi.messaging.MessagingException; 31 import javax.jbi.messaging.NormalizedMessage; 32 import java.io.IOException ; 33 import java.io.OutputStream ; 34 35 42 public class FileWriter extends OutBinding { 43 private static final Log log = LogFactory.getLog(FileWriter.class); 44 45 private FileObject directory; 46 private FileObjectEditor editor = new FileObjectEditor(); 47 private FileMarshaler marshaler = new DefaultFileMarshaler(); 48 private String uniqueFileName = "ServiceMix"; 49 50 public FileObject getDirectory() { 53 return directory; 54 } 55 56 public void setDirectory(FileObject directory) { 57 this.directory = directory; 58 } 59 60 public String getPath() { 61 return editor.getPath(); 62 } 63 64 public void setPath(String path) { 65 editor.setPath(path); 66 } 67 68 public FileSystemManager getFileSystemManager() { 69 return editor.getFileSystemManager(); 70 } 71 72 public void setFileSystemManager(FileSystemManager fileSystemManager) { 73 editor.setFileSystemManager(fileSystemManager); 74 } 75 76 public FileMarshaler getMarshaler() { 77 return marshaler; 78 } 79 80 public void setMarshaler(FileMarshaler marshaler) { 81 this.marshaler = marshaler; 82 } 83 84 public String getUniqueFileName() { 85 return uniqueFileName; 86 } 87 88 93 public void setUniqueFileName(String uniqueFileName) { 94 this.uniqueFileName = uniqueFileName; 95 } 96 97 98 protected void init() throws JBIException { 101 if (directory == null) { 102 directory = editor.getFileObject(); 103 } 104 super.init(); 105 } 106 107 protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception { 108 OutputStream out = null; 109 try { 110 String name = marshaler.getOutputName(exchange, message); 111 if (name == null) { 112 throw new MessagingException("No output name available. Cannot output message!"); 113 } 114 directory.close(); FileObject newFile = directory.resolveFile(name); 116 newFile.close(); FileContent content = newFile.getContent(); 118 content.close(); 119 if (content != null) { 120 out = content.getOutputStream(); 121 } 122 if (out == null) { 123 throw new MessagingException("No output stream available for output name: " + name); 124 } 125 marshaler.writeMessage(exchange, message, out, name); 126 done(exchange); 127 } 128 finally { 129 if (out != null) { 130 try { 131 out.close(); 132 } 133 catch (IOException e) { 134 log.error("Caught exception while closing stream on error: " + e, e); 135 } 136 } 137 } 138 } 139 } 140 141 | Popular Tags |