KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > factory > ChainedInstantiationException


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.factory;
25
26 import org.objectweb.fractal.api.Component;
27 import org.objectweb.fractal.api.Interface;
28 import org.objectweb.fractal.api.NoSuchInterfaceException;
29 import org.objectweb.fractal.api.factory.InstantiationException;
30
31 import org.objectweb.fractal.julia.Util;
32
33 import java.io.PrintStream JavaDoc;
34 import java.io.PrintWriter JavaDoc;
35 import java.io.ObjectOutputStream JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.ObjectInputStream JavaDoc;
38
39 /**
40  * A sub class of the {@link InstantiationException} class.
41  */

42
43 public class ChainedInstantiationException extends InstantiationException JavaDoc {
44
45   /**
46    * The exception that caused this exception. May be <tt>null</tt>.
47    */

48
49   private final Throwable JavaDoc exception;
50
51   /**
52    * The factory component that cannot be instantiated. May be <tt>null</tt>.
53    */

54
55   private transient Component factory;
56
57   /**
58    * Constructs a new {@link ChainedInstantiationException} exception.
59    *
60    * @param exception the cause of this exception. May be <tt>null</tt>.
61    * @param factory the factory component that cannot be instantiated. May be
62    * <tt>null</tt>.
63    * @param message a detailed error message.
64    */

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

81
82   public Throwable JavaDoc getException () {
83     return exception;
84   }
85
86   /**
87    * Returns the factory component that cannot be instantiated.
88    *
89    * @return the factory component that cannot be instantiated. May be
90    * <tt>null</tt>.
91    */

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

107   /**
108    * Returns a String representation of this exception.
109    *
110    * @return a String representation of this exception.
111    */

112
113   public String JavaDoc toString () {
114     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
115     buf.append("InstantiationException: ");
116     buf.append(getMessage());
117     if (getFactory() != null) {
118       buf.append(" (template = ");
119       Util.toString(getFactory(), buf);
120       buf.append(')');
121     }
122     return buf.toString();
123   }
124
125   /**
126    * Prints the stack backtrace.
127    */

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

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

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