[jcifs] WebNFS and JCfis

Sal Scotto salscotto at telocity.com
Fri Apr 12 22:21:56 EST 2002


Here is a quick and dirty hack I did to accomidate jCIFS (0.63b) within
WebNFS
A Stipulation is setting up the system properties.
Otherwise it works.. It atleast gets you interpolating with CIFS and NFS at
the same time

Sal


// Test Harness
package org.sscotto.smb;

import com.sun.xfile.*;
import java.io.*;
import java.net.*;

/**
 * @author Salvatore E. ScottoDiLuzio
 * @version 1.0
 */

public class TestSamba
{
  public TestSamba()
  {
  }

  public static void main(String[] args)throws IOException
  {
    TestSamba testSamba1 = new TestSamba();
    testSamba1.runTest();
  }

  public void runTest()throws IOException
  {
    System.setProperty("java.protocol.xfile","com.pathfire");
    System.setProperty("jcifs.smb.client.domain","<YOUR DOMAIN>");
    System.setProperty("jcifs.smb.client.username","<YOUR USER ID>");
    System.setProperty("jcifs.smb.client.password","<YOUR PASSWORD>");
    System.setProperty("jcifs.netbios.wins","<WINS IP ADDRESS>");
    XFile xf = new XFile("smb://Omega/smbTest/test.txt");
    XFile xfDir = new XFile("smb://Omega/smbTest");
    String list[] = xfDir.list();
    for(int i=0;i<list.length;i++)
    {
      System.out.println("Found:"+list[i]);
    }
    XFileInputStream xfin = new XFileInputStream(xf);
    byte dt[] = new byte[256];
    int read = 0;
    while( (read = xfin.read(dt)) != -1)
    {
      System.out.println(new String(dt,0,read));
    }
    xfin.close();
    XRandomAccessFile raf = new XRandomAccessFile(xf,"rw");
    long lgth = raf.length();
    System.out.println("Length:"+lgth);
    raf.close();

    XRandomAccessFile raf2 = new
XRandomAccessFile("smb://Omega/smbTest/test.txt","rw");
    raf2.writeUTF("This is a JCFIS Test");
    raf2.close();
  }
}

// The XFileAccessor

package org.scotto.smb;

import com.sun.xfile.*;
import jcifs.*;
import jcifs.smb.*;
import jcifs.util.*;
import jcifs.netbios.*;
import java.io.*;
import java.net.*;

/**
 * @author Salvatore E. ScottoDiLuzio
 * @version 1.0
 */

public class XFileAccessor implements com.sun.xfile.XFileAccessor
{
  SmbFile samba = null;
  SmbFileInputStream smbIn = null;
  SmbFileOutputStream smbOut = null;
  XFile xf = null;
  boolean serialAccess = false;
  boolean readOnly = false;

  public XFileAccessor()
  {
  }
  public boolean canRead()
  {
    try
    {
      return samba.canRead();
    }
    catch(SmbException e)
    {
      return false;
    }
  }
  public boolean canWrite()
  {
    try
    {
      return samba.canWrite();
    }
    catch(SmbException e)
    {
      return false;
    }
  }
  public void close() throws java.io.IOException
  {
    if(smbIn != null)
    {
      try
      {
        smbIn.close();
        smbIn = null;
      }
      catch(SmbException ce)
      {
        throw new IOException(ce.toString());
      }
    }
    if(smbOut != null)
    {
      try
      {
        smbOut.close();
        smbOut = null;
      }
      catch(SmbException oe)
      {
        throw new IOException(oe.toString());
      }
    }
  }
  public boolean delete()
  {
    try
    {
      samba.delete();
      return true;
    }
    catch(SmbException delError)
    {
      return false;
    }
  }
  public boolean exists()
  {
    try
    {
      return samba.exists();
    }
    catch(SmbException e)
    {
      return false;
    }
  }
  public void flush() throws java.io.IOException
  {
    if(smbOut != null)
    {
      try
      {
        smbOut.flush();
      }
      catch(SmbException fe)
      {
        throw new IOException(fe.toString());
      }
    }
  }
  public XFile getXFile()
  {
    return xf;
  }
  public boolean isDirectory()
  {
    try
    {
      return samba.isDirectory();
    }
    catch(SmbException e)
    {
      return false;
    }
  }
  public boolean isFile()
  {
    try
    {
      return samba.isFile();
    }
    catch(SmbException e)
    {
      return false;
    }
  }
  public long lastModified()
  {
    try
    {
      return samba.lastModified();
    }
    catch(SmbException e)
    {
      return 0L;
    }
  }
  public long length()
  {
    try
    {
      return samba.length();
    }
    catch(SmbException e)
    {
      return 0L;
    }
  }
  public String[] list()
  {
    try
    {
      return samba.list();
    }
    catch(SmbException e)
    {
      return null;
    }
  }
  public boolean mkdir()
  {
    try
    {
      samba.mkdir();
      return true;
    }
    catch(SmbException mkError)
    {
      return false;
    }
  }
  public boolean mkfile()
  {
    try
    {
      SmbFileOutputStream out = new SmbFileOutputStream(samba,true);
      out.close();
      return true;
    }
    catch(UnknownHostException uhe)
    {
      return false;
    }
    catch(MalformedURLException me)
    {
      return false;
    }
    catch(SmbException createError)
    {
      return false;
    }
    catch(IOException ioError)
    {
      return false;
    }
  }
  public boolean open(XFile file, boolean serial, boolean ro)
  {
    xf = file;
    serialAccess = serial;
    readOnly = ro;
    try
    {
      samba = new SmbFile(xf.getCanonicalPath());
      return true;
    }
    catch(SmbException openError)
    {
      return false;
    }
    catch(UnknownHostException eu)
    {
      return false;
    }
    catch(MalformedURLException eu)
    {
      return false;
    }
    catch(IOException ioE)
    {
      return false;
    }
  }
  public int read(byte[] parm1, int parm2, int parm3, long parm4) throws
java.io.IOException
  {
    try
    {
      if(smbIn == null)
        smbIn = new SmbFileInputStream(samba);
      return smbIn.read(parm1,parm2,parm3);
    }
    catch(SmbException readError)
    {
      throw new IOException(readError.toString());
    }
  }
  public boolean renameTo(XFile parm1)
  {
    try
    {
      samba.renameTo(new SmbFile(parm1.getCanonicalPath()));
      return true;
    }
    catch(SmbException renameError)
    {
      return false;
    }
    catch(UnknownHostException eu)
    {
      return false;
    }
    catch(MalformedURLException eu)
    {
      return false;
    }
    catch(IOException ioE)
    {
      return false;
    }
  }
  public void write(byte[] parm1, int parm2, int parm3, long parm4) throws
java.io.IOException
  {
    if(readOnly)
      throw new IOException("The File is READ-ONLY");
    else
    {
      try
      {
        if(smbOut == null)
          smbOut = new SmbFileOutputStream(samba);
        smbOut.write(parm1,parm2,parm3);
      }
      catch(SmbException writeError)
      {
        throw new IOException(writeError.toString());
      }
    }
  }
}





More information about the jcifs mailing list