/** *

Cette classe gère les resources et services sur les systèmes de fichiers Ether.

*

Cette classe implémente le Singleton pattern exposé par * Gamma et al. dans leur livre désormais célèbre.

*

Elle permet d'instancier des singletons nommés et de gérer * tous les singletons d'une application.

* * @since JDK 1.1.x * @version $Id: FileManager.java,v 1.1 2001/01/15 15:38:47 pn Exp $ * @author Phillip Link */ package fr.alcatel.ether.app.common; //import fr.alcatel.ether.app.order.*; // JDK import java.util.List; import java.util.ArrayList; import java.util.Iterator; import java.util.Enumeration; import java.io.File; import java.io.IOException; // JDK - rmi import java.rmi.Naming; import java.rmi.RemoteException; import java.rmi.NotBoundException; // JDK - net import java.net.MalformedURLException; // application classes import fr.alcatel.ether.app.user.User; //import fr.alcatel.ether.app.user.Media; import fr.alcatel.ether.app.service.Service; //import fr.alcatel.ether.app.service.Services; import fr.alcatel.ether.app.data.EtherFileName; // utility classes import fr.alcatel.ether.tools.singleton.Singleton; import fr.alcatel.ether.app.common.Config; import fr.alcatel.ether.app.exception.EtherException ; //import fr.alcatel.ether.persistency.DatabaseException; import fr.alcatel.ether.tools.Trace; import flafi.util.FileCopy2; // Workflow //import fr.alcatel.ether.tools.workflow.exception.NotAssigned; //import fr.alcatel.ether.tools.workflow.RemoteWfServer; //import fr.alcatel.ether.tools.workflow.WfServer; // Ether workflow //import fr.alcatel.ether.app.order.workflow.FileResource; public class FileManager extends Singleton { // Debugging static private final boolean DEBUG = true; /** Messages d'erreur - MalformedURLException */ static public final String ERR_MALFORMED_URL = "Error invalid address for Remote File Manager"; static public final String ERR_NOT_BOUND = "Error Remote File Manager is not bound"; static public final String ERR_REMOTE = "Error retrieving Remote File Manager"; static public final String ERR_OTHER = "Error"; // Singleton instance static final private FileManager _fileMgr = new FileManager( ); /** Répertoire temporaire de la machine (/tmp ou /var/tmp )*/ /** Ce répertoire est utlisé pour y extraire les fichiers */ private String tmpDir = "/tmp" ; static final private String ETHER = "ETHER_TMP_DIRECTORY"; /** *

Construire le manager pour le traitement de services sur systèmes de fichiers.

