[jcifs] SmbException Supercedes boolean?
Allen, Michael B (RSCH)
Michael_B_Allen at ml.com
Tue Oct 30 11:29:33 EST 2001
> -----Original Message-----
> From: Rob Wygand [SMTP:rob at wygand.com]
...
> just a boolean return value doesn't do it. Exceptions do. I've talked
> before about having some other mechanism (such as an some sort of error
> handler class), but Mike seemed set on Exceptions.
>
There will be no "Error Handler" but there will be an AuthHandler. To clarify
how the new api will change. Some SmbFile methods will throw the now
public SmbException. In addition, an AuthHandler interface has been (is
being) added. Code might take advantage of this like:
public class MyApp implements AuthHandler {
public boolean authenticate( AuthInfo ai ) {
System.out.println( "Authentication for " + ai.domain + "\\" +
ai.username + " at " + ai.url + " failed" );
System.out.print( "Username: " );
ai.username = in.readLine();
System.out.print( "Password: " );
ai.password = in.readLine();
return ai.username.length() > 0;
}
long doSomthing() {
while( true ) {
try {
return file.length();
} catch( SmbException se ) {
if( se.errorClass == SmbException.ERRbadshare ) {
Thread.sleep( 10 * 60 * 1000 );
} else {
System.out.println( se.getMessage() );
This demonstrates several important things. One is that all authentication
releated exceptions are caught internally and passed in AuthInfo to the
AuthHandler implementation (in this case it's MyApp). AuthInfo has the
domain, username, password, target resource, and possibly the
SmbException. The other is that you can catch and interrogate the
SmbExceptions. There are literrally several hundred possible error codes
(not all of which have String messages) so this mechanism will allow users
to act on this feedback. In the example above this would probably print
something like:
$ java -Djcifs.properties=miallen.prp MyApp smb://server/tapemnt/foo.txt
Authentication for nyrsch\yoyo at smb://server/tapemnt/foo.txt failed.
Username: miallen
Password: hello
The device is not ready.
Which returns the size of a file unless it's being accessed by another
process in which case it will sleep for 10 minutes and try again. In the output
above the SmbException.ERRnotready was not caught; you know this as
the message you get when you try to access your cdrom with out a disk in it
right?
Of course if you don't care about Exceptions this code might look like:
long doSomething() {
try {
return file.length();
} catch( Exception x ) {
}
return 0L;
}
Mike
More information about the jcifs
mailing list