KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > SmbException


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.smb;
20
21 import java.io.IOException JavaDoc;
22 import java.io.StringWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24 import jcifs.util.Hexdump;
25
26 /**
27  * There are hundreds of error codes that may be returned by a CIFS
28  * server. Rather than represent each with it's own <code>Exception</code>
29  * class, this class represents all of them. For many of the popular
30  * error codes, constants and text messages like "The device is not ready"
31  * are provided.
32  * <p>
33  * The jCIFS client maps DOS error codes to NTSTATUS codes. This means that
34  * the user may recieve a different error from a legacy server than that of
35  * a newer varient such as Windows NT and above. If you should encounter
36  * such a case, please report it to jcifs at samba dot org and we will
37  * change the mapping.
38  */

39
40 public class SmbException extends IOException JavaDoc implements NtStatus, DosError, WinError {
41
42     static String JavaDoc getMessageByCode( int errcode ) {
43         if(( errcode & 0xC0000000 ) == 0xC0000000 ) {
44             int min = 0;
45             int max = NT_STATUS_CODES.length;
46
47             while( max >= min ) {
48                 int mid = (min + max) / 2;
49
50                 if( errcode > NT_STATUS_CODES[mid] ) {
51                     min = mid + 1;
52                 } else if( errcode < NT_STATUS_CODES[mid] ) {
53                     max = mid - 1;
54                 } else {
55                     return NT_STATUS_MESSAGES[mid];
56                 }
57             }
58         } else {
59             int min = 0;
60             int max = DOS_ERROR_CODES.length;
61
62             while( max >= min ) {
63                 int mid = (min + max) / 2;
64
65                 if( errcode > DOS_ERROR_CODES[mid][0] ) {
66                     min = mid + 1;
67                 } else if( errcode < DOS_ERROR_CODES[mid][0] ) {
68                     max = mid - 1;
69                 } else {
70                     return DOS_ERROR_MESSAGES[mid];
71                 }
72             }
73         }
74
75         return "0x" + Hexdump.toHexString( errcode, 8 );
76     }
77     static int getStatusByCode( int errcode ) {
78         if(( errcode & 0xC0000000 ) != 0 ) {
79             return errcode;
80         } else {
81             int min = 0;
82             int max = DOS_ERROR_CODES.length;
83
84             while( max >= min ) {
85                 int mid = (min + max) / 2;
86
87                 if( errcode > DOS_ERROR_CODES[mid][0] ) {
88                     min = mid + 1;
89                 } else if( errcode < DOS_ERROR_CODES[mid][0] ) {
90                     max = mid - 1;
91                 } else {
92                     return DOS_ERROR_CODES[mid][1];
93                 }
94             }
95         }
96
97         return NT_STATUS_UNSUCCESSFUL;
98     }
99     static String JavaDoc getMessageByWinerrCode( int errcode ) {
100         int min = 0;
101         int max = WINERR_CODES.length;
102
103         while( max >= min ) {
104             int mid = (min + max) / 2;
105
106             if( errcode > WINERR_CODES[mid] ) {
107                 min = mid + 1;
108             } else if( errcode < WINERR_CODES[mid] ) {
109                 max = mid - 1;
110             } else {
111                 return WINERR_MESSAGES[mid];
112             }
113         }
114
115         return errcode + "";
116     }
117
118
119     private int status;
120     private Throwable JavaDoc rootCause;
121
122     SmbException() {
123     }
124     SmbException( int errcode, Throwable JavaDoc rootCause ) {
125         super( getMessageByCode( errcode ));
126         status = getStatusByCode( errcode );
127         this.rootCause = rootCause;
128     }
129     SmbException( String JavaDoc msg ) {
130         super( msg );
131         status = NT_STATUS_UNSUCCESSFUL;
132     }
133     SmbException( String JavaDoc msg, Throwable JavaDoc rootCause ) {
134         super( msg );
135         this.rootCause = rootCause;
136         status = NT_STATUS_UNSUCCESSFUL;
137     }
138     SmbException( int errcode, boolean winerr ) {
139         super( winerr ? getMessageByWinerrCode( errcode ) : getMessageByCode( errcode ));
140         status = winerr ? errcode : getStatusByCode( errcode );
141     }
142
143     public int getNtStatus() {
144         return status;
145     }
146     public Throwable JavaDoc getRootCause() {
147         return rootCause;
148     }
149     public String JavaDoc toString() {
150         if( rootCause != null ) {
151             StringWriter JavaDoc sw = new StringWriter JavaDoc();
152             PrintWriter JavaDoc pw = new PrintWriter JavaDoc( sw );
153             rootCause.printStackTrace( pw );
154             return super.toString() + "\n" + sw;
155         } else {
156             return super.toString();
157         }
158     }
159 }
160
161
Popular Tags