KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > coi > tools > os > win > NativeLibException


1 /*
2  * IzPack - Copyright 2001-2007 Julien Ponge, All Rights Reserved.
3  *
4  * http://www.izforge.com/izpack/
5  * http://developer.berlios.de/projects/izpack/
6  *
7  * Copyright 2005 Klaus Bartz
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */

21
22 package com.coi.tools.os.win;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.MissingResourceException JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30 /**
31  * A exception class which will be used from the native part of system dependent classes to signal
32  * exceptions. The native methods writes only symbolic error messages, the language dependant
33  * mapping will be done in this class.
34  *
35  * @author Klaus Bartz
36  *
37  */

38 public class NativeLibException extends Exception JavaDoc
39 {
40
41     private static final long serialVersionUID = 3257002172494721080L;
42
43     /** Map of founded resource bundles which contains the localized error messages. */
44     private final static HashMap JavaDoc messageResourceBundles = new HashMap JavaDoc();
45
46     /** Internal error as number. */
47     private int libErr;
48
49     /** OS error as number. */
50     private int osErr;
51
52     /** Internal error message; contains most the symbolic error name. */
53     private String JavaDoc libErrString;
54
55     /** OS error string; if possible localized. */
56     private String JavaDoc osErrString;
57
58     /** Additional arguments. */
59     private ArrayList JavaDoc args = new ArrayList JavaDoc();
60
61     static
62     {
63         // add the first resource bundle
64
addResourceBundle("com.coi.tools.os.win.resources.NativeLibErr");
65     }
66
67     /**
68      * Adds a resource bundle which contains localized error messages. The bundlePath should contain
69      * a string with which the bundle is loadable with ResourceBundle.getBundle, may be the full
70      * class path to a ListResourceBundle. The localize is done by getBundle, therefore the path
71      * should not contain the locale substring. At a call to getMessage the bundle is searched with
72      * the libErrString as key. If it exist, the value of it is used by getMessage, else the
73      * libErrString self.
74      *
75      * @param bundlePath path of bundle without locale
76      */

77     public static void addResourceBundle(String JavaDoc bundlePath)
78     {
79         ResourceBundle JavaDoc bd = null;
80         if (messageResourceBundles.containsKey(bundlePath)) return;
81         try
82         {
83             bd = ResourceBundle.getBundle(bundlePath);
84         }
85         catch (MissingResourceException JavaDoc mre)
86         {
87             mre.printStackTrace();
88         }
89         messageResourceBundles.put(bundlePath, bd);
90
91     }
92
93     /**
94      * The constructor.
95      */

96     public NativeLibException()
97     {
98         super();
99     }
100
101     /**
102      * Creates a NativeLibException with the given message.
103      *
104      * @param message to be used
105      */

106     public NativeLibException(String JavaDoc message)
107     {
108         super(message);
109     }
110
111     /**
112      * Creates a NativeLibException with the given cause.
113      *
114      * @param cause to be used
115      */

116     public NativeLibException(Throwable JavaDoc cause)
117     {
118         super(cause);
119     }
120
121     /**
122      * Creates a NativeLibException with the given message and cause.
123      *
124      * @param message message to be used
125      * @param cause cause to be used
126      */

127     public NativeLibException(String JavaDoc message, Throwable JavaDoc cause)
128     {
129         super(message, cause);
130     }
131
132     /**
133      * Creates a NativeLibException with the given values.
134      *
135      * @param libErr identifier of the internal handled error
136      * @param osErr system error number
137      * @param libString message for the internal handled error
138      * @param osString system error message
139      */

140     public NativeLibException(int libErr, int osErr, String JavaDoc libString, String JavaDoc osString)
141     {
142         super();
143         this.libErr = libErr;
144         this.osErr = osErr;
145         libErrString = libString;
146         osErrString = osString;
147     }
148
149     /*
150      * (non-Javadoc)
151      *
152      * @see java.lang.Throwable#getMessage()
153      */

154     public String JavaDoc getMessage()
155     {
156         StringBuffer JavaDoc retval = new StringBuffer JavaDoc();
157         boolean next = false;
158         if (libErrString != null)
159         {
160             retval.append(getLocalizedLibMessage());
161             next = true;
162         }
163         else if (libErr != 0)
164         {
165             if (next) retval.append("\n");
166             next = true;
167             retval.append(getMsg("libErrNumber." + Integer.toString(libErr)));
168         }
169         if (osErr != 0)
170         {
171             if (next) retval.append("\n");
172             next = true;
173             retval.append(getMsg("libInternal.OsErrNumPraefix")).append(Integer.toString(osErr));
174         }
175         if (osErrString != null)
176         {
177             if (next) retval.append("\n");
178             next = true;
179             // Message self should be localized in the native part
180
retval.append(getMsg("libInternal.OsErrStringPraefix")).append(getOsMessage());
181         }
182         if (retval.length() > 0) return (reviseMsgWithArgs(retval.toString()));
183         return null;
184     }
185
186     /**
187      * Returns the number of the internal handled error.
188      *
189      * @return the number of the internal handled error
190      */

191     public int getLibErr()
192     {
193         return libErr;
194     }
195
196     /**
197      * Returns the message of the internal handled error.
198      *
199      * @return the messager of the internal handled error
200      */

201     public String JavaDoc getLibMessage()
202     {
203         return libErrString;
204     }
205
206     /**
207      * Returns the localized message of the internal handled error.
208      *
209      * @return the localized message of the internal handled error
210      */

211     public String JavaDoc getLocalizedLibMessage()
212     {
213         return (getMsg(libErrString));
214     }
215
216     /**
217      * Returns the number of the system error.
218      *
219      * @return the number of the system error
220      */

221     public int getOsErr()
222     {
223         return (osErr);
224     }
225
226     /**
227      * Returns the message of the system error.
228      *
229      * @return the messager of the system error
230      */

231     public String JavaDoc getOsMessage()
232     {
233         return (osErrString);
234     }
235
236     /**
237      * Adds a string to the internal argument list.
238      *
239      * @param arg string to be added to the internal argument list
240      */

241     public void addArgument(String JavaDoc arg)
242     {
243         args.add(arg);
244     }
245
246     /**
247      * Returns the internal argument list.
248      *
249      * @return the internal argument list
250      */

251     public ArrayList JavaDoc getArguments()
252     {
253         return (args);
254     }
255
256     /**
257      * Revise placeholder in the given message with the setted arguments
258      * @param msg message to be revised
259      * @return revised message
260      */

261     public String JavaDoc reviseMsgWithArgs(String JavaDoc msg)
262     {
263         for (int i = 0; i < args.size(); ++i)
264         {
265             String JavaDoc key = "{" + Integer.toString(i) + "}";
266             msg = replaceString(msg, key, (String JavaDoc) args.get(i));
267         }
268         return (msg);
269     }
270
271     /**
272      * Searches the resource bundles for a string which coresponds to the given string as key.
273      *
274      * @param s string which should be used as keys for the resource bundle
275      * @return the founded message as int value
276      */

277
278     private String JavaDoc getMsg(String JavaDoc s)
279     {
280         Iterator JavaDoc it = messageResourceBundles.values().iterator();
281         while (it.hasNext())
282         {
283             try
284             {
285                 return (((ResourceBundle JavaDoc) it.next()).getString(s));
286             }
287             catch (MissingResourceException JavaDoc missingresourceexception)
288             { // do not throw, else look in next bundle.
289
;
290             }
291         }
292         return (s);
293     }
294
295     /**
296      * Returns a string resulting from replacing all occurrences of what in this string with with.
297      * In opposite to the String.replaceAll method this method do not use regular expression or
298      * other methods which are only available in JRE 1.4 and later.
299      *
300      * @param destination string for which the replacing should be performed
301      * @param what what string should be replaced
302      * @param with with what string what should be replaced
303      * @return a new String object if what was found in the given string, else the given string self
304      */

305     private static String JavaDoc replaceString(String JavaDoc destination, String JavaDoc what, String JavaDoc with)
306     {
307         if (destination.indexOf(what) >= 0)
308         { // what found, with (placeholder) not included in destination ->
309
// perform changing.
310
StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
311             int last = 0;
312             int current = destination.indexOf(what);
313             int whatLength = what.length();
314             while (current >= 0)
315             { // Do not use Methods from JRE 1.4 and higher ...
316
if (current > 0) buf.append(destination.substring(last, current));
317                 buf.append(with);
318                 last = current + whatLength;
319                 current = destination.indexOf(what, last);
320             }
321             if (destination.length() > last) buf.append(destination.substring(last));
322             return buf.toString();
323         }
324         return destination;
325     }
326
327 }
328
Popular Tags