001/*
002 * IzPack - Copyright 2001-2005 Julien Ponge, All Rights Reserved.
003 * 
004 * http://www.izforge.com/izpack/
005 * http://developer.berlios.de/projects/izpack/
006 * 
007 * Copyright 2005 Klaus Bartz
008 * 
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *     http://www.apache.org/licenses/LICENSE-2.0
014 *     
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 */
021
022package com.izforge.izpack.util.os;
023
024import com.coi.tools.os.win.NativeLibException;
025import com.izforge.izpack.LocaleDatabase;
026
027/**
028 * This class allows it to define error messages for <code>NativeLibException</code> s in the
029 * IzPack locale files. The getMessage methode searches in the current langpack for entries which
030 * are corresponding to that one which are received from native part. If the langpack do not contain
031 * the entry, the resource boundle is used.
032 * 
033 * @author Klaus Bartz
034 * 
035 */
036public class WrappedNativeLibException extends Exception
037{
038
039    private static final long serialVersionUID = 3257562893309720112L;
040
041    /** The packs locale database. */
042    protected static LocaleDatabase langpack = null;
043
044    /**
045     * Default constructor.
046     */
047    public WrappedNativeLibException()
048    {
049        super();
050    }
051
052    /**
053     * @param message
054     */
055    public WrappedNativeLibException(String message)
056    {
057        super(message);
058    }
059
060    /**
061     * @param cause
062     */
063    public WrappedNativeLibException(Throwable cause)
064    {
065        super(cause);
066    }
067
068    /**
069     * @param message
070     * @param cause
071     */
072    public WrappedNativeLibException(String message, Throwable cause)
073    {
074        super(message, cause);
075    }
076
077    /*
078     * (non-Javadoc)
079     * 
080     * @see java.lang.Throwable#getMessage()
081     */
082    public String getMessage()
083    {
084        StringBuffer retval = new StringBuffer();
085        boolean next = false;
086        boolean ok = false;
087        if (getCause() instanceof NativeLibException)
088        {
089            NativeLibException nle = (NativeLibException) getCause();
090            if (langpack != null)
091            {
092                while (true)
093                {
094                    if (nle.getLibMessage() != null)
095                    {
096                        String val = (String) langpack.get("NativeLibException."
097                                + nle.getLibMessage());
098                        if (val == null) break;
099                        retval.append(val);
100                        next = true;
101                    }
102                    else if (nle.getLibErr() != 0)
103                    {
104                        String val = (String) langpack.get("NativeLibException.libErrNumber."
105                                + Integer.toString(nle.getLibErr()));
106                        if (val == null) break;
107                        if (next) retval.append("\n");
108                        next = true;
109                        retval.append(val);
110                    }
111                    if (nle.getOsErr() != 0)
112                    {
113                        String val = (String) langpack
114                                .get("NativeLibException.libInternal.OsErrNumPraefix")
115                                + Integer.toString(nle.getOsErr());
116                        if (val == null) break;
117                        if (next) retval.append("\n");
118                        next = true;
119                        retval.append(val);
120                    }
121                    if (nle.getOsMessage() != null)
122                    {
123                        String val = (String) langpack
124                                .get("NativeLibException.libInternal.OsErrStringPraefix")
125                                + nle.getOsMessage();
126                        if (val == null) break;
127                        if (next) retval.append("\n");
128                        next = true;
129                        retval.append(val);
130                    }
131                    ok = true;
132                    break;
133                }
134            }
135            if (ok && retval.length() > 0)
136                return (nle.reviseMsgWithArgs(retval.toString()));
137            else
138                return (nle.getMessage());
139
140        }
141        else
142            return (super.getMessage());
143    }
144
145    /**
146     * Returns the langpack.
147     * 
148     * @return Returns the langpack.
149     */
150    public static LocaleDatabase getLangpack()
151    {
152        return langpack;
153    }
154
155    /**
156     * Sets the langpack to the given locale database.
157     * 
158     * @param langpack the langpack to set.
159     */
160    public static void setLangpack(LocaleDatabase langpack)
161    {
162        WrappedNativeLibException.langpack = langpack;
163    }
164}