KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > util > io > NativeResults


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * NativeResults.java
26  *
27  * Created on April 14, 2003, 11:46 AM
28  */

29
30 package com.sun.enterprise.util.io;
31 import java.util.*;
32 import java.util.logging.*;
33 import com.sun.enterprise.util.OS;
34
35 /**
36  * Convert an integer error number returned from native code to a localized String
37  * As of April, 2003 -- UNIX is the only supported OS.
38  * @author bnevins
39  */

40
41 class NativeResults
42 {
43     NativeResults(int id)
44     {
45         this.id = id;
46         initStrings();
47         setResultString();
48         setResultException();
49     }
50     
51     /**
52      * @return The localized String explanation of the error, or success.
53      */

54     String JavaDoc getResultString()
55     {
56         return resultString;
57     }
58     
59     /**
60      * @return the NativeIOException -- suitable for throwing
61      */

62     NativeIOException getResultException()
63     {
64         return exception; // may be null!
65
}
66     
67     ///////////////////////////////////////////////////////////////////////////
68

69     private void setResultString()
70     {
71         if(rb == null)
72         {
73             resultString = genericError + id;
74             return;
75         }
76         
77         try
78         {
79             resultString = rb.getString(errorKey + id);
80             return;
81         }
82         catch(Throwable JavaDoc t)
83         {
84             // go into the next try block. No need to nest it.
85
}
86
87         try
88         {
89             // note: the id is outside the parenthesis -- not a mistake...
90
resultString = rb.getString(unknownErrorKey) + id;
91             return;
92         }
93         catch(Throwable JavaDoc t)
94         {
95             resultString = genericError + id;
96         }
97     }
98     
99     ///////////////////////////////////////////////////////////////////////////
100

101     void setResultException()
102     {
103         if(id != 0) // by definition -- an error!
104
exception = new NativeIOException(resultString, id);
105     }
106     
107     ///////////////////////////////////////////////////////////////////////////
108

109     private void initStrings()
110     {
111         try
112         {
113             rb = ResourceBundle.getBundle(RESOURCE_BUNDLE);
114         }
115         catch(Throwable JavaDoc t)
116         {
117             // any little error --> RuntimeException!!
118
rb = null;
119             logger.severe("Unable to get a ResourceBundle: " + RESOURCE_BUNDLE);
120         }
121         
122         if(OS.isWindows())
123             osString = "Windows";
124         else
125             osString = "UNIX";
126         
127         errorKey = "enterprise_util." + osString + ".error.";
128         unknownErrorKey = errorKey + "unknown";
129         genericError = "UNKNOWN " + osString + " Error returned. Errno =";
130     }
131     
132     ///////////////////////////////////////////////////////////////////////////
133

134     private Logger logger = IOLogger.getLogger();
135     private ResourceBundle rb = null;
136     private static final String JavaDoc RESOURCE_BUNDLE = "com.sun.logging.enterprise.system.util.LogStrings";
137     private String JavaDoc errorKey; // depends on OS
138
private String JavaDoc unknownErrorKey; // depends on OS
139
private String JavaDoc genericError; // depends on OS
140
private String JavaDoc osString; // depends on OS
141
private int id; // returned by native code
142
private String JavaDoc resultString;
143     private NativeIOException exception = null;
144     
145     //////////////////////////////////////////////////////////////////////////
146

147     public static void main(String JavaDoc[] args)
148     {
149         int errs[] = { 0, 1, 2, 4, 5, 13, 14, 20, 22, 30, 67, 78, 9999, 111, -2};
150
151         for(int i = 0; i < errs.length; i++)
152         {
153             int id = errs[i];
154             
155             NativeResults nr = new NativeResults(id);
156             String JavaDoc s = "ID: " + id + ", ";
157             
158             if(nr.getResultException() == null)
159                 s += "NO ERROR, ";
160             else
161                 s += "YES ERROR, ";
162             
163             s += nr.getResultString();
164             
165             System.out.println(s);
166         }
167     }
168 }
169
170
Popular Tags