* */ protected FileManager( ) { Singleton.register( getClass().toString(), this ); Config.getConfig(); Trace.getTrace("ether"); // initialisation du répertoire tmp try { File tempFile = File.createTempFile( "eth", null ); tmpDir = tempFile.getParent( ); tempFile.delete( ); } catch( IOException e ) { Trace.log(this," ERROR WHILE RETREIVING TMP DIR :" + e.toString()); } } /** *

Obtenir la machine hôte des données dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les données Ether */ public String getDataHost( ) { return Config.dataHost; } /** *

Obtenir la machine hôte contenant les répertoires utilisateur dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les répertoires utilisateur Ether */ public String getUsersHost( ) { return Config.usersHost; } /** *

Obtenir la machine hôte des services dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les services Ether */ public String getServicesHost( ) { return Config.servicesHost; } /** *

Obtenir la machine hôte des répertoires temporaires dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les répertoires temporaires Ether */ public String getTempHost( ) { return Config.tempHost; } /** *

Obtenir la machine hôte des répertoires pour fichies offline dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les répertoires offline Ether */ public String getOfflineHost( ) { return Config.offlineHost; } /** *

Obtenir la machine hôte des répertoires pour la création des média dans l'espace Ether.

* * * @return le nom de la machine hôte contenant les répertoires média Ether */ public String getMediaHost( ) { return Config.mediaHost; } /** *

Obtenir le répertoire des fichiers offline récupérés dans l'espace Ether.

* * * @return le chemin absolu du répertoire des données */ public String getOfflineDir( User user ) { StringBuffer temp = new StringBuffer( Config.offlineDir ); temp.append( File.separator ); temp.append( user.getLogin() ); return temp.toString( ); } /** *

Obtenir le répertoire des données dans l'espace Ether.

* * * @return le chemin absolu du répertoire des données */ public String getDataDir( ) { return Config.dataDir; } /** *

Obtenir le répertoire utilisateur dans l'espace Ether.

* * @param user: l'utilisateur * * @return le chemin absolu du répertoire utilisateur */ public String getUserDir( User user ) { StringBuffer temp = new StringBuffer( user.getRepository() ); //temp.append( File.separator ); //temp.append( user.getLogin() ); return temp.toString( ); } /** *

Obtenir le répertoire temporaire pour utilisateur dans l'espace Ether.

* * @param user: l'utilisateur * * @return le chemin absolu du répertoire temporaire pour un utilisateur donné */ public String getTempDir( User user ) { StringBuffer temp = new StringBuffer( Config.tempDir ); temp.append( File.separator ); temp.append( user.getLogin() ); return temp.toString( ); } /** *

Obtenir le répertoire pour déposer des fichiers à traiter par un service dans l'espace Ether.

* * @param user: l'utilisateur * * @return le chemin absolu du répertoire de services pour un utilisateur donné */ public String getServiceDir( User user ) { StringBuffer temp = new StringBuffer( Config.servicesDir ); temp.append( File.separator ); temp.append( user.getLogin() ); return temp.toString( ); } /** *

Obtenir le répertoire pour déposer l'image d'un média dans l'espace Ether.

* * @param user: l'utilisateur * * @return le chemin absolu du répertoire de services pour un utilisateur donné */ public String getMediaDir( User user ) { StringBuffer temp = new StringBuffer( Config.mediaDir ); temp.append( File.separator ); temp.append( user.getLogin() ); return temp.toString( ); } /** *

Obtenir le répertoire temporaire de désarchivage pour une commande.

* * @param orderId: l'identifiant de la commande * * @return le chemin absolu du répertoire temporaire de désarchivage pour une commande */ public String getArchiveOrderDir( int orderId ) { StringBuffer dir = new StringBuffer( getHostTmpDir() ); dir.append( File.separator ); dir.append( orderId ); return dir.toString( ); } /** *

Obtenir le répertoire temporaire d'une machine.

* * @return le chemin absolu du répertoire temporaire de la machine. */ public String getHostTmpDir() { StringBuffer dir = new StringBuffer(tmpDir); dir.append( File.separator ); dir.append(ETHER); return dir.toString() ; } /** *

Obtenir le répertoire d'entrée services pour un lancement interactif dans l'espace Ether.

* * @param User : utilisateur courant * @param processId : un identifiant unique pour le lancement interactif. * * @return le chemin absolu du répertoire services (où seront posés les données). */ public String getServiceInteractifInDir( User user, int processId ) { StringBuffer dir = new StringBuffer( getServiceInteractifDir(user,processId) ); dir.append( File.separator ); dir.append( Config.serviceInDir ); return dir.toString( ); } /** *

Obtenir le répertoire de sortie services pour un lancement interactif dans l'espace Ether.

* * @param User : utilisateur courant * @param processId : un identifiant unique pour le lancement interactif. * * @return le chemin absolu du répertoire services (où seront posés les données). */ public String getServiceInteractifOutDir( User user, int processId ) { StringBuffer dir = new StringBuffer( getServiceInteractifDir(user,processId) ); dir.append( File.separator ); dir.append( Config.serviceOutDir ); return dir.toString( ); } /** *

Obtenir le répertoire de services pour un lancement interactif dans l'espace Ether.

* * @param User : utilisateur courant * @param processId : un identifiant unique pour le lancement interactif. * * @return le chemin absolu du répertoire services (où seront posés les données). */ public String getServiceInteractifDir(User user, int processId) { StringBuffer dir = new StringBuffer( Config.servicesInteractifDir ); dir.append( File.separator ); dir.append( user.getLogin() ); dir.append( File.separator ); dir.append( processId ); return dir.toString( ); } /** *

Obtenir le répertoire résultat pour l'exécution d'un service intéractif * * @param user : l'utilisateur qui lance le service. * @param service : le service intéractif. * @param processId : un identifiant unique pour le lancement interactif. * * @return le chemin absolu du répertoire résultat. */ public String getServiceInteractifResultDir(User user,Service service,int processId) { StringBuffer dir = new StringBuffer(getUserDir(user)); dir.append(File.separator).append(service.getName()).append( "_" ).append(processId).append(File.separator).append(Config.serviceOutDir); return dir.toString(); } /** * Copier un fichier distant. * * @param src: le fichier source * @param dest: le fichier destination */ public boolean copy( RemoteFile src, RemoteFile dest ) { boolean success = false; try { RemoteFileManager destManager = (RemoteFileManager) dest.getFileManager( ); if ( DEBUG ) { Trace.debug( this, "Copying file '" + src + "' to '" + dest + "' : " + new Boolean(success) ); } success = destManager.copy( src, dest ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } return success; } /** * Supprimer un fichier distant. * * @param src: le fichier source */ public boolean delete( RemoteFile file ) { boolean success = false; try { RemoteFileManager fileManager = (RemoteFileManager) file.getFileManager( ); success = fileManager.delete( file ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } return success; } /** * Supprimer un répertoire distant. * * @param dir: le répertoire à supprimer. */ public boolean deleteDir( RemoteFile dir ) { boolean success = false; try { RemoteFileManager fileManager = (RemoteFileManager) dir.getFileManager( ); success = fileManager.deleteDir( dir); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } return success; } /** * Créer un répertoire distant. * * @param dir: le répertoire distant */ public boolean mkdir( RemoteFile file ) { boolean success = false; try { RemoteFileManager fileManager = (RemoteFileManager) file.getFileManager( ); success = fileManager.makeDir( file ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } return success; } /** * Lister les fichiers d'un répertoire distant. * * @param dir: le répertoire distant */ public ArrayList list( RemoteFile dir ) { if ( DEBUG ) { Trace.debug( this, " LISTING remote directory " + dir ); } ArrayList files = null; try { RemoteFileManager fileManager = dir.getFileManager( ); files = fileManager.list( dir ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } catch ( Exception exception ) { if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); } Trace.log( this, ERR_OTHER, exception ); } return files; } /** * Déterminer la taille d'un fichier o de tous les fichiers dans un répertoire. * Cette méthode regarde dans tous les sous-répertoires. * * @param dir: le fichier ou le répertoire distant */ public long size( RemoteFile dir ) { if ( DEBUG ) { Trace.debug( this, " SIZE of remote file or directory " + dir ); } long length = 0; try { RemoteFileManager fileManager = (RemoteFileManager) dir.getFileManager( ); length = fileManager.size( dir ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } catch ( Exception exception ) { if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); } Trace.log( this, ERR_OTHER, exception ); } return length; } /** * Déterminer la taille d'un fichier o de tous les fichiers dans un répertoire. * Cette méthode regarde dans tous les sous-répertoires. * * @param dir: le fichier ou le répertoire local */ public long size( File dir ) { long length = 0; try { if ( DEBUG ) { Trace.debug( this, "Checking size of " + dir.getAbsolutePath() ); } File[] list = dir.listFiles( ); // File is a directory if ( list != null ) { // Add length of directory length += dir.length( ); // Add length of all files in directory for (int i = 0 ; i < list.length; i++ ) { length += size( list[i] ); } } // File is a regular file else { length = dir.length( ); } } catch ( Exception exception ) { if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); } Trace.log( this, ERR_OTHER, exception ); } return length; } /** * Déterminer si un fichier existe. * * @param file: le fichier distant */ public boolean exists( RemoteFile file ) { if ( DEBUG ) { Trace.debug( this, " EXISTENCE of file " + file ); } boolean exists = false; try { RemoteFileManager fileManager = file.getFileManager( ); exists = fileManager.exists( file ); } catch ( RemoteException remote ) { if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); } Trace.log( this, ERR_REMOTE, remote ); } catch ( NotBoundException notBound ) { if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); } Trace.log( this, ERR_NOT_BOUND, notBound ); } catch ( MalformedURLException badURL ) { if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); } Trace.log( this, ERR_MALFORMED_URL, badURL ); } catch ( Exception exception ) { if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); } Trace.log( this, ERR_OTHER, exception ); } return exists; } /** *

Copier un fichier d'un répertoire à un autre.

*

Actuellement, ne prend en compte que des fichiers copiés localement sur une même machine.

*

On fait l'hypothèse que filename est un chemin relatif au répertoire from, * et que les répertoires from et to sont absolus.

* */ protected boolean copyFile( String filename, String from, String to ) throws IOException { boolean success = true; // Copy file File fromFile = new File( from + File.separator + filename ); File toFile = new File( to + File.separator + filename ); FileCopy2.copy( fromFile, toFile ); return success; } /** *

Suppression d'un répertoire et de tout son contenu .

* * @param dirToDelete : répertoire à supprimer (avec un chemin en absolu). * */ public void deleteAllDir(String dir) { File temp = new File(dir); if ( temp.exists() ) { File[] list = temp.listFiles(); for (int i = 0 ; i < list.length; i++ ) { if ( list[i].isDirectory() ) { deleteAllDir(list[i].getAbsolutePath()); } else { if (DEBUG) Trace.debug(this," DELETING: " + list[i].getAbsoluteFile()); list[i].delete(); } } temp.delete(); if (DEBUG) Trace.debug(this," DELETING: " + temp.getAbsoluteFile()); } } /** *

Création d'un répertoire.

*

Crée les répertoires parents inexistants, si nécessaire.

* * @param dir : répertoire à créer (avec un chemin en absolu). * */ public boolean mkdir( String dir ) { if (DEBUG) { Trace.debug(this," CREATING dir: " + dir ); } boolean success = false; File temp = new File( dir ); if (DEBUG) { Trace.debug(this," CREATING: " + temp.getAbsoluteFile() ); } if ( !temp.exists() ) { success = temp.mkdirs( ); if ( !success ) { if (DEBUG) { Trace.debug( this, " FILE is not a directory: " + temp.getAbsoluteFile() ); } } } else { if (DEBUG) { Trace.debug(this," DIRECTORY already exists: " + temp.getAbsoluteFile() ); } success = true; } return success; } /** * Obtenir un serveur de fichier sur un machine donnée. * * @param host : la machine à atteindre * @return : une référence vers le serveur RMI */ public RemoteFileManager getFileServer( String host) { RemoteFileManager fileServer = null; try { fileServer = (RemoteFileManager) Naming.lookup( "//" + host + "/" + RemoteFileManager.IF_REMOTE_FILE_MANAGER ); } catch ( MalformedURLException badURL ) { Trace.log(this,"Bad URL", badURL); } catch ( NotBoundException notBound ) { Trace.log(this,"File server not bound", notBound); } catch ( RemoteException remote ) { Trace.log(this,"Error connecting to file server", remote); } return fileServer; } }