8 Replies Latest reply: Sep 24, 2008 3:18 AM by 843793 RSS

    Remote Browsing using RMI

    843793
      Hi,
      Read a lot of forums about how to create a Remote file browser using RMI and JFileChooser.
      Not done any RMI coding I thought I'd give it a try. I have finally after 2 days managed to do it.

      This is an overview.
      Create a RemoteFile class that extends Remote.. eg.
      import java.rmi.*;
      import java.io.*;
      import java.net.*;
      
      public interface RemoteFile extends Remote {
          
          public boolean canRead() throws RemoteException;
          public boolean canWrite() throws RemoteException;
          public int compareTo(File pathname) throws RemoteException;
      etc.etc

      Then create an implementation class...
      import java.rmi.server.*;
      import java.io.*;
      import java.net.*;
      
      public class RemoteFileImpl extends UnicastRemoteObject implements RemoteFile {
          
          private File f;
          
          public RemoteFileImpl(File sf) throws java.rmi.RemoteException {
              f=sf;
          }
          public boolean canRead() throws java.rmi.RemoteException {
              return f.canRead();
          }
      etc etc for all other File functions.

      Then we create a RemoteFileSystemView interface, again extending remote...
      public interface RemoteFileSystemView extends Remote {
          
          public RemoteFile createFileObject(File dir, String filename) throws RemoteException;
          public RemoteFile createFileObject(String path) throws RemoteException;
      blah blah

      Then An implementation class...
      import javax.swing.filechooser.*;
      import java.io.*;
      
      public class RemoteFileSystemViewImpl extends java.rmi.server.UnicastRemoteObject implements RemoteFileSystemView {
          
          int length;
          
          private FileSystemView fs;
          
          public RemoteFileSystemViewImpl() throws java.rmi.RemoteException {
              fs=FileSystemView.getFileSystemView();
          }
          
          public RemoteFile createFileObject(String path) throws java.rmi.RemoteException {
              return new RemoteFileImpl(fs.createFileObject(path));
          }
          
          public RemoteFile createFileObject(java.io.File dir, String filename) throws java.rmi.RemoteException {
              return new RemoteFileImpl(fs.createFileObject(dir,filename));
          }
          
          //public RemoteFileImpl createFileSystemRoot(java.io.File f) throws java.rmi.RemoteException {
          //    return new RemoteFileImpl(fs.createFileSystemRoot(f));
          // }
          
          public RemoteFile createNewFolder(java.io.File containingDir) throws java.rmi.RemoteException {
              RemoteFile tempf;
              try{
                  tempf= new RemoteFileImpl(fs.createNewFolder(containingDir));
              } catch (IOException e){System.out.println(e); tempf=null;}
              return  tempf;
          }
          
          public RemoteFile getDefaultDirectory() throws java.rmi.RemoteException {
              return new RemoteFileImpl(fs.getDefaultDirectory());
          }
          
          public String[] getFiles(java.io.File dir, boolean useFileHiding) throws java.rmi.RemoteException {
              String[] tempf= new String[fs.getFiles(dir,useFileHiding).length];
              int i;
              File[] f = fs.getFiles(dir,useFileHiding);
              for(i=0;i<tempf.length;i++) tempf=f[i].toString();
      return tempf;
      }

      public RemoteFile getHomeDirectory() throws java.rmi.RemoteException {
      return new RemoteFileImpl(fs.getHomeDirectory());
      }

      public RemoteFile getParentDirectory(java.io.File dir) throws java.rmi.RemoteException {
      return new RemoteFileImpl(fs.getParentDirectory(dir));
      }

      public String[] getRoots() throws java.rmi.RemoteException {
      String[] tempf= new String[fs.getRoots().length];
      System.out.println("fs is-"+fs.getRoots().length);
      int i;
      File[] f = fs.getRoots();
      for(i=0;i<tempf.length;i++) {tempf[i]=f[i].toString();}
      System.out.println("Roots="+tempf[0]);
      return tempf;
      }

      public String getSystemDisplayName(java.io.File f) throws java.rmi.RemoteException {
      return fs.getSystemDisplayName(f);
      }

      public javax.swing.Icon getSystemIcon(java.io.File f) throws java.rmi.RemoteException {
      return fs.getSystemIcon(f);
      }

      public String getSystemTypeDescription(java.io.File f) throws java.rmi.RemoteException {
      return fs.getSystemTypeDescription(f);
      }

      public boolean isComputerNode(java.io.File dir) throws java.rmi.RemoteException {
      return fs.isComputerNode(dir);
      }

      public boolean isDrive(java.io.File dir) throws java.rmi.RemoteException {
      return fs.isDrive(dir);
      }

      public boolean isFileSystem(java.io.File f) throws java.rmi.RemoteException {
      return fs.isFileSystem(f);
      }

      public boolean isFileSystemRoot(java.io.File dir) throws java.rmi.RemoteException {
      return fs.isFileSystemRoot(dir);
      }

      public boolean isFloppyDrive(java.io.File dir) throws java.rmi.RemoteException {
      return fs.isFloppyDrive(dir);
      }

      public boolean isHiddenFile(java.io.File f) throws java.rmi.RemoteException {
      return fs.isHiddenFile(f);
      }

      public boolean isParent(java.io.File folder, java.io.File file) throws java.rmi.RemoteException {
      return fs.isParent(folder,file);
      }

      public boolean isRoot(java.io.File f) throws java.rmi.RemoteException {
      return fs.isRoot(f);
      }

      public Boolean isTraversable(java.io.File f) throws java.rmi.RemoteException {
      return fs.isTraversable(f);
      }

      public RemoteFile getChild(File parent, String fileName) throws java.rmi.RemoteException {
      return new RemoteFileImpl(fs.getChild(parent,fileName));
      }

      }

      Notice that whenever I should be passing a File, I pass a RemoteFile instead! Also.. when I am passing a File Array, I instead pass a String array. Reason is.. I havent managed to get File arrays through RMI.. Any Help anyone?!?!?!?!
      I got round it by simply passing a String array and relying on the client to reconstruct a File array locally using the String array. Messy... perhaps.. any suggestions welcome!

      Next.. use rmic to create Stubs and Skels for the 2 implementations and write an RMI server.. eg.
      import java.rmi.Naming;
      
      public class RemoteServerRMI {
          
          /** Creates a new instance of RemoteServerRMI */
          public RemoteServerRMI() {
              try {
                  RemoteFileSystemView c = new RemoteFileSystemViewImpl();
                  Naming.rebind("rmi://localhost:1099/FileService", c);
              } catch (Exception e) {
                  System.out.println("Trouble: " + e);
              }
              
          }
          
          
          public static void main(String[] args) {
              new RemoteServerRMI();
          }
          
      }
      Yes.. simple and shamelessly lifted from the tutorials.

      and Finally the client....
      import javax.swing.filechooser.*;
      import java.rmi.*;
      import java.net.MalformedURLException;
      import java.io.*;
      import javax.swing.*;
      
      public class RemoteFileSystemViewClient extends FileSystemView {
          
          static RemoteFileSystemView c;
          
          public RemoteFileSystemViewClient() {
              try {
                  c = (RemoteFileSystemView) Naming.lookup("rmi://localhost:1099/FileService");
              }
              catch (MalformedURLException murle) {
                  System.out.println();
                  System.out.println(
                  "MalformedURLException");
                  System.out.println(murle);
              }
              catch (RemoteException re) {
                  System.out.println();
                  System.out.println(
                  "RemoteException");
                  System.out.println(re);
              }
              catch (NotBoundException nbe) {
                  System.out.println();
                  System.out.println(
                  "NotBoundException");
                  System.out.println(nbe);
              }
              
          }
          public File getHomeDirectory() {
              File tempf;
              try {
                  tempf= new File(c.getHomeDirectory().gettoString());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File createFileObject(File dir, String filename) {
              File tempf;
              try {
                  tempf= new File(c.createFileObject(dir, filename).gettoString());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File createFileObject(String path) {
              File tempf;
              try {
                  tempf=new File(c.createFileObject(path).gettoString());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File createNewFolder(File containingDir) throws RemoteException {
              File tempf;
              try {
                  tempf=new File(c.createNewFolder(containingDir).getAbsolutePath());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File getChild(File parent, String fileName) {
              File tempf;
              try {
                  tempf=new File(c.getChild(parent,fileName).gettoString());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File getDefaultDirectory() {
              File tempf;
              try {
                  tempf=new File(c.getDefaultDirectory().gettoString());
              } catch (RemoteException re){System.out.println(re);tempf=null;}
              return tempf;
          }
          
          public File[] getFiles(File dir, boolean useFileHiding) {
              File[] tempf;
              try {
                  tempf=new File[c.getFiles(dir,useFileHiding).length];
                  String[] rtempf=c.getFiles(dir, useFileHiding);
                  int i;
                  for(i=0;i<c.getFiles(dir, useFileHiding).length;i++) tempf=new File(rtempf[i]);
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }

      public File getParentDirectory(File dir) {
      File tempf;
      try {
      tempf= new File(c.getParentDirectory(dir).gettoString());
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }

      public File[] getRoots(){
      File[] tempf;
      try {
      String[] rtempf= c.getRoots();
      System.out.println("in");
      tempf=new File[rtempf.length];
      int i;
      for(i=0;i<c.getRoots().length;i++) tempf[i]=new File(rtempf[i]);
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }

      public String getSystemDisplayName(File f) {
      String tempf;
      try{
      tempf=c.getSystemDisplayName(f);
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }
      public javax.swing.Icon getSystemIcon(File f) {
      javax.swing.Icon tempf;
      try{
      tempf=c.getSystemIcon(f);
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }

      public String getSystemTypeDescription(File f) {
      String tempf;
      try {
      tempf=c.getSystemTypeDescription(f);
      } catch (RemoteException re){System.out.println(re);tempf=null;}
      return tempf;
      }

      public boolean isComputerNode(File dir) {
      boolean tempf;
      try {
      tempf= c.isComputerNode(dir);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isDrive(File dir) {
      boolean tempf;
      try {
      tempf= c.isDrive(dir);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isFileSystem(File f) {
      boolean tempf;
      try {
      tempf= c.isFileSystem(f);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isFileSystemRoot(File dir) {
      boolean tempf;
      try {
      tempf= c.isFileSystemRoot(dir);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isFloppyDrive(File dir) {
      boolean tempf;
      try {
      tempf= c.isFloppyDrive(dir);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isHiddenFile(File f) {
      boolean tempf;
      try {
      tempf= c.isHiddenFile(f);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }
      public boolean isParent(File folder, File file) {
      boolean tempf;
      try {
      tempf= c.isParent(folder,file);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }

      public boolean isRoot(File f) {
      boolean tempf;
      try {
      tempf= c.isRoot(f);
      } catch (RemoteException re){System.out.println(re);tempf=false;}
      return tempf;
      }
      public Boolean isTraversable(File f) {
      Boolean tempf;
      try {
      tempf= c.isTraversable(f);
      } catch (RemoteException re){System.out.println(re);tempf=new Boolean(false);}
      return tempf;
      }

      public static void main(String[] args) {
      RemoteFileSystemViewClient rc = new RemoteFileSystemViewClient();

      JFileChooser fch= new JFileChooser(rc);
      fch.showOpenDialog(null);
      }
      }

      Notice.. the getFiles and getRoots, procedures, get String arrays from the RMI and reconstruct local File arrays.
      I managed to browse up and down directories with this, and even.. to my surprise, create folders!

      If you got any improvements please let me know at gpetroui@csc.com
        • 1. Re: Remote Browsing using RMI
          843793
          Hi,

          Maybe my idea can help you. You can use java.util.Vector class instead of File class arrays. You seem to use very old JDK since you need Skel classes. Why don't you use the latest J2SDk 1.4.2_03? Maybe, your troubles would be reduced...

          Bye
          • 2. Re: Remote Browsing using RMI
            843793
            Did somebody make it work ? Since most of JFileChooser calls to 'File' are static, the remote directory tree is mis-interpreted (local check)
            • 3. Re: Remote Browsing using RMI
              EJP
              Copyright &copy; Esmond Pitt, 2007. All rights reserved.
              /*
               * RemoteFileSystem.java
               *
               * Created on 13 May 2007, 14:39
               */
              
              import java.io.File;
              import java.io.IOException;
              import java.rmi.Remote;
              import java.rmi.RemoteException;
              import javax.swing.Icon;
              
              /**
               *
               * @author Esmond Pitt
               */
              public interface RemoteFileSystem extends Remote
              {
                   File createFileObject(String path) throws RemoteException;
              
                   File[] getFiles(File dir, boolean useFileHiding) throws RemoteException;
              
                   File createFileObject(File dir, String filename) throws RemoteException;
              
                   File getChild(File parent, String fileName) throws RemoteException;
              
                   boolean isFloppyDrive(File dir) throws RemoteException;
              
                   boolean isFileSystemRoot(File dir) throws RemoteException;
              
                   boolean isFileSystem(File f) throws RemoteException;
              
                   boolean isDrive(File dir) throws RemoteException;
              
                   boolean isComputerNode(File dir) throws RemoteException;
              
                   File createNewFolder(File containingDir) throws RemoteException, IOException;
              
                   File getParentDirectory(File dir) throws RemoteException;
              
                   String getSystemDisplayName(File f) throws RemoteException;
              
                   Icon getSystemIcon(File f) throws RemoteException;
              
                   String getSystemTypeDescription(File f) throws RemoteException;
              
                   File getDefaultDirectory() throws RemoteException;
              
                   File getHomeDirectory() throws RemoteException;
              
                   File[] getRoots() throws RemoteException;
                   
                   // File     createFileSystemRoot(File f) throws RemoteException;
                   
              }
              
              import java.io.File;
              import java.io.IOException;
              import java.rmi.RemoteException;
              import java.rmi.server.UnicastRemoteObject;
              import java.util.logging.Level;
              import java.util.logging.Logger;
              import javax.swing.Icon;
              import javax.swing.filechooser.FileSystemView;
              
              /**
               *
               * @author Esmond Pitt
               * @version $Revision: 3 $
               */
              public class RemoteFileSystemServer extends UnicastRemoteObject implements RemoteFileSystem
              {
                   private FileSystemView     fs = FileSystemView.getFileSystemView();
                   private Logger     logger = Logger.getLogger(this.getClass().getName());
                   
                   /** Creates a new instance of RemoteFileSystemServer */
                   public RemoteFileSystemServer(int port) throws RemoteException
                   {
                        super(port);
                        logger.log(Level.INFO, "exported on port {0} and receiving calls fs={1}", new Object[]{port, fs});
                   }
              
                   public File createFileObject(String path)
                   {
                        logger.log(Level.FINE, "createFileObject({0})", path);
                        return fs.createFileObject(path);
                   }
              
                   public File createFileObject(File dir, String filename)
                   {
                        logger.log(Level.FINE, "createFileObject({0},{1})", new Object[]{dir, filename});
                        return fs.createFileObject(dir, filename);
                   }
                   
                   public File[] getFiles(File dir, boolean useFileHiding)
                   {
                        logger.log(Level.FINE, "getFiles({0},{1})", new Object[]{dir, useFileHiding});
                        return fs.getFiles(dir, useFileHiding);
                   }
              
                   public File getChild(File parent, String fileName)
                   {
                        logger.log(Level.FINE, "getChild({0},{1})", new Object[]{parent, fileName});
                        return fs.getChild(parent, fileName);
                   }
              
                   public boolean isFloppyDrive(File dir)
                   {
                        logger.log(Level.FINE, "isFloppyDrive({0})", dir);
                        return fs.isFloppyDrive(dir);
                   }
              
                   public boolean isFileSystemRoot(File dir)
                   {
                        logger.log(Level.FINE, "isFileSystemRoot({0})", dir);
                        return fs.isFileSystemRoot(dir);
                   }
              
                   public boolean isFileSystem(File f)
                   {
                        logger.log(Level.FINE, "isFileSystem({0})", f);
                        return fs.isFileSystem(f);
                   }
              
                   public boolean isDrive(File dir)
                   {
                        logger.log(Level.FINE, "isDrive({0})", dir);
                        return fs.isDrive(dir);
                   }
              
                   public boolean isComputerNode(File dir)
                   {
                        logger.log(Level.FINE, "isComputerNode({0})", dir);
                        return fs.isComputerNode(dir);
                   }
              
                   public File createNewFolder(File containingDir) throws IOException
                   {
                        logger.log(Level.FINE, "createNewFolder({0})", containingDir);
                        return fs.createNewFolder(containingDir);
                   }
              
                   public File getParentDirectory(File dir)
                   {
                        logger.log(Level.FINE, "getParentDirectory({0})", dir);
                        return fs.getParentDirectory(dir);
                   }
              
                   public String getSystemDisplayName(File f)
                   {
                        logger.log(Level.FINE, "getSystemDisplayName({0})", f);
                        return fs.getSystemDisplayName(f);
                   }
              
                   public Icon getSystemIcon(File f)
                   {
                        logger.log(Level.FINE, "getSystemIcon({0})", f);
                        return fs.getSystemIcon(f);
                   }
              
                   public String getSystemTypeDescription(File f)
                   {
                        logger.log(Level.FINE, "getSystemTypeDescription({0})", f);
                        return fs.getSystemTypeDescription(f);
                   }
              
                   public File getDefaultDirectory()
                   {
                        logger.log(Level.FINE, "getDefaultDirectory()");
                        return fs.getDefaultDirectory();
                   }
              
                   public File getHomeDirectory()
                   {
                        logger.log(Level.FINE, "getHomeDirectory()");
                        return fs.getHomeDirectory();
                   }
              
                   public File[] getRoots()
                   {
                        logger.log(Level.FINE, "getRoots()");
                        return fs.getRoots();
                   }
              
                   /*
                    public File createFileSystemRoot(File f)
                   {
                        return fs.createFileSystemRoot(f);
                   }
                    */
                   
              }
              import java.io.File;
              import java.io.IOException;
              import java.net.MalformedURLException;
              import java.rmi.Naming;
              import java.rmi.NotBoundException;
              import java.rmi.RemoteException;
              import java.util.logging.Level;
              import java.util.logging.Logger;
              import javax.swing.Icon;
              import javax.swing.filechooser.FileSystemView;
              
              /**
               *
               * @author Esmond Pitt
               * @version $Revision: 4 $
               */
              public class RemoteFileSystemView extends FileSystemView
              {
                   private Logger     logger = Logger.getLogger(this.getClass().getName());
                   private RemoteFileSystem     fs;
                   
                   public RemoteFileSystemView(String host)
                   throws NotBoundException, RemoteException, MalformedURLException
                   {
                        String     url = "rmi://"+host+"/"+RemoteFileSystem.class.getName();
                        this.fs = (RemoteFileSystem)Naming.lookup(url);
                        logger.log(Level.INFO, "Connected to {0} via {1}", new Object[]{url, fs});
                   }
                   
                   public File createFileObject(String path)
                   {
                        logger.entering(this.getClass().getName(), "createFileObject", path);
                        try
                        {
                             return fs.createFileObject(path);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "path="+path, exc);
                             return null;
                        }
                   }
              
                   public File[] getFiles(File dir, boolean useFileHiding)
                   {
                        logger.entering(this.getClass().getName(), "getFiles", new Object[]{dir, useFileHiding});
                        try
                        {
                             return fs.getFiles(dir, useFileHiding);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir+" useFileHiding="+useFileHiding, exc);
                             return null;
                        }
                   }
              
                   public File createFileObject(File dir, String filename)
                   {
                        logger.entering(this.getClass().getName(), "createFileObject", new Object[]{dir, filename});
                        try
                        {
                             return fs.createFileObject(dir, filename);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir+" filename="+filename, exc);
                             return null;
                        }
                   }
              
                   public File getChild(File parent, String fileName)
                   {
                        logger.entering(this.getClass().getName(), "getChild", new Object[]{parent, fileName});
                        try
                        {
                             return fs.getChild(parent, fileName);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "parent="+parent+" fileName="+fileName, exc);
                             return null;
                        }
                   }
              
                   public boolean isFloppyDrive(File dir)
                   {
                        logger.entering(this.getClass().getName(), "isFloppyDrive", dir);
                        try
                        {
                             return fs.isFloppyDrive(dir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir, exc);
                             return false;
                        }
                   }
              
                   public boolean isFileSystemRoot(File dir)
                   {
                        logger.entering(this.getClass().getName(), "isFileSystemRoot", dir);
                        try
                        {
                             return fs.isFileSystemRoot(dir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir, exc);
                             return false;
                        }
                   }
              
                   public boolean isFileSystem(File file)
                   {
                        logger.entering(this.getClass().getName(), "isFileSystem", file);
                        try
                        {
                             return fs.isFileSystem(file);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "file="+file, exc);
                             return false;
                        }
                   }
              
                   public boolean isDrive(File dir)
                   {
                        logger.entering(this.getClass().getName(), "isDrive", dir);
                        try
                        {
                             return fs.isDrive(dir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir, exc);
                             return false;
                        }
                   }
              
                   public boolean isComputerNode(File dir)
                   {
                        logger.entering(this.getClass().getName(), "isComputerNode", dir);
                        try
                        {
                             return fs.isComputerNode(dir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir, exc);
                             return false;
                        }
                   }
              
                   public File createNewFolder(File containingDir) throws IOException
                   {
                        logger.entering(this.getClass().getName(), "createNewFolder", containingDir);
                        try
                        {
                             return fs.createNewFolder(containingDir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "containingDir="+containingDir, exc);
                             return null;
                        }
                   }
              
                   public File getParentDirectory(File dir)
                   {
                        logger.entering(this.getClass().getName(), "getParentDirectory", dir);
                        try
                        {
                             return fs.getParentDirectory(dir);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "dir="+dir, exc);
                             return null;
                        }
                   }
              
                   public String getSystemDisplayName(File file)
                   {
                        logger.entering(this.getClass().getName(), "getSystemDisplayName", file);
                        try
                        {
                             return fs.getSystemDisplayName(file);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "file="+file, exc);
                             return null;
                        }
                   }
              
                   public Icon getSystemIcon(File file)
                   {
                        logger.entering(this.getClass().getName(), "getSystemIcon", file);
                        try
                        {
                             return fs.getSystemIcon(file);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "file="+file, exc);
                             return null;
                        }
                   }
              
                   public String getSystemTypeDescription(File file)
                   {
                        logger.entering(this.getClass().getName(), "getSystemTypeDescription", file);
                        try
                        {
                             return fs.getSystemTypeDescription(file);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "file="+file, exc);
                             return null;
                        }
                   }
              
                   public File getDefaultDirectory()
                   {
                        logger.entering(this.getClass().getName(), "getDefaultDirectory");
                        try
                        {
                             return fs.getDefaultDirectory();
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "getDefaultDirectory()", exc);
                             return null;
                        }
                   }
              
                   public File getHomeDirectory()
                   {
                        logger.entering(this.getClass().getName(), "getHomeDirectory");
                        try
                        {
                             return fs.getHomeDirectory();
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "getHomeDirectory()", exc);
                             return null;
                        }
                   }
              
                   public File[] getRoots()
                   {
                        logger.entering(this.getClass().getName(), "getRoots");
                        try
                        {
                             return fs.getRoots();
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "getRoots()", exc);
                             return null;
                        }
                   }
              
                   protected File createFileSystemRoot(File file)
                   {
                        logger.entering(this.getClass().getName(), "createFileSystemRoot", file);
                        logger.log(Level.SEVERE, "createFileSystemRoot called with ({0})", new Object[]{file});
                        /*
                        try
                        {
                             return fs.createFileSystemRoot(file);
                        }
                        catch (RemoteException exc)
                        {
                             logger.log(Level.SEVERE, "", exc);
                        }
                         */
                        return null;
                   }
                   
              }
              • 4. Re: Remote Browsing using RMI
                843793
                Cool ! Thanks for the sample. However, as soon as I create the JFileChooser (locally) using th FileSystemView (remote through RMI). It displays the right files and directories, but always try to 'resolve/identify' files locally : I mean, to display a "folder icon" or a "file icon", the check seems made locally (by static calls to File, I guess). This made the JFileChooser unusable since most of the directory, that does not exist locally are then reported as file (even if the isTraverable returns true).

                The RemoteFileSystemServer is running on the remote host. showing log as expected
                The RemoteFileSystemView is created locally, connection is fine.

                          JFileChooser fc = null;
                          try {
                                    RemoteFileSystemView fs = new RemoteFileSystemView(hostAddr,"1098");
                                    fc = new JFileChooser(new File("/opt/myapp"), fs);
                               int status = fc.showDialog(this,"Select File");
                                    if (status == JFileChooser.APPROVE_OPTION) {
                                    File selectedFile = fc.getSelectedFile();
                                    System.out.println("Selected: "
                                    + selectedFile.getParent()
                                    + " --- "
                                    + selectedFile.getAbsolutePath()
                                    + " name "
                                    + selectedFile.getName());
                                    }
                               } catch (MalformedURLException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                               } catch (NotBoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                               }
                          } catch (RemoteException e) {
                               // TODO Auto-generated catch block
                               e.printStackTrace();
                          }
                • 5. Re: Remote Browsing using RMI
                  843793
                  How can this code could work over RMI ? When debugging JFileChooser, the code shows a lot of calls made locally. For example, setCurrentDirectory is calling File.exist() which make a local call
                  • 6. Re: Remote Browsing using RMI
                    843793
                    It seems that JFileChooser worked with a RemoteFileSystemView subclassing FileSystemView for Java 1.3 but in the Java 1.4 time frame, calls to methods in classes other than FileSystemView were introduced. There's a bug report about it.... I wasted a lot of time trying to get this code to work, not to say that someone more skilled than I couldn't get it to work but I finally adopted an approach using a file chooser dialog that we own all the source for.
                    Too bad, JFileChooser with a RemoteFileSystemView was an elegantly beautiful solution, it made me believe Java was great, for the moment that I thought it would work.
                    • 7. Re: Remote Browsing using RMI
                      EJP
                      Hang on, it works perfectly for me in Java 1.5, which is what I wrote it in. But there was some subtlety which I have forgotten about how to initialize it. I think you have to specifically set the current directory on the chooser to a directory that exists on the remote system. It's a while ago and I can't remember the details.
                      • 8. Re: Remote Browsing using RMI
                        843793
                        Setting the initial directory or trying the initialize the FileSystemView does not solve the problems generated by the JFileChooser. Many calls are static (like file.exits(File f) ).

                        I solve the issue by creating an Object similar to JFileChooser, dealing with the remote FileSystemView . It took me 2 hours an work perfectly ...

                        I just found a new bug in the FileSystemView :-)) : if the remote server as an invalid DISPLAY value, an uncatched exception makes the remote FS unusable. For me it's a bug as the RemoteFileSystem does not need to have a DISPLAY neither a Xwindows system at all.

                        Anyway, thanks for your help and comments. I finally have the feature I needed. Your code and comments were very usefull.

                        Thank you