KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcckit > util > FactoryException


1 /*
2  * Copyright 2003-2004, Franz-Josef Elmer, All rights reserved
3  *
4  * This library is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published by
6  * the Free Software Foundation; either version 2.1 of the License, or
7  * (at your option) any later version.
8  *
9  * This program 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
12  * GNU Lesser General Public License for more details
13  * (http://www.gnu.org/copyleft/lesser.html).
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package jcckit.util;
20
21 import java.lang.reflect.*;
22
23 /**
24  * Exception thrown in the case of an error during creation of a new
25  * object by {@link Factory#create}.
26  *
27  * @author Franz-Josef Elmer
28  */

29 public class FactoryException extends RuntimeException JavaDoc {
30   private final String JavaDoc _fullKey;
31   private final String JavaDoc _className;
32   private final Object JavaDoc _reason;
33
34   /**
35    * Creates a new instance based on the specified configuration parameters
36    * and reason object.
37    * <p>
38    * If <tt>reason</tt> is an instance of <tt>InvocationTargetException</tt>
39    * it will be replaced by the wrapped <tt>Throwable</tt>.
40    * @param configParameters Configuration parameters from which the
41    * <tt>className</tt> will be extracted (if it exists, otherwise
42    * <tt>null</tt> will be taken).
43    * @param reason The reason causing this exception. Most often an
44    * an exception.
45    */

46   public FactoryException(ConfigParameters configParameters, String JavaDoc key,
47                           Object JavaDoc reason) {
48     _fullKey = configParameters.getFullKey(key);
49     _className = configParameters.get(key, null);
50     if (reason instanceof InvocationTargetException) {
51       reason = ((InvocationTargetException) reason).getTargetException();
52     }
53     _reason = reason;
54   }
55
56   /** Returns the full class name key. */
57   public String JavaDoc getFullKey() {
58     return _fullKey;
59   }
60
61   /** Returns the fully qualified class name. */
62   public String JavaDoc getClassName() {
63     return _className;
64   }
65
66   /** Returns the reason object causing this exception. */
67   public Object JavaDoc getReason() {
68     return _reason;
69   }
70
71   /**
72    * Renders this instance as follows: <tt>jcckit.util.FactoryException:
73    * <i>full key</i> = <i>class name</i>: <i>reason</i></tt>.
74    */

75   public String JavaDoc toString() {
76     return getClass().getName() + ": " + _fullKey + " = " + _className
77            + ": " + _reason;
78   }
79 }
80
Popular Tags