Index: lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandler.java diff -u /dev/null lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandler.java:1.1 --- /dev/null Wed Apr 30 12:31:14 2008 +++ lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandler.java Wed Apr 30 12:31:08 2008 @@ -0,0 +1,194 @@ +package org.codelutin.generator.demo.files; + +import static org.codelutin.generator.demo.LutinGenDemoProperties.getProperties; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.nio.channels.FileChannel; +import java.util.ArrayList; +import java.util.List; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.codelutin.util.FileUtil; + +/** + * Cette classe est responsable de gérer des fichiers. Les fichiers sont + * regroupés dans des isaltors. Le but étant d'isoler les fichiers des + * utilisateurs en fonction de leurs sessions HTTP + * + * @author thimel + */ +public class FileHandler { + + public static final String DEFAULT_LOCATION; + static { + if (System.getProperty("user.home") != null) { + DEFAULT_LOCATION = System.getProperty("user.home") + File.pathSeparator + "lutingen-demo-files"; + } else { + DEFAULT_LOCATION = "/var/local/lutingen-demo-files"; + } + } + public static final String FS_FILE_LOCATION = getProperties().getProperty( + "FS_FILE_LOCATION", DEFAULT_LOCATION); + + public static final String FS_FILENAME = "file"; + + public static final String FS_PREFIX_MODEL = "model-"; + + public static final String FS_PREFIX_GENERATOR = "generator-"; + + private static final Log log = LogFactory.getLog(FileHandler.class); + + private static FileHandler instance; + + public static FileHandler getInstance() { + if (instance == null) { + instance = new FileHandler(); + } + return instance; + } + + private File getFileIsolatorLocation(String isolatorKey) { + File filesFolder = new File(FS_FILE_LOCATION); + if (!filesFolder.exists()) { + filesFolder.mkdir(); + } + File isolationFolder = new File(filesFolder, isolatorKey); + if (!isolationFolder.exists()) { + isolationFolder.mkdir(); + } + return isolationFolder; + } + + private File findFile(File isolatorFolder, String prefix, String name) { + File modelFoder = new File(isolatorFolder, name); + if (!modelFoder.exists()) { + modelFoder.mkdir(); + } + return new File(modelFoder, prefix + FS_FILENAME); + + } + + private File findModel(File isolatorFolder, String name) { + return findFile(isolatorFolder, FS_PREFIX_MODEL, name); + } + + private File findGenerator(File isolatorFolder, String name) { + return findFile(isolatorFolder, FS_PREFIX_GENERATOR, name); + } + + public void addModel(String isolatorKey, String name, File o) + throws FileHandlerException { + addFile(isolatorKey, FS_PREFIX_MODEL, name, o); + } + + public void addGenerator(String isolatorKey, String name, File o) + throws FileHandlerException { + addFile(isolatorKey, FS_PREFIX_GENERATOR, name, o); + } + + private void addFile(String isolatorKey, String prefix, String name, File o) + throws FileHandlerException { + File isolatorFolder = getFileIsolatorLocation(isolatorKey); + File file = findFile(isolatorFolder, prefix, name); + if (file.exists()) { + throw new FileHandlerException("File \"" + prefix + name + + "\" already exists"); + } + boolean result = deplacer(o, file); + if (log.isDebugEnabled()) { + log.debug("Résultat du déplacement: " + result); + } + } + + public boolean deplacer(File source, File destination) { + if (!destination.exists()) { + // On essaye avec renameTo + boolean result = source.renameTo(destination); + if (!result) { + // On essaye de copier + result = true; + result &= copier(source, destination); + if (result) + result &= source.delete(); + } + return (result); + } else { + // Si le fichier destination existe, on annule ... + return (false); + } + } + + public boolean copier(File source, File destination) { + boolean resultat = false; + FileChannel in = null; // canal d'entrée + FileChannel out = null; // canal de sortie + + try { + // Init + in = new FileInputStream(source).getChannel(); + out = new FileOutputStream(destination).getChannel(); + + // Copie depuis le in vers le out + in.transferTo(0, in.size(), out); + resultat = true; + } catch (Exception e) { + e.printStackTrace(); // n'importe quelle exception + } finally { // finalement on ferme + if (in != null) { + try { + in.close(); + } catch (IOException e) { + } + } + if (out != null) { + try { + out.close(); + } catch (IOException e) { + } + } + } + return (resultat); + } + + public File getModel(String isolatorKey, String name) { + File isolatorFolder = getFileIsolatorLocation(isolatorKey); + return findModel(isolatorFolder, name); + } + + public File getGenerator(String isolatorKey, String name) { + File isolatorFolder = getFileIsolatorLocation(isolatorKey); + return findGenerator(isolatorFolder, name); + } + + public void unvalidateIsolator(String isolatorKey) { + if (log.isInfoEnabled()) { + log.info("Unvalidating isolator: " + isolatorKey); + } + File loc = getFileIsolatorLocation(isolatorKey); + FileUtil.deleteRecursively(loc); + } + + public List getModelNames(String isolatorKey) { + return getFileNames(isolatorKey, FS_PREFIX_MODEL); + } + + public List getGeneratorNames(String isolatorKey) { + return getFileNames(isolatorKey, FS_PREFIX_GENERATOR); + } + + private List getFileNames(String isolatorKey, String prefix) { + File isolatorFolder = getFileIsolatorLocation(isolatorKey); + List fileNames = new ArrayList(); + for (String fileName : isolatorFolder.list()) { + if (findFile(isolatorFolder, prefix, fileName).exists()) { + fileNames.add(fileName); + } + } + return fileNames; + } + +} // FileHandler Index: lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandlerException.java diff -u /dev/null lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandlerException.java:1.1 --- /dev/null Wed Apr 30 12:31:15 2008 +++ lutingenerator-demo/src/main/java/org/codelutin/generator/demo/files/FileHandlerException.java Wed Apr 30 12:31:08 2008 @@ -0,0 +1,9 @@ +package org.codelutin.generator.demo.files; + +public class FileHandlerException extends Exception { + + public FileHandlerException(String message) { + super(message); + } + +} //FileHandlerException