KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > file > def > FileDefinition


1 package org.jbpm.file.def;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.File JavaDoc;
6 import java.io.FileInputStream JavaDoc;
7 import java.io.FileNotFoundException JavaDoc;
8 import java.io.FileOutputStream JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.io.InputStream JavaDoc;
11 import java.io.OutputStream JavaDoc;
12 import java.util.ArrayList JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 JavaDoc rootDir = JbpmConfiguration.getString("jbpm.files.dir");
31
32   private String JavaDoc dir = null;
33
34   private Map JavaDoc processFiles = null;
35
36   public FileDefinition() {
37   }
38
39   public ModuleInstance createInstance() {
40     return null;
41   }
42
43   // storing files
44
// ///////////////////////////////////////////////////////////////////////////
45

46   /**
47    * add a file to this definition.
48    */

49   public void addFile(String JavaDoc name, byte[] bytes) {
50     ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(bytes);
51     addFile(name, bais);
52   }
53
54   /**
55    * add a file to this definition.
56    */

57   public void addFile(String JavaDoc name, InputStream JavaDoc is) {
58     try {
59       if (isStoredOnFileSystem()) {
60         storeFileOnFileSystem(name, is);
61
62       } else { // its stored in the database
63
storeFileInDb(name, is);
64       }
65     } catch (Exception JavaDoc e) {
66       throw new RuntimeException JavaDoc("file '" + name + "' could not be stored", e);
67     }
68   }
69
70   private void storeFileOnFileSystem(String JavaDoc name, InputStream JavaDoc is) throws FileNotFoundException JavaDoc, IOException JavaDoc {
71     String JavaDoc fileName = getFilePath(name);
72     log.trace("storing file '" + name + "' on file system to '" + fileName + "'");
73     FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(fileName);
74     transfer(is, fos);
75     fos.close();
76   }
77
78   private void storeFileInDb(String JavaDoc name, InputStream JavaDoc is) throws IOException JavaDoc {
79     if (processFiles == null) {
80       processFiles = new HashMap JavaDoc();
81     }
82     ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
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   // retrieving files
89
// ///////////////////////////////////////////////////////////////////////////
90

91   /**
92    * retrieve a file of this definition as an inputstream.
93    */

94   public InputStream JavaDoc getInputStream(String JavaDoc name) {
95     InputStream JavaDoc inputStream = null;
96     try {
97       if (isStoredOnFileSystem()) {
98         inputStream = getInputStreamFromFileSystem(name);
99       } else { // its stored in the database
100
inputStream = getInputStreamFromDb(name);
101       }
102     } catch (Exception JavaDoc e) {
103       throw new RuntimeException JavaDoc("couldn't get inputstream for file '" + name + "'", e);
104     }
105     return inputStream;
106   }
107
108   public Map JavaDoc getInputStreamMap() {
109     HashMap JavaDoc result = new HashMap JavaDoc();
110     if (processFiles != null) {
111       Iterator JavaDoc iterator = processFiles.keySet().iterator();
112       while (iterator.hasNext()) {
113         String JavaDoc name = (String JavaDoc) iterator.next();
114         result.put(name, getInputStream(name));
115       }
116     }
117     return result;
118   }
119
120   public Map JavaDoc getBytesMap() {
121     HashMap JavaDoc result = new HashMap JavaDoc();
122     if (processFiles != null) {
123       Iterator JavaDoc iterator = processFiles.keySet().iterator();
124       while (iterator.hasNext()) {
125         String JavaDoc name = (String JavaDoc) iterator.next();
126         result.put(name, getBytes(name));
127       }
128     }
129     return result;
130   }
131
132   private InputStream JavaDoc getInputStreamFromFileSystem(String JavaDoc name) throws FileNotFoundException JavaDoc {
133     InputStream JavaDoc inputStream = null;
134     String JavaDoc fileName = getFilePath(name);
135     log.trace("loading file '" + name + "' from file system '" + fileName + "'");
136     inputStream = new FileInputStream JavaDoc(fileName);
137     return inputStream;
138   }
139
140   private InputStream JavaDoc getInputStreamFromDb(String JavaDoc name) {
141     InputStream JavaDoc inputStream = null;
142     log.trace("loading file '" + name + "' from database");
143     ByteArray byteArray = getByteArray(name);
144     inputStream = new ByteArrayInputStream JavaDoc(byteArray.getBytes());
145     return inputStream;
146   }
147
148   /**
149    * retrieve a file of this definition as a byte array.
150    */

151   public byte[] getBytes(String JavaDoc name) {
152     byte[] bytes = null;
153     try {
154       if (isStoredOnFileSystem()) {
155         bytes = getBytesFromFileSystem(name);
156       } else { // its stored in the database
157
bytes = getBytesFromDb(name);
158       }
159     } catch (Exception JavaDoc e) {
160       throw new RuntimeException JavaDoc("couldn't get value for file '" + name + "'", e);
161     }
162     return bytes;
163   }
164
165   private byte[] getBytesFromFileSystem(String JavaDoc name) throws IOException JavaDoc {
166     byte[] bytes = null;
167     InputStream JavaDoc in = getInputStreamFromFileSystem(name);
168     ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
169     transfer(in, out);
170     bytes = out.toByteArray();
171     return bytes;
172   }
173
174   private byte[] getBytesFromDb(String JavaDoc name) {
175     byte[] bytes;
176     ByteArray byteArray = getByteArray(name);
177     bytes = byteArray.getBytes();
178     return bytes;
179   }
180
181   private ByteArray getByteArray(String JavaDoc name) {
182     ByteArray byteArray = (ByteArray) (processFiles != null ? processFiles.get(name) : null);
183     if (byteArray == null) {
184       throw new RuntimeException JavaDoc("file '" + name + "' not found in db");
185     }
186     return byteArray;
187   }
188
189   private boolean isStoredOnFileSystem() {
190     boolean isStoredOnFileSystem = (rootDir != null);
191     // if files should be stored on the file system and no directory has been
192
// created yet...
193
if ((isStoredOnFileSystem) && (dir == null)) {
194       // create a new directory
195
dir = findNewDirName();
196       new File JavaDoc(rootDir + "/" + dir).mkdirs();
197     }
198     return isStoredOnFileSystem;
199   }
200
201   private String JavaDoc findNewDirName() {
202     String JavaDoc newDirName = "files-1";
203
204     File JavaDoc parentFile = new File JavaDoc(rootDir);
205     if (parentFile.exists()) {
206       // get the current contents of the directory
207
String JavaDoc[] children = parentFile.list();
208       List JavaDoc fileNames = new ArrayList JavaDoc();
209       if (children != null) {
210         fileNames = new ArrayList JavaDoc(Arrays.asList(children));
211       }
212
213       // find an unused name for the directory to be created
214
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 JavaDoc getFilePath(String JavaDoc name) {
225     String JavaDoc filePath = rootDir + "/" + dir + "/" + name;
226     new File JavaDoc(filePath).getParentFile().mkdirs();
227     return filePath;
228   }
229   
230   private static final int BUFFER_SIZE = 512;
231   public static int transfer(InputStream JavaDoc in, OutputStream JavaDoc out) throws IOException JavaDoc {
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