Author: tchemit Date: 2008-02-13 16:37:36 +0000 (Wed, 13 Feb 2008) New Revision: 929 Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java Log: exemple de mise en place configuration dynamique (pouvoir specifier explicitement le parametrage) utilisation de File.separator fermeture de stream redundant initializer TODO en faire de meme sur les autres objets requierant une configuration (LuceneDatabase,Services,...) Modified: trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java =================================================================== --- trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-13 16:25:50 UTC (rev 928) +++ trunk/simexplorer-is/simexplorer-is-storage/src/java/fr/cemagref/simexplorer/is/storage/attachment/FileSystemAttachmentHandler.java 2008-02-13 16:37:36 UTC (rev 929) @@ -25,6 +25,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; import org.codelutin.util.MD5; @@ -41,10 +42,28 @@ public class FileSystemAttachmentHandler extends AttachmentHandler { /** Base folder. */ - private static String baseFolder = Config.getProperties().getProperty( - "simexplorer.data"); + protected String baseFolder; /** + * Constructeur utilisant la configuration par defaut. + * <p/> + * TODO on devrait supprimer ce constructeur, car on doit maitriser la + * configuration. + */ + public FileSystemAttachmentHandler() { + baseFolder = Config.getProperties().getProperty("simexplorer.data"); + } + + /** + * Constructeur utilisant une configuration spécifique + * + * @param baseFolder le répertoire de base + */ + public FileSystemAttachmentHandler(String baseFolder) { + this.baseFolder = baseFolder; + } + + /** * Retrieve file associated to content Create directories associated to * file. * @@ -59,14 +78,15 @@ String resultPath = baseFolder; // Create file path + String separator = File.separator; if (entity.getUuid().length() > 3) { - resultPath = resultPath + entity.getUuid().substring(0, 1) + "/" - + entity.getUuid().substring(1, 2) + "/" - + entity.getUuid().substring(2, 3) + "/" + entity.getUuid(); + resultPath = resultPath + entity.getUuid().substring(0, 1) + separator + + entity.getUuid().substring(1, 2) + separator + + entity.getUuid().substring(2, 3) + separator + entity.getUuid(); } else { resultPath = resultPath + entity.getUuid(); } - resultPath = resultPath + "/" + entity.getVersion().toString() + "/" + resultPath = resultPath + separator + entity.getVersion().toString() + separator + attachment.getUniqueId(); File result = new File(resultPath); @@ -86,7 +106,7 @@ public InputStream retrieveData(MetaData entity, Attachment attachment) throws SimExplorerStorageException { // Simple stream on file - FileInputStream fis = null; + FileInputStream fis; try { fis = new FileInputStream(getFile(entity, attachment)); } catch (FileNotFoundException e) { @@ -101,7 +121,7 @@ @Override public String retrieveMD5Data(MetaData entity, Attachment attachment) throws SimExplorerStorageException { - String md5 = null; + String md5; try { md5 = MD5.asHex(MD5.getHash(getFile(entity, attachment))); } catch (Exception e) { @@ -117,14 +137,16 @@ public void storeData(MetaData entity, Attachment attachment, InputStream is) throws SimExplorerStorageException { + BufferedOutputStream bout = null; // Simple stream on file try { + File file = getFile(entity, attachment); FileOutputStream fos = new FileOutputStream(file); // Buffer copy stream to stream BufferedInputStream bin = new BufferedInputStream(is); - BufferedOutputStream bout = new BufferedOutputStream(fos); + bout = new BufferedOutputStream(fos); while (true) { int datum = bin.read(); @@ -132,17 +154,29 @@ break; bout.write(datum); } + bout.flush(); - // Close file - fos.close(); } catch (FileNotFoundException e) { throw new SimExplorerStorageException(e); } catch (IOException e) { throw new SimExplorerStorageException(e); + } finally { + closeStream(bout); } } + protected void closeStream(OutputStream fos) throws SimExplorerStorageException { + if (fos != null) { + // Close file + try { + fos.close(); + } catch (IOException e) { + throw new SimExplorerStorageException(e); + } + } + } + /* (non-Javadoc) * @see fr.cemagref.simexplorer.is.storage.attachment.AttachmentHandler#deleteData(fr.cemagref.simexplorer.is.entities.metadata.MetaData, java.lang.String) */