1 package org.jbpm.file.def; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.io.InputStream ; 11 import java.io.OutputStream ; 12 import java.util.ArrayList ; 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.jbpm.JbpmConfiguration; 22 import org.jbpm.bytes.ByteArray; 23 import org.jbpm.module.def.ModuleDefinition; 24 import org.jbpm.module.exe.ModuleInstance; 25 26 public class FileDefinition extends ModuleDefinition { 27 28 private static final long serialVersionUID = 1L; 29 30 static String rootDir = JbpmConfiguration.getString("jbpm.files.dir"); 31 32 private String dir = null; 33 34 private Map processFiles = null; 35 36 public FileDefinition() { 37 } 38 39 public ModuleInstance createInstance() { 40 return null; 41 } 42 43 46 49 public void addFile(String name, byte[] bytes) { 50 ByteArrayInputStream bais = new ByteArrayInputStream (bytes); 51 addFile(name, bais); 52 } 53 54 57 public void addFile(String name, InputStream is) { 58 try { 59 if (isStoredOnFileSystem()) { 60 storeFileOnFileSystem(name, is); 61 62 } else { storeFileInDb(name, is); 64 } 65 } catch (Exception e) { 66 throw new RuntimeException ("file '" + name + "' could not be stored", e); 67 } 68 } 69 70 private void storeFileOnFileSystem(String name, InputStream is) throws FileNotFoundException , IOException { 71 String fileName = getFilePath(name); 72 log.trace("storing file '" + name + "' on file system to '" + fileName + "'"); 73 FileOutputStream fos = new FileOutputStream (fileName); 74 transfer(is, fos); 75 fos.close(); 76 } 77 78 private void storeFileInDb(String name, InputStream is) throws IOException { 79 if (processFiles == null) { 80 processFiles = new HashMap (); 81 } 82 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 83 log.trace("preparing file '" + name + "' for storage in the database"); 84 transfer(is, baos); 85 processFiles.put(name, new ByteArray(name, baos.toByteArray())); 86 } 87 88 91 94 public InputStream getInputStream(String name) { 95 InputStream inputStream = null; 96 try { 97 if (isStoredOnFileSystem()) { 98 inputStream = getInputStreamFromFileSystem(name); 99 } else { inputStream = getInputStreamFromDb(name); 101 } 102 } catch (Exception e) { 103 throw new RuntimeException ("couldn't get inputstream for file '" + name + "'", e); 104 } 105 return inputStream; 106 } 107 108 public Map getInputStreamMap() { 109 HashMap result = new HashMap (); 110 if (processFiles != null) { 111 Iterator iterator = processFiles.keySet().iterator(); 112 while (iterator.hasNext()) { 113 String name = (String ) iterator.next(); 114 result.put(name, getInputStream(name)); 115 } 116 } 117 return result; 118 } 119 120 public Map getBytesMap() { 121 HashMap result = new HashMap (); 122 if (processFiles != null) { 123 Iterator iterator = processFiles.keySet().iterator(); 124 while (iterator.hasNext()) { 125 String name = (String ) iterator.next(); 126 result.put(name, getBytes(name)); 127 } 128 } 129 return result; 130 } 131 132 private InputStream getInputStreamFromFileSystem(String name) throws FileNotFoundException { 133 InputStream inputStream = null; 134 String fileName = getFilePath(name); 135 log.trace("loading file '" + name + "' from file system '" + fileName + "'"); 136 inputStream = new FileInputStream (fileName); 137 return inputStream; 138 } 139 140 private InputStream getInputStreamFromDb(String name) { 141 InputStream inputStream = null; 142 log.trace("loading file '" + name + "' from database"); 143 ByteArray byteArray = getByteArray(name); 144 inputStream = new ByteArrayInputStream (byteArray.getBytes()); 145 return inputStream; 146 } 147 148 151 public byte[] getBytes(String name) { 152 byte[] bytes = null; 153 try { 154 if (isStoredOnFileSystem()) { 155 bytes = getBytesFromFileSystem(name); 156 } else { bytes = getBytesFromDb(name); 158 } 159 } catch (Exception e) { 160 throw new RuntimeException ("couldn't get value for file '" + name + "'", e); 161 } 162 return bytes; 163 } 164 165 private byte[] getBytesFromFileSystem(String name) throws IOException { 166 byte[] bytes = null; 167 InputStream in = getInputStreamFromFileSystem(name); 168 ByteArrayOutputStream out = new ByteArrayOutputStream (); 169 transfer(in, out); 170 bytes = out.toByteArray(); 171 return bytes; 172 } 173 174 private byte[] getBytesFromDb(String name) { 175 byte[] bytes; 176 ByteArray byteArray = getByteArray(name); 177 bytes = byteArray.getBytes(); 178 return bytes; 179 } 180 181 private ByteArray getByteArray(String name) { 182 ByteArray byteArray = (ByteArray) (processFiles != null ? processFiles.get(name) : null); 183 if (byteArray == null) { 184 throw new RuntimeException ("file '" + name + "' not found in db"); 185 } 186 return byteArray; 187 } 188 189 private boolean isStoredOnFileSystem() { 190 boolean isStoredOnFileSystem = (rootDir != null); 191 if ((isStoredOnFileSystem) && (dir == null)) { 194 dir = findNewDirName(); 196 new File (rootDir + "/" + dir).mkdirs(); 197 } 198 return isStoredOnFileSystem; 199 } 200 201 private String findNewDirName() { 202 String newDirName = "files-1"; 203 204 File parentFile = new File (rootDir); 205 if (parentFile.exists()) { 206 String [] children = parentFile.list(); 208 List fileNames = new ArrayList (); 209 if (children != null) { 210 fileNames = new ArrayList (Arrays.asList(children)); 211 } 212 213 int seqNr = 1; 215 while (fileNames.contains(newDirName)) { 216 seqNr++; 217 newDirName = "files-" + seqNr; 218 } 219 } 220 221 return newDirName; 222 } 223 224 private String getFilePath(String name) { 225 String filePath = rootDir + "/" + dir + "/" + name; 226 new File (filePath).getParentFile().mkdirs(); 227 return filePath; 228 } 229 230 private static final int BUFFER_SIZE = 512; 231 public static int transfer(InputStream in, OutputStream out) throws IOException { 232 int total = 0; 233 byte[] buffer = new byte[BUFFER_SIZE]; 234 int bytesRead = in.read( buffer ); 235 while ( bytesRead != -1 ) { 236 out.write( buffer, 0, bytesRead ); 237 total += bytesRead; 238 bytesRead = in.read( buffer ); 239 } 240 return total; 241 } 242 243 private static final Log log = LogFactory.getLog(FileDefinition.class); 244 245 } 246 | Popular Tags |