001: /* jcifs smb client library in Java
002: * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2.1 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package jcifs.smb;
020:
021: import java.io.IOException;
022: import java.io.StringWriter;
023: import java.io.PrintWriter;
024: import jcifs.util.Hexdump;
025:
026: /**
027: * There are hundreds of error codes that may be returned by a CIFS
028: * server. Rather than represent each with it's own <code>Exception</code>
029: * class, this class represents all of them. For many of the popular
030: * error codes, constants and text messages like "The device is not ready"
031: * are provided.
032: * <p>
033: * The jCIFS client maps DOS error codes to NTSTATUS codes. This means that
034: * the user may recieve a different error from a legacy server than that of
035: * a newer varient such as Windows NT and above. If you should encounter
036: * such a case, please report it to jcifs at samba dot org and we will
037: * change the mapping.
038: */
039:
040: public class SmbException extends IOException implements NtStatus,
041: DosError, WinError {
042:
043: static String getMessageByCode(int errcode) {
044: if ((errcode & 0xC0000000) == 0xC0000000) {
045: int min = 0;
046: int max = NT_STATUS_CODES.length;
047:
048: while (max >= min) {
049: int mid = (min + max) / 2;
050:
051: if (errcode > NT_STATUS_CODES[mid]) {
052: min = mid + 1;
053: } else if (errcode < NT_STATUS_CODES[mid]) {
054: max = mid - 1;
055: } else {
056: return NT_STATUS_MESSAGES[mid];
057: }
058: }
059: } else {
060: int min = 0;
061: int max = DOS_ERROR_CODES.length;
062:
063: while (max >= min) {
064: int mid = (min + max) / 2;
065:
066: if (errcode > DOS_ERROR_CODES[mid][0]) {
067: min = mid + 1;
068: } else if (errcode < DOS_ERROR_CODES[mid][0]) {
069: max = mid - 1;
070: } else {
071: return DOS_ERROR_MESSAGES[mid];
072: }
073: }
074: }
075:
076: return "0x" + Hexdump.toHexString(errcode, 8);
077: }
078:
079: static int getStatusByCode(int errcode) {
080: if ((errcode & 0xC0000000) != 0) {
081: return errcode;
082: } else {
083: int min = 0;
084: int max = DOS_ERROR_CODES.length;
085:
086: while (max >= min) {
087: int mid = (min + max) / 2;
088:
089: if (errcode > DOS_ERROR_CODES[mid][0]) {
090: min = mid + 1;
091: } else if (errcode < DOS_ERROR_CODES[mid][0]) {
092: max = mid - 1;
093: } else {
094: return DOS_ERROR_CODES[mid][1];
095: }
096: }
097: }
098:
099: return NT_STATUS_UNSUCCESSFUL;
100: }
101:
102: static String getMessageByWinerrCode(int errcode) {
103: int min = 0;
104: int max = WINERR_CODES.length;
105:
106: while (max >= min) {
107: int mid = (min + max) / 2;
108:
109: if (errcode > WINERR_CODES[mid]) {
110: min = mid + 1;
111: } else if (errcode < WINERR_CODES[mid]) {
112: max = mid - 1;
113: } else {
114: return WINERR_MESSAGES[mid];
115: }
116: }
117:
118: return errcode + "";
119: }
120:
121: private int status;
122: private Throwable rootCause;
123:
124: SmbException() {
125: }
126:
127: SmbException(int errcode, Throwable rootCause) {
128: super (getMessageByCode(errcode));
129: status = getStatusByCode(errcode);
130: this .rootCause = rootCause;
131: }
132:
133: SmbException(String msg) {
134: super (msg);
135: status = NT_STATUS_UNSUCCESSFUL;
136: }
137:
138: SmbException(String msg, Throwable rootCause) {
139: super (msg);
140: this .rootCause = rootCause;
141: status = NT_STATUS_UNSUCCESSFUL;
142: }
143:
144: SmbException(int errcode, boolean winerr) {
145: super (winerr ? getMessageByWinerrCode(errcode)
146: : getMessageByCode(errcode));
147: status = winerr ? errcode : getStatusByCode(errcode);
148: }
149:
150: public int getNtStatus() {
151: return status;
152: }
153:
154: public Throwable getRootCause() {
155: return rootCause;
156: }
157:
158: public String toString() {
159: if (rootCause != null) {
160: StringWriter sw = new StringWriter();
161: PrintWriter pw = new PrintWriter(sw);
162: rootCause.printStackTrace(pw);
163: return super .toString() + "\n" + sw;
164: } else {
165: return super.toString();
166: }
167: }
168: }
|