Hi,
COuld someone explain why i get
org.apache.webdav.lib.WebdavException: Address already in use: connect
When a multithreaded program ( JFIleChooser ) tries to query the same
directory simultaneously ( I am guessing ).
Cheers
Paul.
package it.jrc.compass.webdav;
import java.io.File;
import java.io.IOException;
/*
* WebDAVFileSystemView.java
*
* Created on May 13, 2004, 8:55 AM
*/
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.UIManager;
import javax.swing.filechooser.FileSystemView;
import org.apache.commons.httpclient.HttpURL;
import org.apache.commons.httpclient.HttpsURL;
import org.apache.commons.httpclient.URIException;
import org.apache.webdav.lib.WebdavResource;
/**
*
* @author hussepa
*/
public class WebDAVFileSystemView extends FileSystemView {
/** The WebDAV resource. */
private WebdavResource webdavResource = null;
private HttpURL rootURL = null;
private WebdavFile homedir = null;
private String username = null;
private String password = null;
private String uri = null;
private String rootPath = null;
private static final String newFolderString =
UIManager.getString("FileChooser.other.newFolder");
static FileSystemView fsv = null;
public WebDAVFileSystemView( String uri, String rootPath, String
username, String password ) throws java.lang.IllegalAccessError,
URIException {
try {
this.rootURL = this.uriToHttpURL( uri + rootPath );
this.uri = uri;
this.rootURL.setUserinfo( username, password );
this.username = username;
this.password = password;
this.rootPath = rootPath;
this.connect();
System.out.println( "Connected successfully to : " +
this.rootURL );
this.disconnect( );
// Create home directory object
this.homedir = new WebdavFile( this.rootURL );
System.out.println( "Homedir : " + this.homedir );
}
catch ( java.lang.IllegalAccessError e ) {
System.err.println( e.toString() );
e.printStackTrace( );
throw e;
}
}
private static HttpURL uriToHttpURL(String uri) throws URIException {
HttpURL url = null;
if (uri.startsWith("http://")) {
url = new HttpURL(uri);
}else if (uri.startsWith("https://")) {
url = new HttpsURL(uri);
} else {
throw new URIException("Unknown protocol in URL " + uri);
}
return url;
}
public void disconnect() throws java.lang.UnknownError {
try {
this.webdavResource.close();
}
catch ( Exception e ) {
System.out.println( e.toString() );
throw new UnknownError( );
}
}
public void connect( ) throws java.lang.IllegalAccessError {
try {
this.webdavResource = new WebdavResource( this.rootURL );
}
catch ( Exception e ) {
System.err.println( e.toString() );
throw new IllegalAccessError( );
}
}
public static FileSystemView getFileSystemView() {
try {
if(fsv == null) {
fsv = new WebDAVFileSystemView( "http://127.0.0.1",
"/WebDAV", "", "");
}
return fsv;
}
catch ( Exception e ) {
System.err.println( e.toString() );
return null;
}
}
/**
* Returns a File object constructed in dir from the given filename.
*/
public File createFileObject(File dir, String filename) {
File file = null;
if(dir == null) {
file = new File(filename);
} else {
file = new File(dir, filename);
}
return file;
}
/**
* Returns a File object constructed from the given path string.
*/
public File createFileObject(String path) {
File f = new File(path);
if (isFileSystemRoot(f)) {
f = createFileSystemRoot(f);
}
return f;
}
/**
* Creates a new folder with a default folder name.
*/
public File createNewFolder(File containingDir) throws IOException {
try {
if(containingDir == null) {
throw new IOException("Containing directory is null:");
}
WebdavFile newFolder = null;
HttpURL url = null;
url = this.uriToHttpURL( containingDir.getPath() +
WebdavFile.davSeparator + newFolderString );
// Need to add user info so has access for queries
url.setUserinfo( username, password );
newFolder = new WebdavFile( url );
System.out.println( "new folder : " + newFolder.toString( ) );
this.connect( );
if ( this.webdavResource.mkcolMethod(
newFolder.getAbsolutePath() ) ) {
System.out.println("succeeded.");
return newFolder;
} else {
System.err.println("failed.");
System.err.println(this.webdavResource.getStatusMessage());
throw new IOException(
this.webdavResource.getStatusMessage() );
}
}
catch ( IOException e ) {
throw e;
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}
/**
* Returns all root partitions on this system. For example, on
* Windows, this would be the "Desktop" folder, while on DOS this
* would be the A: through Z: drives.
*/
public File[] getRoots() {
try {
return new WebdavFile[] { this.homedir };
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
}
/**
* Returns true if the file (directory) can be visited.
* Returns false if the directory cannot be traversed.
*
* @param f the <code>File</code>
* @return <code>true</code> if the file/directory can be traversed,
otherwise <code>false</code>
* @see JFileChooser#isTraversable
* @see FileView#isTraversable
*/
public Boolean isTraversable(File f) {
try {
// System.out.println( "isTraversable : " + f.getPath( ) );
// WebdavFile webdavFile = null;
// this.connect();
// webdavFile = ( WebdavFile ) f;
// this.webdavResource.setHttpURL( new HttpURL(
f.getPath( ) ) );
// System.out.println( this.webdavResource.getPath( ) + " :
collection : " + this.webdavResource.isCollection() );
// return Boolean.valueOf( this.webdavResource.isCollection() );
return Boolean.valueOf( f.isDirectory() );
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return Boolean.valueOf( false );
}
finally {
this.disconnect();
}
}
/**
* Name of a file, directory, or folder as it would be displayed in
* a system file browser. Example from Windows: the "M:\" directory
* displays as "CD-ROM (M:)"
*
* The default implementation gets information from the ShellFolder
class.
*
* @param f a <code>File</code> object
* @return the file name as it would be displayed by a native file
chooser
* @see JFileChooser#getName
*/
public String getSystemDisplayName(File f) {
try {
WebdavFile wdf = null;
wdf = ( WebdavFile ) f;
System.out.println( "getSystemDisplayName : getName : "
+ f.getName() );
System.out.println( "getSystemDisplayName : getAbsolutePath : "
+ f.getAbsolutePath() );
System.out.println( "getSystemDisplayName : getCanonicalPath : "
+ f.getCanonicalPath() );
System.out.println( "getSystemDisplayName : getPath : "
+ f.getPath( ) );
return f.getName( );
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}
/**
* Type description for a file, directory, or folder as it would be
displayed in
* a system file browser. Example from Windows: the "Desktop" folder
* is desribed as "Desktop".
*
* Override for platforms with native ShellFolder implementations.
*
* @param f a <code>File</code> object
* @return the file type description as it would be displayed by a
native file chooser
* or null if no native information is available.
* @see JFileChooser#getTypeDescription
*/
public String getSystemTypeDescription(File f) {
return null;
}
/**
* Checks if <code>f</code> represents a real directory or file as
opposed to a
* special folder such as <code>"Desktop"</code>. Used by UI classes to
decide if
* a folder is selectable when doing directory choosing.
*
* @param f a <code>File</code> object
* @return <code>true</code> if <code>f</code> is a real file or
directory.
*/
public boolean isFileSystem(File f) {
return true;
}
/**
* Returns whether a file is hidden or not.
*/
public boolean isHiddenFile(File f) {
return f.isHidden();
}
/**
* Is dir the root of a tree in the file system, such as a drive
* or partition. Example: Returns true for "C:\" on Windows 98.
*
* @param f a <code>File</code> object representing a directory
* @return <code>true</code> if <code>f</code> is a root of a filesystem
* @see #isRoot
*/
public boolean isFileSystemRoot(File dir) {
try {
return ( rootURL.getPath().equals( dir.getPath( ) ) );
}
catch ( Exception e ) {
System.out.println( "isFileSystemRoot" + e.toString( ) );
e.printStackTrace( );
return false;
}
}
/**
* Used by UI classes to decide whether to display a special icon
* for drives or partitions, e.g. a "hard disk" icon.
*
* The default implementation has no way of knowing, so always returns
false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a floppy disk. Implies isDrive(dir).
*
* The default implementation has no way of knowing, so always returns
false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isFloppyDrive(File dir) {
return false;
}
/**
* Used by UI classes to decide whether to display a special icon
* for a computer node, e.g. "My Computer" or a network server.
*
* The default implementation has no way of knowing, so always returns
false.
*
* @param dir a directory
* @return <code>false</code> always
*/
public boolean isComputerNode(File dir) {
return false;
}
// Providing default implementations for the remaining methods
// because most OS file systems will likely be able to use this
// code. If a given OS can't, override these methods in its
// implementation.
public File getHomeDirectory() {
return this.homedir;
}
/**
* Return the user's default starting directory for the file chooser.
*
* @return a <code>File</code> object representing the default
* starting folder
*/
public File getDefaultDirectory() {
return this.homedir;
}
/**
* Gets the list of shown (i.e. not hidden) files.
*/
public File[] getFiles(File dir, boolean useFileHiding) {
try {
String filenames[] = null;
WebdavFile files[] = null;
HttpURL url = null;
String path = null;
this.connect();
// Now we try to list files
path = dir.getPath( );
System.out.println( "getFiles : RAW PATH : '" + path + "'" );
// If path contains the server preamble, we need to extract that
// and have the path only
if ( path.startsWith( "http" ) ) {
System.out.println( "getFiles : preample : " + this.uri );
path = path.replaceAll(this.uri, "" );
}
if ( !path.endsWith("/") ) {
path = path + "/";
}
System.out.println( "getFiles : path : " + path );
this.webdavResource.setPath( path );
filenames = this.webdavResource.list();
files = new WebdavFile[filenames.length];
for ( int i = 0; i < filenames.length; i++ ) {
System.out.println( "file : " + filenames[i] );
// Lets try to construct a uri from the dir
// given and the current file
String filepath = dir.getPath() + filenames[i];
System.out.println( "getFiles : file fullpath : " +
filepath );
url = this.uriToHttpURL( filepath );
// Need to add user info so has access for queries
url.setUserinfo( username, password );
files[i] = new WebdavFile( url );
}
return files;
}
catch ( Exception e ) {
System.err.println( e.toString() );
e.printStackTrace( );
return null;
}
finally {
this.disconnect();
}
}
/**
* Returns the parent directory of <code>dir</code>.
* @param dir the <code>File</code> being queried
* @return the parent directory of <code>dir</code>, or
* <code>null</code> if <code>dir</code> is <code>null</code>
*/
public File getParentDirectory(File dir) {
System.out.println( "dir : " + dir );
if ( dir == null ) {
return null;
}
else if ( dir.equals(this.homedir) ) {
return this.homedir;
}
else {
System.out.println( "getParentFile : " + dir.getParentFile( ) );
return dir.getParentFile();
}
}
/**
* Creates a new <code>File</code> object for <code>f</code> with
correct
* behavior for a file system root directory.
*
* @param f a <code>File</code> object representing a file system root
* directory, for example "/" on Unix or "C:\" on Windows.
* @return a new <code>File</code> object
*/
protected File createFileSystemRoot(File f) {
return new FileSystemRoot(f);
}
static class WebdavFile extends org.apache.webdav.lib.WebdavFile {
public WebdavFile( HttpURL httpUrl ) throws URIException {
super( httpUrl );
}
public String getName() {
String name = null;
// Get the base name
name = super.getName( );
// If is a directory, we need to add a trailing slash
if ( this.isDirectory() ) {
name = name + "/";
}
return name;
}
public boolean isDirectory ( )
{
System.out.println ( "isDirectory : " + this.getPath() );
return super.isDirectory();
}
// public String getPath ( )
// {
// String path = null;
//
// // Get the base name
// path = super.getName( );
//
// // If is a directory, we need to add a trailing slash
// if ( this.isDirectory() ) {
// path = path + "/";
// }
// System.out.println ( "WebdavFile getPath : " + path );
// return path;
// }
}
static class FileSystemRoot extends File {
public FileSystemRoot(File f) {
super(f,"");
}
public FileSystemRoot(String s) {
super(s);
}
public boolean isDirectory() {
return true;
}
public String getName() {
return getPath();
}
}
// Test code
public static void main( String args[] ) throws Exception {
JFrame frame = null;
// Setup
JFileChooser fc = new JFileChooser(new WebDAVFileSystemView(
"http://139.191.67.52:8080", "/slide/files", "root", "" ) );
frame = new JFrame( );
fc.showOpenDialog(frame);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]