[jcifs] Problem with DFS - including attachement :-)

Eric eglass1 at comcast.net
Thu Apr 15 00:38:45 GMT 2004


>>BTW: For my own purposes I would like to create an abstract file class.
>>This should allow to use a common class "AbstractFile" (with 2
>>subclasses) to access both local ( file:// ) and SMB ( smb:// ) files.
>>It should have methods like:
> 
> <snip>
> 
>>Is there already something out like that and I missed it ?
> 
> 
> Nope.
> 
> 
>>Or might this be a useful addition for the jcifs/util, in which case I
>>could send the result to you ?
> 
> 
> Sure. Post it to the list so people can experiment.
> 
> Mike
> 
> 

See attached.  I'm not really sure this would "belong" in jCIFS (as it's 
really independent of any underlying implementation).  This uses the JDK 
1.3 Proxy class to create a dynamic implementation of the AbstractFile 
interface, which calls through to *any* underlying object.  So the 
backing implementation doesn't have to implement any interface or 
inherit from any specific superclass, it just has to have a method with 
the same name and signature (discovered at runtime). 
AbstractFileFactory has some boilerplate to create instances from 
SmbFiles, Files, or string paths.  Usage would be like:


import jcifs.util.*;
import java.io.*;

public class test {

     public static void main(String[] args) throws Exception {
         AbstractFile file;
         file = AbstractFileFactory.createFile("/file/path/");
         String[] list = file.list();
         for (int i = list.length - 1; i >= 0; i--) {
             System.out.println(list[i]);
         }
         file = AbstractFileFactory.createFile("smb://server/share/");
         list = file.list();
         for (int i = list.length - 1; i >= 0; i--) {
             System.out.println(list[i]);
         }
     }

}





-------------- next part --------------
package jcifs.util;

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;

public interface AbstractFile {

    public String getName();

    public String getParent();

    public AbstractFile getParentFile();

    public String getPath();

    public boolean isAbsolute();

    public String getAbsolutePath();

    public AbstractFile getAbsoluteFile();

    public String getCanonicalPath() throws IOException;

    public AbstractFile getCanonicalFile() throws IOException;

    public URL toURL() throws MalformedURLException;

    public boolean canRead();

    public boolean canWrite();

    public boolean exists();

    public boolean isDirectory();

    public boolean isFile();

    public boolean isHidden();

    public long lastModified();

    public long length();

    public boolean createNewFile() throws IOException;

    public boolean delete();

    public void deleteOnExit();

    public String[] list();

    public AbstractFile[] listFiles();

    public boolean mkdir();

    public boolean mkdirs();

    public boolean setLastModified(long modified);

    public boolean setReadOnly();

}
-------------- next part --------------
package jcifs.util;

import java.io.File;
import java.io.IOException;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import jcifs.smb.SmbFile;

public class AbstractFileFactory {

    public static AbstractFile createFile(String path) throws IOException {
        return path.startsWith("smb:") ? createFile(new SmbFile(path)) :
                createFile(new File(path));
    }

    public static AbstractFile createFile(File file) {
        return createFile((Object) file);
    }

    public static AbstractFile createFile(SmbFile file) {
        return createFile((Object) file);
    }

    private static AbstractFile createFile(Object impl) {
        return (AbstractFile) Proxy.newProxyInstance(
                AbstractFile.class.getClassLoader(),
                        new Class[] { AbstractFile.class },
                                new AbstractFileHandler(impl));
    }

    private static class AbstractFileHandler implements InvocationHandler {

        private final Object impl;

        public AbstractFileHandler(Object impl) {
            this.impl = impl;
        }

        public Object invoke(Object proxy, Method method, Object[] args)
                throws Throwable {
            String methodName = method.getName();
            if (methodName.equals("getParentFile")) {
                return createFile(impl.getClass().getMethod(methodName,
                        method.getParameterTypes()).invoke(impl, args));
            } else if (methodName.equals("getAbsoluteFile")) {
                return createFile(impl.getClass().getMethod(methodName,
                        method.getParameterTypes()).invoke(impl, args));
            } else if (methodName.equals("getCanonicalFile")) {
                return createFile(impl.getClass().getMethod(methodName,
                        method.getParameterTypes()).invoke(impl, args));
            } else if (methodName.equals("listFiles")) {
                Object[] files = (Object[])
                        impl.getClass().getMethod(methodName,
                                method.getParameterTypes()).invoke(impl, args);
                AbstractFile[] iFiles = new AbstractFile[files.length];
                for (int i = iFiles.length - 1; i >= 0; i--) {
                    iFiles[i] = createFile(files[i]);
                }
                return iFiles;
            } else {
                return impl.getClass().getMethod(methodName,
                        method.getParameterTypes()).invoke(impl, args);
            }
        }

    }

}


More information about the jcifs mailing list