KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > fractal > julia > control > content > ChainedIllegalContentException


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.content;
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.IllegalContentException;
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 IllegalContentException} class.
41  */

42
43 public class ChainedIllegalContentException extends IllegalContentException {
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 component whose content is or would become illegal.
53    */

54
55   private transient Component component;
56
57   /**
58    * The component that makes the content of {@link #getComponent
59    * getComponent} illegal. May be <tt>null</tt>.
60    */

61
62   private transient Component content;
63
64   /**
65    * Constructs a new {@link ChainedIllegalContentException} exception.
66    *
67    * @param exception the cause of this exception. May be <tt>null</tt>.
68    * @param component the component whose content is or would become illegal.
69    * @param content the component that makes the content of <tt>component</tt>
70    * illegal. May be <tt>null</tt>.
71    * @param message a detailed error message.
72    */

73
74   public ChainedIllegalContentException (
75     final Throwable JavaDoc exception,
76     final Component component,
77     final Component content,
78     final String JavaDoc message)
79   {
80     super(message);
81     this.exception = exception;
82     this.component = component;
83     this.content = content;
84   }
85
86   /**
87    * Returns the exception that caused in this exception.
88    *
89    * @return the exception that caused this exception. May be <tt>null</tt>.
90    */

91
92   public Throwable JavaDoc getException () {
93     return exception;
94   }
95
96   /**
97    * Returns the component whose content is or would become illegal.
98    *
99    * @return the component whose content is or would become illegal.
100    */

101
102   public Component getComponent () {
103     if (component != null && !(component instanceof Interface)) {
104       try {
105         return (Component)component.getFcInterface("component");
106       } catch (NoSuchInterfaceException ignored) {
107       }
108     }
109     return component;
110   }
111
112   /**
113    * Returns the component that makes the content of {@link #getComponent
114    * getComponent} illegal. More precisely, this method returns the component
115    * that would make the content of {@link #getComponent getComponent} illegal
116    * if it were added or removed from it.
117    *
118    * @return the component that makes the content of {@link #getComponent
119    * getComponent} illegal. May be <tt>null</tt>.
120    */

121
122   public Component getContent () {
123     if (content != null && !(content instanceof Interface)) {
124       try {
125         return (Component)content.getFcInterface("component");
126       } catch (NoSuchInterfaceException ignored) {
127       }
128     }
129     return content;
130   }
131
132   // -------------------------------------------------------------------------
133
// Overriden Exception methods
134
// -------------------------------------------------------------------------
135

136   /**
137    * Returns a String representation of this exception.
138    *
139    * @return a String representation of this exception.
140    */

141
142   public String JavaDoc toString () {
143     StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
144     buf.append("IllegalContentException: ");
145     buf.append(getMessage());
146     buf.append(" (super component = ");
147     Util.toString(getComponent(), buf);
148     if (getContent() != null) {
149       buf.append(", sub component = ");
150       Util.toString(getContent(), buf);
151     }
152     buf.append(')');
153     return buf.toString();
154   }
155
156   /**
157    * Prints the stack backtrace.
158    */

159
160   public void printStackTrace () {
161     if (exception != null) {
162       System.err.println(this);
163       exception.printStackTrace();
164     } else {
165       super.printStackTrace();
166     }
167   }
168
169   /**
170    * Prints this exception and its backtrace to the specified print stream.
171    *
172    * @param s <tt>PrintStream</tt> to use for output.
173    */

174
175   public void printStackTrace (final PrintStream JavaDoc s) {
176     if (exception != null) {
177       s.println(this);
178       exception.printStackTrace(s);
179     } else {
180       super.printStackTrace(s);
181     }
182   }
183
184   /**
185    * Prints this exception and its backtrace to the specified print writer.
186    *
187    * @param s <tt>PrintWriter</tt> to use for output.
188    */

189
190   public void printStackTrace (final PrintWriter JavaDoc s) {
191     if (exception != null) {
192       s.write(this + "\n");
193       exception.printStackTrace(s);
194     } else {
195       super.printStackTrace(s);
196     }
197   }
198   
199   private void writeObject (final ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
200     out.defaultWriteObject();
201     Component c = getComponent();
202     out.writeObject(c instanceof Interface ? c : null);
203     c = getContent();
204     out.writeObject(c instanceof Interface ? c : null);
205   }
206
207   private void readObject (final ObjectInputStream JavaDoc in)
208     throws IOException JavaDoc, ClassNotFoundException JavaDoc
209   {
210     in.defaultReadObject();
211     component = (Component)in.readObject();
212     content = (Component)in.readObject();
213   }
214 }
215
Popular Tags