1 package org.apache.turbine.services; 2 3 18 19 import java.util.Hashtable ; 20 import java.util.Stack ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 25 54 public abstract class BaseInitableBroker 55 implements InitableBroker 56 { 57 58 protected Hashtable initables = new Hashtable (); 59 60 65 protected Stack stack = new Stack (); 66 67 68 private Log log = LogFactory.getLog(this.getClass()); 69 70 78 protected BaseInitableBroker() 79 { 80 } 81 82 89 public void initClass(String className, Object data) 90 throws InitializationException 91 { 92 synchronized (stack) 94 { 95 int pos = stack.search(className); 96 if (pos != -1) 97 { 98 StringBuffer msg = new StringBuffer ().append(className) 99 .append(" couldn't be initialized because of circular depency chain:\n"); 100 for (int i = pos; i > 0; i--) 101 { 102 msg.append((String ) stack.elementAt(stack.size() - i - 1) + "->"); 103 } 104 msg.append(className).append('\n'); 105 106 throw new InitializationException(msg.toString()); 107 } 108 try 109 { 110 stack.push(className); 111 Initable instance = getInitableInstance(className); 112 if (!instance.getInit()) 113 { 114 instance.init(data); 116 } 117 } 118 finally 119 { 120 stack.pop(); 122 } 123 } 124 } 125 126 135 public void shutdownClass(String className) 136 { 137 try 138 { 139 Initable initable = getInitableInstance(className); 140 if (initable.getInit()) 141 { 142 initable.shutdown(); 143 ((BaseInitable) initable).setInit(false); 144 } 145 } 146 catch (InstantiationException e) 147 { 148 log.error("Shutdown of a nonexistent class " + 151 className + " was requested", e); 152 } 153 } 154 155 168 public Initable getInitable(String className) 169 throws InstantiationException 170 { 171 Initable initable; 172 try 173 { 174 initable = getInitableInstance(className); 175 if (!initable.getInit()) 176 { 177 synchronized (initable.getClass()) 178 { 179 if (!initable.getInit()) 180 { 181 initable.init(); 182 } 183 if (!initable.getInit()) 184 { 185 throw new InitializationException( 191 "init() failed to initialize class " 192 + className); 193 } 194 } 195 } 196 return initable; 197 } 198 catch (InitializationException e) 199 { 200 throw new InstantiationException ("Class " + className + 201 " failed to initialize", e); 202 } 203 } 204 205 216 protected Initable getInitableInstance(String className) 217 throws InstantiationException 218 { 219 Initable initable = (Initable) initables.get(className); 220 221 if (initable == null) 222 { 223 try 224 { 225 initable = (Initable) Class.forName(className).newInstance(); 226 } 227 228 catch (ThreadDeath t) 230 { 231 throw t; 232 } 233 catch (OutOfMemoryError t) 234 { 235 throw t; 236 } 237 238 catch (Throwable t) 239 { 240 String msg = null; 242 243 if (t instanceof NoClassDefFoundError ) 244 { 245 msg = "A class referenced by " + className + 246 " is unavailable. Check your jars and classes."; 247 } 248 else if (t instanceof ClassNotFoundException ) 249 { 250 msg = "Class " + className + 251 " is unavailable. Check your jars and classes."; 252 } 253 else if (t instanceof ClassCastException ) 254 { 255 msg = "Class " + className + 256 " doesn't implement Initable."; 257 } 258 else 259 { 260 msg = "Failed to instantiate " + className; 261 } 262 263 throw new InstantiationException (msg, t); 264 } 265 266 initable.setInitableBroker(this); 267 initables.put(className, initable); 268 } 269 270 return initable; 271 } 272 273 } 274 | Popular Tags |