]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.spring/src/org/argeo/slc/core/attachment/FileAttachmentsStorage.java
Re-activate Spring runtime unit tests.
[gpl/argeo-slc.git] / org.argeo.slc.spring / src / org / argeo / slc / core / attachment / FileAttachmentsStorage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.core.attachment;
17
18 import java.io.File;
19 import java.io.FileInputStream;
20 import java.io.FileOutputStream;
21 import java.io.FileWriter;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
25 import java.text.DateFormat;
26 import java.text.SimpleDateFormat;
27 import java.util.Date;
28
29 import org.apache.commons.io.IOUtils;
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32 import org.argeo.slc.SlcException;
33 import org.argeo.slc.attachment.Attachment;
34 import org.argeo.slc.attachment.AttachmentsStorage;
35 import org.springframework.beans.factory.InitializingBean;
36 import org.springframework.core.io.Resource;
37
38 public class FileAttachmentsStorage implements AttachmentsStorage,
39 AttachmentUploader, InitializingBean {
40 private final static Log log = LogFactory
41 .getLog(FileAttachmentsStorage.class);
42
43 private File attachmentsDirectory;
44
45 private String attachmentsTocFileName = "attachmentsToc.csv";
46
47 private DateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd");
48 private DateFormat dateFormatTime = new SimpleDateFormat("HH:mm:ss");
49
50 public void afterPropertiesSet() {
51 if (attachmentsDirectory == null) {
52
53 String osgiInstanceArea = System.getProperty("osgi.instance.area");
54 if (osgiInstanceArea != null) {
55 if (osgiInstanceArea.startsWith("file:"))
56 osgiInstanceArea = osgiInstanceArea.substring("file:"
57 .length());
58 attachmentsDirectory = new File(osgiInstanceArea
59 + File.separator + "slcAttachments");
60 }
61
62 if (attachmentsDirectory == null) {
63 String tempDir = System.getProperty("java.io.tmpdir");
64 attachmentsDirectory = new File(tempDir + File.separator
65 + "slcAttachments");
66 }
67 }
68 if (!attachmentsDirectory.exists())
69 attachmentsDirectory.mkdirs();
70 if (log.isDebugEnabled())
71 log.debug("File attachment storage initialized in directory "
72 + attachmentsDirectory);
73 }
74
75 public void retrieveAttachment(Attachment attachment,
76 OutputStream outputStream) {
77 File file = getFile(attachment);
78 InputStream in = null;
79 try {
80 byte[] buffer = new byte[1024 * 1024];
81 in = new FileInputStream(file);
82 int read = -1;
83 while ((read = in.read(buffer)) >= 0) {
84 outputStream.write(buffer, 0, read);
85 }
86 if (log.isTraceEnabled())
87 log.trace("Read " + attachment + " from " + file);
88 } catch (IOException e) {
89 throw new SlcException("Cannot write attachment " + attachment
90 + " to " + file, e);
91 } finally {
92 IOUtils.closeQuietly(in);
93 }
94 }
95
96 public void storeAttachment(Attachment attachment, InputStream inputStream) {
97 File file = getFile(attachment);
98 FileOutputStream out = null;
99 try {
100 byte[] buffer = new byte[1024 * 1024];
101 out = new FileOutputStream(file);
102 int read = -1;
103 while ((read = inputStream.read(buffer)) >= 0) {
104 out.write(buffer, 0, read);
105 }
106 if (log.isTraceEnabled())
107 log.trace("Wrote " + attachment + " to " + file);
108 updateAttachmentToc(attachment, file);
109 } catch (IOException e) {
110 throw new SlcException("Cannot write attachment " + attachment
111 + " to " + file, e);
112 } finally {
113 IOUtils.closeQuietly(out);
114 }
115
116 }
117
118 public void upload(Attachment attachment, Resource resource) {
119 try {
120 storeAttachment(attachment, resource.getInputStream());
121 } catch (IOException e) {
122 throw new SlcException("Cannot upload attachment " + attachment, e);
123 }
124 }
125
126 /** For monitoring purposes only */
127 protected void updateAttachmentToc(Attachment attachment, File file) {
128 Date date = new Date(file.lastModified());
129 FileWriter writer = null;
130 try {
131 writer = new FileWriter(attachmentsDirectory + File.separator
132 + attachmentsTocFileName, true);
133 writer.append(dateFormatDay.format(date));
134 writer.append(',');
135 writer.append(dateFormatTime.format(date));
136 writer.append(',');
137 writer.append(attachment.getUuid());
138 writer.append(',');
139 writer.append(attachment.getName());
140 writer.append(',');
141 writer.append(attachment.getContentType());
142 writer.append(',');
143 writer.append(Long.toString(file.length()));
144 writer.append(',');
145 writer.append(file.getCanonicalPath());
146 writer.append('\n');
147 } catch (IOException e) {
148 log.warn("Could not update attachments TOC for " + attachment
149 + " and file " + file, e);
150 } finally {
151 IOUtils.closeQuietly(writer);
152 }
153
154 }
155
156 protected File getFile(Attachment attachment) {
157 File file = new File(attachmentsDirectory + File.separator
158 + attachment.getUuid());
159 return file;
160 }
161
162 public void setAttachmentsDirectory(File attachmentsDirectory) {
163 this.attachmentsDirectory = attachmentsDirectory;
164 }
165
166 public void setAttachmentsTocFileName(String attachmentsTocFileName) {
167 this.attachmentsTocFileName = attachmentsTocFileName;
168 }
169
170 public void setDateFormatDay(DateFormat dateFormatDay) {
171 this.dateFormatDay = dateFormatDay;
172 }
173
174 public void setDateFormatTime(DateFormat dateFormatTime) {
175 this.dateFormatTime = dateFormatTime;
176 }
177
178 }