source: ether_core/trunk/kit/interactifKit/zipContent/src/FileManager.java @ 784

Last change on this file since 784 was 6, checked in by cbipsl, 18 years ago

ajout rep ether

  • Property svn:executable set to *
File size: 19.4 KB
Line 
1/**
2 *      <p>Cette classe gère les resources et services sur les systèmes de fichiers Ether.</p>
3 *      <p>Cette classe implémente le Singleton pattern exposé par
4 *      Gamma et al. dans leur livre désormais célèbre.</p>
5 *      <p>Elle permet d'instancier des singletons nommés et de gérer
6 *      tous les singletons d'une application.</p>
7 *
8 * @since JDK 1.1.x
9 * @version $Id: FileManager.java,v 1.1 2001/01/15 15:38:47 pn Exp $
10 * @author Phillip Link
11 */
12package fr.alcatel.ether.app.common;
13
14//import fr.alcatel.ether.app.order.*;
15
16// JDK
17import java.util.List;
18import java.util.ArrayList;
19import java.util.Iterator;
20import java.util.Enumeration;
21import java.io.File;
22import java.io.IOException;
23// JDK - rmi
24import java.rmi.Naming;
25import java.rmi.RemoteException;
26import java.rmi.NotBoundException;
27// JDK - net
28import java.net.MalformedURLException;
29// application classes
30import fr.alcatel.ether.app.user.User;
31//import fr.alcatel.ether.app.user.Media;
32import fr.alcatel.ether.app.service.Service;
33//import fr.alcatel.ether.app.service.Services;
34import fr.alcatel.ether.app.data.EtherFileName;
35// utility classes
36import fr.alcatel.ether.tools.singleton.Singleton;
37import fr.alcatel.ether.app.common.Config;
38import fr.alcatel.ether.app.exception.EtherException ;
39//import fr.alcatel.ether.persistency.DatabaseException;
40import fr.alcatel.ether.tools.Trace;
41import flafi.util.FileCopy2;
42// Workflow
43//import fr.alcatel.ether.tools.workflow.exception.NotAssigned;
44//import fr.alcatel.ether.tools.workflow.RemoteWfServer;
45//import fr.alcatel.ether.tools.workflow.WfServer;
46
47// Ether workflow
48//import fr.alcatel.ether.app.order.workflow.FileResource;
49
50public class FileManager extends Singleton {
51       
52        // Debugging
53        static private final boolean DEBUG = true;
54       
55        /** Messages d'erreur - MalformedURLException */
56        static public final String ERR_MALFORMED_URL    = "Error invalid address for Remote File Manager";
57        static public final String ERR_NOT_BOUND                        = "Error Remote File Manager is not bound";
58        static public final String ERR_REMOTE                                   = "Error retrieving Remote File Manager";
59        static public final String ERR_OTHER                                    = "Error";
60       
61        // Singleton instance
62        static final private FileManager _fileMgr = new FileManager( );
63       
64        /** Répertoire temporaire de la machine (/tmp ou /var/tmp )*/
65        /** Ce répertoire est utlisé pour y extraire les fichiers */
66        private String tmpDir = "/tmp" ;
67
68        static final private String ETHER = "ETHER_TMP_DIRECTORY";
69       
70        /**
71         *      <p>Construire le manager pour le traitement de services sur systèmes de fichiers.</p>
72         *
73         */
74        protected FileManager( ) {
75                Singleton.register( getClass().toString(), this );
76                Config.getConfig();                                                                             
77                Trace.getTrace("ether");
78                // initialisation du répertoire tmp
79                try {
80                File tempFile = File.createTempFile( "eth", null );
81                tmpDir = tempFile.getParent( );
82                tempFile.delete( );
83                }
84                catch( IOException e )
85                {
86                        Trace.log(this," ERROR WHILE RETREIVING TMP DIR :" + e.toString());
87                }
88        }
89
90
91        /**
92         *      <p>Obtenir la machine hôte des données dans l'espace Ether.</p>
93         *
94         *
95         *      @return le nom de la machine hôte contenant les données Ether
96         */
97        public String getDataHost( ) {
98                return Config.dataHost;
99        }
100
101        /**
102         *      <p>Obtenir la machine hôte contenant les répertoires utilisateur dans l'espace Ether.</p>
103         *
104         *
105         *      @return le nom de la machine hôte contenant les répertoires utilisateur Ether
106         */
107        public String getUsersHost( ) {
108                return Config.usersHost;
109        }
110
111        /**
112         *      <p>Obtenir la machine hôte des services dans l'espace Ether.</p>
113         *
114         *
115         *      @return le nom de la machine hôte contenant les services Ether
116         */
117        public String getServicesHost( ) {
118                return Config.servicesHost;
119        }
120
121        /**
122         *      <p>Obtenir la machine hôte des répertoires temporaires dans l'espace Ether.</p>
123         *
124         *
125         *      @return le nom de la machine hôte contenant les répertoires temporaires Ether
126         */
127        public String getTempHost( ) {
128                return Config.tempHost;
129        }
130
131        /**
132         *      <p>Obtenir la machine hôte des répertoires pour fichies offline dans l'espace Ether.</p>
133         *
134         *
135         *      @return le nom de la machine hôte contenant les répertoires offline Ether
136         */
137        public String getOfflineHost( ) {
138                return Config.offlineHost;
139        }
140
141        /**
142         *      <p>Obtenir la machine hôte des répertoires pour la création des média dans l'espace Ether.</p>
143         *
144         *
145         *      @return le nom de la machine hôte contenant les répertoires média Ether
146         */
147        public String getMediaHost( ) {
148                return Config.mediaHost;
149        }
150
151       
152        /**
153         *      <p>Obtenir le répertoire des fichiers offline récupérés dans l'espace Ether.</p>
154         *
155         *
156         *      @return le chemin absolu du répertoire des données
157         */
158        public String getOfflineDir( User user ) {
159                StringBuffer temp = new StringBuffer( Config.offlineDir );
160                temp.append( File.separator );
161                temp.append( user.getLogin() );
162                return temp.toString( );
163        }
164
165        /**
166         *      <p>Obtenir le répertoire des données dans l'espace Ether.</p>
167         *
168         *
169         *      @return le chemin absolu du répertoire des données
170         */
171        public String getDataDir( ) {
172                return Config.dataDir;
173        }
174
175        /**
176         *      <p>Obtenir le répertoire utilisateur dans l'espace Ether.</p>
177         *
178         *      @param  user:   l'utilisateur
179         *
180         *      @return le chemin absolu du répertoire utilisateur
181         */
182        public String getUserDir( User user ) {
183                StringBuffer temp = new StringBuffer( user.getRepository()  );
184                //temp.append( File.separator );
185                //temp.append( user.getLogin() );
186                return temp.toString( );
187        }
188
189        /**
190         *      <p>Obtenir le répertoire temporaire pour utilisateur dans l'espace Ether.</p>
191         *
192         *      @param  user:   l'utilisateur
193         *
194         *      @return le chemin absolu du répertoire temporaire pour un utilisateur donné
195         */
196        public String getTempDir( User user ) {
197                StringBuffer temp = new StringBuffer( Config.tempDir );
198                temp.append( File.separator );
199                temp.append( user.getLogin() );
200                return temp.toString( );
201        }
202
203        /**
204         *      <p>Obtenir le répertoire pour déposer des fichiers à traiter par un service dans l'espace Ether.</p>
205         *
206         *      @param  user:   l'utilisateur
207         *
208         *      @return le chemin absolu du répertoire de services pour un utilisateur donné
209         */
210        public String getServiceDir( User user ) {
211                StringBuffer temp = new StringBuffer( Config.servicesDir );
212                temp.append( File.separator );
213                temp.append( user.getLogin() );
214                return temp.toString( );
215        }
216
217        /**
218         *      <p>Obtenir le répertoire pour déposer l'image d'un média dans l'espace Ether.</p>
219         *
220         *      @param  user:   l'utilisateur
221         *
222         *      @return le chemin absolu du répertoire de services pour un utilisateur donné
223         */
224        public String getMediaDir( User user ) {
225                StringBuffer temp = new StringBuffer( Config.mediaDir );
226                temp.append( File.separator );
227                temp.append( user.getLogin() );
228                return temp.toString( );
229        }
230
231       
232                /**
233         *      <p>Obtenir le répertoire temporaire de désarchivage pour une commande.</p>
234         *
235         *      @param  orderId:        l'identifiant de la commande
236         *
237         *      @return le chemin absolu du répertoire temporaire de désarchivage pour une commande
238         */
239        public String getArchiveOrderDir( int orderId ) {
240                StringBuffer dir = new StringBuffer( getHostTmpDir() );
241                dir.append( File.separator );
242                dir.append( orderId );
243                return dir.toString( );
244        }
245
246
247       
248        /**
249         *      <p>Obtenir le répertoire temporaire d'une machine.</p>
250         *
251         *      @return le chemin absolu du répertoire temporaire de la machine.
252         */
253        public String getHostTmpDir() {
254                StringBuffer dir = new StringBuffer(tmpDir);
255                dir.append( File.separator );
256                dir.append(ETHER);
257                return dir.toString() ;
258        }
259       
260
261        /**
262         *      <p>Obtenir le répertoire d'entrée services pour un lancement interactif dans l'espace Ether.</p>
263         *
264         *      @param  User : utilisateur courant
265         *      @param  processId : un identifiant unique pour le lancement interactif.
266         *
267         *      @return le chemin absolu du répertoire services (où seront posés les données).
268         */
269        public String getServiceInteractifInDir( User user, int processId ) {
270                StringBuffer dir = new StringBuffer( getServiceInteractifDir(user,processId) );
271                dir.append( File.separator );
272                dir.append( Config.serviceInDir );
273                return dir.toString( );
274        }
275       
276        /**
277         *      <p>Obtenir le répertoire de sortie services pour un lancement interactif dans l'espace Ether.</p>
278         *
279         *      @param  User : utilisateur courant
280         *      @param  processId : un identifiant unique pour le lancement interactif.
281         *
282         *      @return le chemin absolu du répertoire services (où seront posés les données).
283         */
284        public String getServiceInteractifOutDir( User user, int processId ) {
285                StringBuffer dir = new StringBuffer( getServiceInteractifDir(user,processId) );
286                dir.append( File.separator );
287                dir.append( Config.serviceOutDir );
288                return dir.toString( );
289        }
290
291        /**
292         *      <p>Obtenir le répertoire de services pour un lancement interactif dans l'espace Ether.</p>
293         *
294         *      @param  User : utilisateur courant
295         *      @param  processId : un identifiant unique pour le lancement interactif.
296         *
297         *      @return le chemin absolu du répertoire services (où seront posés les données).
298         */
299        public String getServiceInteractifDir(User user, int processId) {
300                StringBuffer dir = new StringBuffer( Config.servicesInteractifDir );
301                dir.append( File.separator );
302                dir.append( user.getLogin() );
303                dir.append( File.separator );
304                dir.append( processId );
305                return dir.toString( );
306        }
307
308        /**
309        * <p>Obtenir le répertoire résultat pour l'exécution d'un service intéractif
310        *
311        * @param user : l'utilisateur qui lance le service.
312        * @param service : le service intéractif.
313        * @param processId : un identifiant unique pour le lancement interactif.
314        *
315        * @return le chemin absolu du répertoire résultat.
316        */
317        public String getServiceInteractifResultDir(User user,Service service,int processId)
318        {
319                StringBuffer dir = new StringBuffer(getUserDir(user));
320                dir.append(File.separator).append(service.getName()).append( "_" ).append(processId).append(File.separator).append(Config.serviceOutDir);
321                return dir.toString(); 
322        }
323               
324        /**
325         *      Copier un fichier distant.
326         *
327         *      @param  src:    le fichier source
328         *      @param  dest:   le fichier destination
329         */
330  public boolean copy( RemoteFile src, RemoteFile dest ) {
331        boolean success = false;
332        try {
333            RemoteFileManager destManager = (RemoteFileManager) dest.getFileManager( );
334            if ( DEBUG ) { Trace.debug( this, "Copying file '" + src + "' to '" + dest + "' : " + new Boolean(success) ); }
335            success = destManager.copy( src, dest );
336        }
337        catch ( RemoteException remote ) {
338            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE ); }
339                Trace.log( this, ERR_REMOTE, remote );
340        }
341        catch ( NotBoundException notBound ) {
342            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND ); }
343                Trace.log( this, ERR_NOT_BOUND, notBound );
344        }
345        catch ( MalformedURLException badURL ) {
346            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL ); }
347                Trace.log( this, ERR_MALFORMED_URL, badURL );
348        }
349        return success;
350  }
351 
352        /**
353         *      Supprimer un fichier distant.
354         *
355         *      @param  src:    le fichier source
356         */
357  public boolean delete( RemoteFile file ) {
358        boolean success = false;
359        try {
360            RemoteFileManager fileManager = (RemoteFileManager) file.getFileManager( );
361            success = fileManager.delete( file );
362        }
363        catch ( RemoteException remote ) {
364            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
365                Trace.log( this, ERR_REMOTE, remote );
366        }
367        catch ( NotBoundException notBound ) {
368            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
369                Trace.log( this, ERR_NOT_BOUND, notBound );
370        }
371        catch ( MalformedURLException badURL ) {
372            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
373                Trace.log( this, ERR_MALFORMED_URL, badURL );
374        }
375        return success;
376}
377       
378
379
380        /**
381         *      Supprimer un répertoire distant.
382         *
383         *      @param  dir:    le répertoire à supprimer.
384         */
385  public boolean deleteDir( RemoteFile dir ) {
386        boolean success = false;
387        try {
388            RemoteFileManager fileManager = (RemoteFileManager) dir.getFileManager( );
389            success = fileManager.deleteDir( dir);
390        }
391        catch ( RemoteException remote ) {
392            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
393                Trace.log( this, ERR_REMOTE, remote );
394        }
395        catch ( NotBoundException notBound ) {
396            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
397                Trace.log( this, ERR_NOT_BOUND, notBound );
398        }
399        catch ( MalformedURLException badURL ) {
400            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
401                Trace.log( this, ERR_MALFORMED_URL, badURL );
402        }
403        return success;
404        }
405       
406       
407        /**
408         *      Créer un répertoire distant.
409         *
410         *      @param  dir:    le répertoire distant
411         */
412  public boolean mkdir( RemoteFile file ) {
413        boolean success = false;
414        try {
415            RemoteFileManager fileManager = (RemoteFileManager) file.getFileManager( );
416            success = fileManager.makeDir( file );
417        }
418        catch ( RemoteException remote ) {
419            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
420                Trace.log( this, ERR_REMOTE, remote );
421        }
422        catch ( NotBoundException notBound ) {
423            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
424                Trace.log( this, ERR_NOT_BOUND, notBound );
425        }
426        catch ( MalformedURLException badURL ) {
427            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
428                Trace.log( this, ERR_MALFORMED_URL, badURL );
429        }
430        return success;
431}
432       
433        /**
434         *      Lister les fichiers d'un répertoire distant.
435         *
436         *      @param  dir:    le répertoire distant
437         */
438  public ArrayList list( RemoteFile dir ) {
439        if ( DEBUG ) { Trace.debug( this, " LISTING remote directory " + dir ); }
440        ArrayList files = null;
441        try {
442            RemoteFileManager fileManager = dir.getFileManager( );
443            files = fileManager.list( dir );
444    }
445        catch ( RemoteException remote ) {
446            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
447                Trace.log( this, ERR_REMOTE, remote );
448        }
449        catch ( NotBoundException notBound ) {
450            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
451                Trace.log( this, ERR_NOT_BOUND, notBound );
452        }
453        catch ( MalformedURLException badURL ) {
454            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
455                Trace.log( this, ERR_MALFORMED_URL, badURL );
456        }
457        catch ( Exception exception ) {
458            if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); }
459                Trace.log( this, ERR_OTHER, exception );
460        }
461        return files;
462}
463       
464        /**
465         *      Déterminer la taille d'un fichier o de tous les fichiers dans un répertoire.
466         *      Cette méthode regarde dans tous les sous-répertoires.
467         *
468         *      @param  dir:    le fichier ou le répertoire distant
469         */
470  public long size( RemoteFile dir ) {
471        if ( DEBUG ) { Trace.debug( this, " SIZE of remote file or directory " + dir ); }
472        long length = 0;
473        try {
474            RemoteFileManager fileManager = (RemoteFileManager) dir.getFileManager( );
475            length = fileManager.size( dir );
476    }
477        catch ( RemoteException remote ) {
478            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
479                Trace.log( this, ERR_REMOTE, remote );
480        }
481        catch ( NotBoundException notBound ) {
482            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
483                Trace.log( this, ERR_NOT_BOUND, notBound );
484        }
485        catch ( MalformedURLException badURL ) {
486            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
487                Trace.log( this, ERR_MALFORMED_URL, badURL );
488        }
489        catch ( Exception exception ) {
490            if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); }
491                Trace.log( this, ERR_OTHER, exception );
492        }
493        return length;
494        }
495
496       
497        /**
498         *      Déterminer la taille d'un fichier o de tous les fichiers dans un répertoire.
499         *      Cette méthode regarde dans tous les sous-répertoires.
500         *
501         *      @param  dir:    le fichier ou le répertoire local
502         */
503  public long size( File dir ) {
504        long length = 0;
505        try {
506                if ( DEBUG ) { Trace.debug( this, "Checking size of " + dir.getAbsolutePath() ); }
507                        File[] list = dir.listFiles( );
508                        // File is a directory
509                        if ( list != null ) {
510                                // Add length of directory
511                                length += dir.length( );
512                                // Add length of all files in directory
513                                for (int i = 0 ; i < list.length; i++ )
514                                {
515                                        length += size( list[i] );
516                                }
517                        }
518                        // File is a regular file
519                        else {
520                                length = dir.length( );
521                        }
522                }
523                catch ( Exception exception ) {
524                  if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); }
525                        Trace.log( this, ERR_OTHER, exception );
526                }
527        return length;
528        }
529
530
531        /**
532         *      Déterminer si un fichier existe.
533         *
534         *      @param  file:   le fichier distant
535         */
536  public boolean exists( RemoteFile file ) {
537        if ( DEBUG ) { Trace.debug( this, " EXISTENCE of file " + file ); }
538        boolean exists = false;
539        try {
540            RemoteFileManager fileManager = file.getFileManager( );
541            exists = fileManager.exists( file );
542    }
543        catch ( RemoteException remote ) {
544            if ( DEBUG ) { Trace.debug( this, ERR_REMOTE, remote ); }
545                Trace.log( this, ERR_REMOTE, remote );
546        }
547        catch ( NotBoundException notBound ) {
548            if ( DEBUG ) { Trace.debug( this, ERR_NOT_BOUND, notBound ); }
549                Trace.log( this, ERR_NOT_BOUND, notBound );
550        }
551        catch ( MalformedURLException badURL ) {
552            if ( DEBUG ) { Trace.debug( this, ERR_MALFORMED_URL, badURL ); }
553                Trace.log( this, ERR_MALFORMED_URL, badURL );
554        }
555        catch ( Exception exception ) {
556            if ( DEBUG ) { Trace.debug( this, ERR_OTHER, exception ); }
557                Trace.log( this, ERR_OTHER, exception );
558        }
559        return exists;
560        }
561       
562
563        /**
564         *      <p>Copier un fichier d'un répertoire à un autre.</p>
565         *      <p>Actuellement, ne prend en compte que des fichiers copiés localement sur une même machine.</p>
566         *      <p>On fait l'hypothèse que filename est un chemin relatif au répertoire <code>from</code>,
567         *      et que les répertoires <code>from</code> et <code>to</code> sont absolus.</p>
568         *
569         */
570        protected boolean copyFile( String filename, String from, String to ) throws IOException {
571                boolean success = true;
572                // Copy file
573    File fromFile = new File( from + File.separator + filename );
574    File toFile = new File( to + File.separator + filename );
575    FileCopy2.copy( fromFile, toFile );
576                return success;
577        }
578
579 /**
580        *       <p> Suppression d'un répertoire et de tout son contenu .</p>
581        *
582        * @param dirToDelete : répertoire à supprimer (avec un chemin en absolu).
583        *
584        */
585        public void deleteAllDir(String dir)
586        {
587                File temp = new File(dir);
588                if ( temp.exists() )
589                {
590                        File[] list = temp.listFiles();
591                        for (int i = 0 ; i < list.length; i++ )
592                        {
593                                        if ( list[i].isDirectory() )
594                                        {
595                                                deleteAllDir(list[i].getAbsolutePath());
596                                        }
597                                        else
598                                        {
599                                                if (DEBUG) Trace.debug(this," DELETING: " + list[i].getAbsoluteFile());
600                                                list[i].delete();
601                                        }
602                        }
603                        temp.delete();
604                        if (DEBUG) Trace.debug(this," DELETING: " + temp.getAbsoluteFile());
605                }
606        }
607       
608        /**
609         *      <p>Création d'un répertoire.</p>
610         *      <p>Crée les répertoires parents inexistants, si nécessaire.</p>
611         *
612         * @param dir : répertoire à créer (avec un chemin en absolu).
613         *
614         */
615        public boolean mkdir( String dir )
616        {
617                if (DEBUG) { Trace.debug(this," CREATING dir: " + dir ); }
618                boolean success = false;
619                File temp = new File( dir );
620                if (DEBUG) { Trace.debug(this," CREATING: " + temp.getAbsoluteFile() ); }
621                if ( !temp.exists() )
622                {
623                        success = temp.mkdirs( );
624                        if ( !success )
625                        {
626                                if (DEBUG) { Trace.debug( this, " FILE is not a directory: " + temp.getAbsoluteFile() ); }
627                        }
628                }
629                else {
630                        if (DEBUG) { Trace.debug(this," DIRECTORY already exists: " + temp.getAbsoluteFile() ); }
631                        success = true;
632                }
633                return success;
634        }
635       
636        /**
637         *      Obtenir un serveur de fichier sur un machine donnée.
638         *
639         * @param host : la machine à atteindre
640         * @return      : une référence vers le serveur RMI
641         */
642        public RemoteFileManager getFileServer( String host) {
643                RemoteFileManager fileServer = null;
644                try {
645      fileServer = (RemoteFileManager) Naming.lookup( "//" + host + "/" + RemoteFileManager.IF_REMOTE_FILE_MANAGER );
646                }
647                catch ( MalformedURLException badURL ) {
648                        Trace.log(this,"Bad URL", badURL);
649                }
650                catch ( NotBoundException notBound ) {
651                        Trace.log(this,"File server not bound", notBound);
652                }
653                catch ( RemoteException remote ) {
654                        Trace.log(this,"Error connecting to file server", remote);
655                }
656                return fileServer;
657        }
658       
659}
Note: See TracBrowser for help on using the repository browser.