]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java
- Custom namespace
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / attachment / FileAttachmentsStorage.java
1 package org.argeo.slc.core.attachment;
2
3 import java.io.File;
4 import java.io.FileInputStream;
5 import java.io.FileOutputStream;
6 import java.io.FileWriter;
7 import java.io.IOException;
8 import java.io.InputStream;
9 import java.io.OutputStream;
10 import java.text.DateFormat;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13
14 import org.apache.commons.io.IOUtils;
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.argeo.slc.SlcException;
18 import org.springframework.beans.factory.InitializingBean;
19
20 public class FileAttachmentsStorage implements AttachmentsStorage,
21 InitializingBean {
22 private final static Log log = LogFactory
23 .getLog(FileAttachmentsStorage.class);
24
25 private File attachmentsDirectory;
26
27 private String attachmentsTocFileName = "attachmentsToc.csv";
28
29 private DateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");
30 private DateFormat dateFormatTime = new SimpleDateFormat("HH:mm:ss");
31
32 public void afterPropertiesSet() {
33 if (attachmentsDirectory == null) {
34
35 String osgiInstanceArea = System.getProperty("osgi.instance.area");
36 if (osgiInstanceArea != null) {
37 if (osgiInstanceArea.startsWith("file:"))
38 osgiInstanceArea = osgiInstanceArea.substring("file:"
39 .length());
40 attachmentsDirectory = new File(osgiInstanceArea
41 + File.separator + "slcAttachments");
42 }
43
44 if (attachmentsDirectory == null) {
45 String tempDir = System.getProperty("java.io.tmpdir");
46 attachmentsDirectory = new File(tempDir + File.separator
47 + "slcAttachments");
48 }
49 }
50 if (!attachmentsDirectory.exists())
51 attachmentsDirectory.mkdirs();
52 if (log.isDebugEnabled())
53 log.debug("File attachment storage initialized in directory "
54 + attachmentsDirectory);
55 }
56
57 public void retrieveAttachment(Attachment attachment,
58 OutputStream outputStream) {
59 File file = getFile(attachment);
60 InputStream in = null;
61 try {
62 byte[] buffer = new byte[1024 * 1024];
63 in = new FileInputStream(file);
64 int read = -1;
65 while ((read = in.read(buffer)) >= 0) {
66 outputStream.write(buffer, 0, read);
67 }
68 if (log.isTraceEnabled())
69 log.trace("Read " + attachment + " from " + file);
70 } catch (IOException e) {
71 throw new SlcException("Cannot write attachment " + attachment
72 + " to " + file, e);
73 } finally {
74 IOUtils.closeQuietly(in);
75 }
76 }
77
78 public void storeAttachment(Attachment attachment, InputStream inputStream) {
79 File file = getFile(attachment);
80 FileOutputStream out = null;
81 try {
82 byte[] buffer = new byte[1024 * 1024];
83 out = new FileOutputStream(file);
84 int read = -1;
85 while ((read = inputStream.read(buffer)) >= 0) {
86 out.write(buffer, 0, read);
87 }
88 if (log.isTraceEnabled())
89 log.trace("Wrote " + attachment + " to " + file);
90 updateAttachmentToc(attachment, file);
91 } catch (IOException e) {
92 throw new SlcException("Cannot write attachment " + attachment
93 + " to " + file, e);
94 } finally {
95 IOUtils.closeQuietly(out);
96 }
97
98 }
99
100 /** For monitoring purposes only */
101 protected void updateAttachmentToc(Attachment attachment, File file) {
102 Date date = new Date(file.lastModified());
103 FileWriter writer = null;
104 try {
105 writer = new FileWriter(attachmentsDirectory + File.separator
106 + attachmentsTocFileName, true);
107 writer.append(dateFormatDay.format(date));
108 writer.append(',');
109 writer.append(dateFormatTime.format(date));
110 writer.append(',');
111 writer.append(attachment.getUuid());
112 writer.append(',');
113 writer.append(attachment.getName());
114 writer.append(',');
115 writer.append(attachment.getContentType());
116 writer.append(',');
117 writer.append(Long.toString(file.length()));
118 writer.append(',');
119 writer.append(file.getCanonicalPath());
120 writer.append('\n');
121 } catch (IOException e) {
122 log.warn("Could not update attachments TOC for " + attachment
123 + " and file " + file, e);
124 } finally {
125 IOUtils.closeQuietly(writer);
126 }
127
128 }
129
130 protected File getFile(Attachment attachment) {
131 File file = new File(attachmentsDirectory + File.separator
132 + attachment.getUuid());
133 return file;
134 }
135
136 public void setAttachmentsDirectory(File attachmentsDirectory) {
137 this.attachmentsDirectory = attachmentsDirectory;
138 }
139
140 public void setAttachmentsTocFileName(String attachmentsTocFileName) {
141 this.attachmentsTocFileName = attachmentsTocFileName;
142 }
143
144 public void setDateFormatDay(DateFormat dateFormatDay) {
145 this.dateFormatDay = dateFormatDay;
146 }
147
148 public void setDateFormatTime(DateFormat dateFormatTime) {
149 this.dateFormatTime = dateFormatTime;
150 }
151
152 }