1 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 ; 34 import java.io.PrintWriter ; 35 import java.io.ObjectOutputStream ; 36 import java.io.IOException ; 37 import java.io.ObjectInputStream ; 38 39 42 43 public class ChainedInstantiationException extends InstantiationException { 44 45 48 49 private final Throwable exception; 50 51 54 55 private transient Component factory; 56 57 65 66 public ChainedInstantiationException ( 67 final Throwable exception, 68 final Component factory, 69 final String message) 70 { 71 super(message); 72 this.exception = exception; 73 this.factory = factory; 74 } 75 76 81 82 public Throwable getException () { 83 return exception; 84 } 85 86 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 107 112 113 public String toString () { 114 StringBuffer buf = new StringBuffer (); 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 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 143 144 public void printStackTrace (final PrintStream s) { 145 if (exception != null) { 146 s.println(this); 147 exception.printStackTrace(s); 148 } else { 149 super.printStackTrace(s); 150 } 151 } 152 153 158 159 public void printStackTrace (final PrintWriter 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 out) throws IOException { 169 out.defaultWriteObject(); 170 Component c = getFactory(); 171 out.writeObject(c instanceof Interface ? c : null); 172 } 173 174 private void readObject (final ObjectInputStream in) 175 throws IOException , ClassNotFoundException 176 { 177 in.defaultReadObject(); 178 factory = (Component)in.readObject(); 179 } 180 } 181 | Popular Tags |