X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=runtime%2Forg.argeo.slc.core%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Fattachment%2FFileAttachmentsStorage.java;h=51080c63fa35aaed79850f09158e60c044e2ebc0;hb=b21a43af1dbdae169d09147f8ee646537dfd1a4c;hp=0e70805c061c602f10e678d9063ef176f503d060;hpb=ee6c3543a0ff9403420ce6a9c647723269f14331;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java index 0e70805c0..51080c63f 100644 --- a/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java +++ b/runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/attachment/FileAttachmentsStorage.java @@ -3,30 +3,55 @@ package org.argeo.slc.core.attachment; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import org.apache.commons.io.IOUtils; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; +import org.springframework.beans.factory.InitializingBean; + +public class FileAttachmentsStorage implements AttachmentsStorage, + InitializingBean { + private final static Log log = LogFactory + .getLog(FileAttachmentsStorage.class); -public class FileAttachmentsStorage implements AttachmentsStorage { private File attachmentsDirectory; - public FileAttachmentsStorage() { - String osgiInstanceArea = System.getProperty("osgi.instance.area"); - if (osgiInstanceArea != null) { - if (osgiInstanceArea.startsWith("file:")) - osgiInstanceArea = osgiInstanceArea.substring("file:".length()); - attachmentsDirectory = new File(osgiInstanceArea + File.separator - + "slcAttachments"); - } + private String attachmentsTocFileName = "attachmentsToc.csv"; + private DateFormat dateFormatDay = new SimpleDateFormat("yyyy-MM-dd"); + private DateFormat dateFormatTime = new SimpleDateFormat("HH:mm:ss"); + + public void afterPropertiesSet() { if (attachmentsDirectory == null) { - String tempDir = System.getProperty("java.io.tmpdir"); - attachmentsDirectory = new File(tempDir + File.separator - + "slcAttachments"); + + String osgiInstanceArea = System.getProperty("osgi.instance.area"); + if (osgiInstanceArea != null) { + if (osgiInstanceArea.startsWith("file:")) + osgiInstanceArea = osgiInstanceArea.substring("file:" + .length()); + attachmentsDirectory = new File(osgiInstanceArea + + File.separator + "slcAttachments"); + } + + if (attachmentsDirectory == null) { + String tempDir = System.getProperty("java.io.tmpdir"); + attachmentsDirectory = new File(tempDir + File.separator + + "slcAttachments"); + } } + if (!attachmentsDirectory.exists()) + attachmentsDirectory.mkdirs(); + if (log.isDebugEnabled()) + log.debug("File attachment storage initialized in directory " + + attachmentsDirectory); } public void retrieveAttachment(Attachment attachment, @@ -40,6 +65,8 @@ public class FileAttachmentsStorage implements AttachmentsStorage { while ((read = in.read(buffer)) >= 0) { outputStream.write(buffer, 0, read); } + if (log.isTraceEnabled()) + log.trace("Read " + attachment + " from " + file); } catch (IOException e) { throw new SlcException("Cannot write attachment " + attachment + " to " + file, e); @@ -58,6 +85,9 @@ public class FileAttachmentsStorage implements AttachmentsStorage { while ((read = inputStream.read(buffer)) >= 0) { out.write(buffer, 0, read); } + if (log.isTraceEnabled()) + log.trace("Wrote " + attachment + " to " + file); + updateAttachmentToc(attachment, file); } catch (IOException e) { throw new SlcException("Cannot write attachment " + attachment + " to " + file, e); @@ -67,11 +97,56 @@ public class FileAttachmentsStorage implements AttachmentsStorage { } + /** For monitoring purposes only */ + protected void updateAttachmentToc(Attachment attachment, File file) { + Date date = new Date(file.lastModified()); + FileWriter writer = null; + try { + writer = new FileWriter(attachmentsDirectory + File.separator + + attachmentsTocFileName, true); + writer.append(dateFormatDay.format(date)); + writer.append(','); + writer.append(dateFormatTime.format(date)); + writer.append(','); + writer.append(attachment.getUuid()); + writer.append(','); + writer.append(attachment.getName()); + writer.append(','); + writer.append(attachment.getContentType()); + writer.append(','); + writer.append(Long.toString(file.length())); + writer.append(','); + writer.append(file.getCanonicalPath()); + writer.append('\n'); + } catch (IOException e) { + log.warn("Could not update attachments TOC for " + attachment + + " and file " + file, e); + } finally { + IOUtils.closeQuietly(writer); + } + + } + protected File getFile(Attachment attachment) { - if (!attachmentsDirectory.exists()) - attachmentsDirectory.mkdirs(); File file = new File(attachmentsDirectory + File.separator + attachment.getUuid()); return file; } + + public void setAttachmentsDirectory(File attachmentsDirectory) { + this.attachmentsDirectory = attachmentsDirectory; + } + + public void setAttachmentsTocFileName(String attachmentsTocFileName) { + this.attachmentsTocFileName = attachmentsTocFileName; + } + + public void setDateFormatDay(DateFormat dateFormatDay) { + this.dateFormatDay = dateFormatDay; + } + + public void setDateFormatTime(DateFormat dateFormatTime) { + this.dateFormatTime = dateFormatTime; + } + }