KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > lifecycle > ChainedIllegalLifeCycleException


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.control.lifecycle;
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.control.IllegalLifeCycleException;
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 IllegalLifeCycleException} class.
41  */

42
43 public class ChainedIllegalLifeCycleException
44   extends IllegalLifeCycleException
45 {
46
47   /**
48    * The exception that caused this exception. May be <tt>null</tt>.
49    */

50
51   private final Throwable JavaDoc exception;
52
53   /**
54    * The component whose life cycle state is illegal.
55    */

56
57   private transient Component component;
58
59   /**
60    * Constructs a new {@link ChainedIllegalLifeCycleException} exception.
61    *
62    * @param exception the cause of this exception. May be <tt>null</tt>.
63    * @param component the component whose life cycle state is illegal.
64    * @param message a detailed error message.
65    */

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

82
83   public Throwable JavaDoc getException () {
84     return exception;
85   }
86
87   /**
88    * Returns the component whose life cycle state is illegal.
89    *
90    * @return the component whose life cycle state is illegal.
91    */

92
93   public Component getComponent () {
94     if (component != null && !(component instanceof Interface)) {
95       try {
96         return (Component)component.getFcInterface("component");
97       } catch (NoSuchInterfaceException ignored) {
98       }
99     }
100     return component;
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("IllegalLifeCycleException: ");
116     buf.append(getMessage());
117     if (getComponent() != null) {
118       buf.append(" (component = ");
119       Util.toString(getComponent(), 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 = getComponent();
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     component = (Component)in.readObject();
179   }
180 }
181
Popular Tags