KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > ChainedNoSuchInterfaceException


1 /***
2  * Julia: France Telecom's implementation of the Fractal API
3  * Copyright (C) 2001-2002 France Telecom R&D
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License 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  * Contact: Eric.Bruneton@rd.francetelecom.com
20  *
21  * Author: Eric Bruneton
22  */

23
24 package org.objectweb.fractal.julia;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.Interface;
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29
30 import java.io.PrintStream JavaDoc;
31 import java.io.PrintWriter JavaDoc;
32 import java.io.ObjectOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.ObjectInputStream JavaDoc;
35
36 /**
37  * A sub class of the {@link NoSuchInterfaceException} class.
38  */

39
40 public class ChainedNoSuchInterfaceException extends NoSuchInterfaceException {
41
42   /**
43    * The exception that caused this exception. May be <tt>null</tt>.
44    */

45
46   private final Throwable JavaDoc exception;
47
48   /**
49    * The component whose interface cannot be found. May be <tt>null</tt>.
50    */

51
52   private transient Component component;
53
54   /**
55    * Constructs a new {@link ChainedNoSuchInterfaceException} exception.
56    *
57    * @param exception the cause of this exception. May be <tt>null</tt>.
58    * @param component the component whose interface cannot be found. May be
59    * <tt>null</tt>.
60    * @param message the name of the interface that cannot be found.
61    */

62
63   public ChainedNoSuchInterfaceException (
64     final Throwable JavaDoc exception,
65     final Component component,
66     final String JavaDoc message)
67   {
68     super(message);
69     this.exception = exception;
70     this.component = component;
71   }
72
73   /**
74    * Returns the exception that caused in this exception.
75    *
76    * @return the exception that caused this exception. May be <tt>null</tt>.
77    */

78
79   public Throwable JavaDoc getException () {
80     return exception;
81   }
82
83   /**
84    * Returns the component whose interface cannot be found.
85    *
86    * @return the component whose interface cannot be found. May be
87    * <tt>null</tt>.
88    */

89
90   public Component getComponent () {
91     if (component != null && !(component instanceof Interface)) {
92       try {
93         return (Component)component.getFcInterface("component");
94       } catch (NoSuchInterfaceException ignored) {
95       }
96     }
97     return component;
98   }
99
100   // -------------------------------------------------------------------------
101
// Overriden Exception methods
102
// -------------------------------------------------------------------------
103

104   /**
105    * Returns a String representation of this exception.
106    *
107    * @return a String representation of this exception.
108    */

109
110   public String JavaDoc toString () {
111     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
112     buf.append("NoSuchInterfaceException: ");
113     buf.append(getMessage());
114     if (getComponent() != null) {
115       buf.append(" in component ");
116       Util.toString(getComponent(), buf);
117     }
118     return buf.toString();
119   }
120
121   /**
122    * Prints the stack backtrace.
123    */

124
125   public void printStackTrace () {
126     if (exception != null) {
127       System.err.println(this);
128       exception.printStackTrace();
129     } else {
130       super.printStackTrace();
131     }
132   }
133
134   /**
135    * Prints this exception and its backtrace to the specified print stream.
136    *
137    * @param s <tt>PrintStream</tt> to use for output.
138    */

139
140   public void printStackTrace (final PrintStream JavaDoc s) {
141     if (exception != null) {
142       s.println(this);
143       exception.printStackTrace(s);
144     } else {
145       super.printStackTrace(s);
146     }
147   }
148
149   /**
150    * Prints this exception and its backtrace to the specified print writer.
151    *
152    * @param s <tt>PrintWriter</tt> to use for output.
153    */

154
155   public void printStackTrace (final PrintWriter JavaDoc s) {
156     if (exception != null) {
157       s.write(this + "\n");
158       exception.printStackTrace(s);
159     } else {
160       super.printStackTrace(s);
161     }
162   }
163
164   private void writeObject (final ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
165     out.defaultWriteObject();
166     Component c = getComponent();
167     out.writeObject(c instanceof Interface ? c : null);
168   }
169
170   private void readObject (final ObjectInputStream JavaDoc in)
171     throws IOException JavaDoc, ClassNotFoundException JavaDoc
172   {
173     in.defaultReadObject();
174     component = (Component)in.readObject();
175   }
176 }
177
Popular Tags