1 17 package org.apache.servicemix.components.file; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.components.util.DefaultFileMarshaler; 22 import org.apache.servicemix.components.util.FileMarshaler; 23 import org.apache.servicemix.components.util.OutBinding; 24 25 import javax.jbi.JBIException; 26 import javax.jbi.messaging.MessageExchange; 27 import javax.jbi.messaging.NormalizedMessage; 28 import java.io.BufferedOutputStream ; 29 import java.io.File ; 30 import java.io.FileOutputStream ; 31 import java.io.IOException ; 32 import java.io.OutputStream ; 33 34 41 public class FileWriter extends OutBinding { 42 private static final Log log = LogFactory.getLog(FileWriter.class); 43 44 private File directory; 45 private FileMarshaler marshaler = new DefaultFileMarshaler(); 46 private String tempFilePrefix = "servicemix-"; 47 private String tempFileSuffix = ".xml"; 48 private boolean autoCreateDirectory = true; 49 50 public File getDirectory() { 53 return directory; 54 } 55 56 public void setDirectory(File directory) { 57 this.directory = directory; 58 } 59 60 public FileMarshaler getMarshaler() { 61 return marshaler; 62 } 63 64 public void setMarshaler(FileMarshaler marshaler) { 65 this.marshaler = marshaler; 66 } 67 68 public String getTempFilePrefix() { 69 return tempFilePrefix; 70 } 71 72 public void setTempFilePrefix(String tempFilePrefix) { 73 this.tempFilePrefix = tempFilePrefix; 74 } 75 76 public String getTempFileSuffix() { 77 return tempFileSuffix; 78 } 79 80 public void setTempFileSuffix(String tempFileSuffix) { 81 this.tempFileSuffix = tempFileSuffix; 82 } 83 84 public boolean isAutoCreateDirectory() { 85 return autoCreateDirectory; 86 } 87 88 public void setAutoCreateDirectory(boolean autoCreateDirectory) { 89 this.autoCreateDirectory = autoCreateDirectory; 90 } 91 92 protected void init() throws JBIException { 95 if (directory == null) { 96 throw new IllegalArgumentException ("You must specify the directory property"); 97 } 98 if (isAutoCreateDirectory()) { 99 directory.mkdirs(); 100 } 101 if (!directory.isDirectory()) { 102 throw new IllegalArgumentException ("The directory property must be a directory but was: " + directory); 103 } 104 105 super.init(); 106 } 107 108 protected void process(MessageExchange exchange, NormalizedMessage message) throws Exception { 109 OutputStream out = null; 110 try { 111 String name = marshaler.getOutputName(exchange, message); 112 File newFile = null; 113 if (name == null) { 114 newFile = File.createTempFile(tempFilePrefix, tempFileSuffix, directory); 115 } 116 else { 117 newFile = new File (directory, name); 118 } 119 out = new BufferedOutputStream (new FileOutputStream (newFile)); 120 marshaler.writeMessage(exchange, message, out, name); 121 done(exchange); 122 } 123 finally { 124 if (out != null) { 125 try { 126 out.close(); 127 } 128 catch (IOException e) { 129 log.error("Caught exception while closing stream on error: " + e, e); 130 } 131 } 132 } 133 } 134 } 135 136 | Popular Tags |