KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > components > framework > ComponentException


1 /*******************************************************************************
2  * Copyright (c) 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.components.framework;
12
13 import org.eclipse.core.runtime.IStatus;
14 import org.eclipse.osgi.util.NLS;
15 import org.eclipse.ui.internal.WorkbenchPlugin;
16 import org.eclipse.ui.internal.components.ComponentMessages;
17 import org.eclipse.ui.internal.components.ComponentUtil;
18 import org.eclipse.ui.internal.misc.StatusUtil;
19
20 /**
21  * Exception thrown when unable to construct a component.
22  *
23  * Not intended to be subclassed by clients
24  *
25  * <p>EXPERIMENTAL: The components framework is currently under active development. All
26  * aspects of this class including its existence, name, and public interface are likely
27  * to change during the development of Eclipse 3.1</p>
28  *
29  * @since 3.1
30  */

31 public class ComponentException extends Exception JavaDoc {
32     
33     private static final long serialVersionUID = 6009335074727417445L;
34     
35     private String JavaDoc componentName;
36     
37     private Throwable JavaDoc cause;
38     
39     /**
40      * Creates an exception indicating failure to create a particular component.
41      *
42      * @param componentKey identifies the component being created
43      * @param reason exception that prevented the component from being created, or null if none
44      */

45     public ComponentException(Object JavaDoc componentKey, Throwable JavaDoc reason) {
46         this(componentKey, getMessage(componentKey, reason), reason);
47     }
48     
49     /**
50      * Creates a component exception, given the name of the class the caller was
51      * attempting to create, a string describing the error, and the Throwable that
52      * caused the error.
53      *
54      * @param componentKey the fully-qualified name of the component class
55      * @param message explanation of the error
56      * @param reason exception that caused the error
57      */

58     public ComponentException(Object JavaDoc componentKey, String JavaDoc message, Throwable JavaDoc reason) {
59         super(message);
60         // don't pass the cause to super, to allow compilation against JCL Foundation
61
this.cause = ComponentUtil.getMostSpecificCause(reason);
62         
63         this.componentName = getShortName(componentKey);
64     }
65     
66     private static String JavaDoc getShortName(Object JavaDoc key) {
67         if (key instanceof Class JavaDoc) {
68             return ComponentUtil.getSimpleClassName(((Class JavaDoc)key).getName());
69         }
70         return key.toString();
71     }
72     
73     private static String JavaDoc getLongName(Object JavaDoc key) {
74         if (key instanceof Class JavaDoc) {
75             return ((Class JavaDoc)key).getName();
76         }
77         return key.toString();
78     }
79         
80     private static String JavaDoc getMessage(Object JavaDoc key, Throwable JavaDoc reason) {
81         
82         String JavaDoc longName = getLongName(key);
83         String JavaDoc shortName = getShortName(key);
84         
85         Throwable JavaDoc current = reason;
86         while (current != null) {
87             if (current instanceof ComponentException) {
88                 ComponentException ce = (ComponentException)current;
89                 if (shortName.equals(ce.getComponentName())) {
90                     return ce.getLocalizedMessage();
91                 }
92                 return getMessageString(longName, (ComponentException)current);
93             }
94             
95             Throwable JavaDoc cause = ComponentUtil.getCause(current);
96             
97             if (cause == current) {
98                 break;
99             }
100             
101             current = cause;
102         }
103         
104         reason = ComponentUtil.getCause(reason);
105         
106         if (reason != null) {
107             String JavaDoc userString = StatusUtil.getLocalizedMessage(reason);
108             
109             if (userString != null) {
110                 return NLS.bind(ComponentMessages.ComponentException_unable_to_instantiate_because, longName, userString);
111             }
112         }
113         
114         return NLS.bind(ComponentMessages.ComponentException_unable_to_instantiate, longName);
115     }
116     
117     private static String JavaDoc getMessageString(String JavaDoc componentName, ComponentException e) {
118         return NLS.bind(ComponentMessages.ComponentException_recursive_requires_string,
119                 new Object JavaDoc[] {
120                 componentName,
121                 e.getComponentName(),
122                 ComponentUtil.getMessage(e)});
123         
124     }
125     
126     /**
127      * Describes the component being created.
128      *
129      * @return description of the component being created
130      */

131     public String JavaDoc getComponentName() {
132         return componentName;
133     }
134     
135     /**
136      * Returns an IStatus suitable for logging this exception or embedding it
137      * within a CoreException.
138      *
139      * @return an IStatus suitable for embedding this exception within a CoreException.
140      */

141     public IStatus getStatus() {
142         return WorkbenchPlugin.getStatus(getMessage(), ComponentUtil.getCause(this));
143     }
144     
145     /**
146      * Returns the cause of this throwable or <code>null</code> if the
147      * cause is nonexistent or unknown.
148      *
149      * @return the cause or <code>null</code>
150      * @since 3.1
151      */

152     public Throwable JavaDoc getCause() {
153         return cause;
154     }
155     
156 }
157
Popular Tags