1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.AmberRuntimeException; 33 import com.caucho.amber.entity.Listener; 34 import com.caucho.amber.field.StubMethod; 35 import com.caucho.amber.manager.AmberPersistenceUnit; 36 import com.caucho.bytecode.JClass; 37 import com.caucho.bytecode.JMethod; 38 import com.caucho.util.L10N; 39 40 import java.util.ArrayList ; 41 import java.util.logging.Level ; 42 import java.util.logging.Logger ; 43 44 48 public abstract class AbstractEnhancedType extends Type { 49 private static final Logger log = Logger.getLogger(AbstractEnhancedType.class.getName()); 50 private static final L10N L = new L10N(AbstractEnhancedType.class); 51 52 AmberPersistenceUnit _amberPersistenceUnit; 53 54 JClass _beanClass; 55 56 private String _name; 57 58 private String _instanceClassName; 59 60 private ClassLoader _instanceLoader; 61 private Class _instanceClass; 62 63 private boolean _isIdClass; 65 66 private boolean _isEnhanced; 67 68 private volatile boolean _isGenerated; 69 70 private Object _instance; 71 72 private Throwable _exception; 73 74 private ArrayList <StubMethod> _methods = new ArrayList <StubMethod>(); 75 76 private ArrayList <JMethod> _postLoadCallbacks 77 = new ArrayList <JMethod>(); 78 79 private ArrayList <JMethod> _prePersistCallbacks 80 = new ArrayList <JMethod>(); 81 82 private ArrayList <JMethod> _postPersistCallbacks 83 = new ArrayList <JMethod>(); 84 85 private ArrayList <JMethod> _preUpdateCallbacks 86 = new ArrayList <JMethod>(); 87 88 private ArrayList <JMethod> _postUpdateCallbacks 89 = new ArrayList <JMethod>(); 90 91 private ArrayList <JMethod> _preRemoveCallbacks 92 = new ArrayList <JMethod>(); 93 94 private ArrayList <JMethod> _postRemoveCallbacks 95 = new ArrayList <JMethod>(); 96 97 98 public AbstractEnhancedType(AmberPersistenceUnit amberPersistenceUnit) 99 { 100 _amberPersistenceUnit = amberPersistenceUnit; 101 } 102 103 106 public AmberPersistenceUnit getPersistenceUnit() 107 { 108 return _amberPersistenceUnit; 109 } 110 111 public Throwable getConfigException() 112 { 113 return _exception; 114 } 115 116 public void setConfigException(Throwable e) 117 { 118 if (_exception == null) 119 _exception = e; 120 } 121 122 125 public void setBeanClass(JClass beanClass) 126 { 127 _beanClass = beanClass; 128 129 if (getName() == null) { 130 String name = beanClass.getName(); 131 int p = name.lastIndexOf('.'); 132 133 if (p > 0) 134 name = name.substring(p + 1); 135 136 setName(name); 137 } 138 } 139 140 143 public JClass getBeanClass() 144 { 145 return _beanClass; 146 } 147 148 151 public void setName(String name) 152 { 153 _name = name; 154 } 155 156 159 public String getName() 160 { 161 return _name; 162 } 163 164 167 public void setInstance(Object instance) 168 { 169 _instance = instance; 170 } 171 172 175 public Object getInstance() 176 { 177 if (_instance == null) { 178 try { 179 _instance = getInstanceClass().newInstance(); 180 } catch (Exception e) { 181 throw new RuntimeException (e); 182 } 183 } 184 185 return _instance; 186 } 187 188 191 public void setInstanceClassLoader(ClassLoader loader) 192 { 193 _instanceLoader = loader; 194 } 195 196 public boolean isInit() 197 { 198 return _instanceClass != null; 199 } 200 201 204 abstract public Class getInstanceClass(); 205 206 209 protected Class getInstanceClass(Class validationInterface) 210 { 211 if (_instanceClass == null) { 212 if (getInstanceClassName() == null) { 213 throw new RuntimeException ("No instance class:" + this); 214 } 215 216 try { 217 if (isEnhanced()) { 218 ClassLoader loader = getPersistenceUnit().getEnhancedLoader(); 219 220 if (log.isLoggable(Level.FINEST)) 221 log.finest(L.l("loading bean class `{0}' from `{1}'", getBeanClass().getName(), loader)); 222 223 _instanceClass = Class.forName(getBeanClass().getName(), false, loader); 224 } 225 else { 226 ClassLoader loader = _instanceLoader; 227 228 if (loader == null) 229 loader = getPersistenceUnit().getEnhancedLoader(); 230 231 if (log.isLoggable(Level.FINEST)) 232 log.finest(L.l("loading instance class `{0}' from `{1}'", getInstanceClassName(), loader)); 233 234 _instanceClass = Class.forName(getInstanceClassName(), false, loader); 235 } 236 } catch (ClassNotFoundException e) { 237 throw new RuntimeException (e); 238 } 239 240 if (! validationInterface.isAssignableFrom(_instanceClass)) { 241 if (getConfigException() != null) 242 throw new AmberRuntimeException(getConfigException()); 243 else if (_amberPersistenceUnit.getConfigException() != null) 244 throw new AmberRuntimeException(_amberPersistenceUnit.getConfigException()); 245 246 throw new AmberRuntimeException(L.l("'{0}' with classloader {1} is an illegal instance class", 247 _instanceClass.getName(), _instanceClass.getClassLoader())); 248 } 249 } 250 251 return _instanceClass; 252 } 253 254 257 public void setInstanceClassName(String className) 258 { 259 _instanceClassName = className; 260 } 261 262 265 public String getInstanceClassName() 266 { 267 return _instanceClassName; 268 } 269 270 273 public void setEnhanced(boolean isEnhanced) 274 { 275 _isEnhanced = isEnhanced; 276 } 277 278 281 public boolean isEnhanced() 282 { 283 return _isEnhanced; 284 } 285 286 290 public void setIdClass(boolean isIdClass) 291 { 292 294 _isIdClass = isIdClass; 295 } 296 297 300 public boolean isIdClass() 301 { 302 return _isIdClass; 303 } 304 305 308 public boolean isGenerated() 309 { 310 return _isGenerated; 311 } 312 313 316 public void setGenerated(boolean isGenerated) 317 { 318 if (isEnhanced()) 320 _isGenerated = isGenerated; 321 } 322 323 326 public void addStubMethod(StubMethod method) 327 { 328 _methods.add(method); 329 } 330 331 334 public ArrayList <StubMethod> getMethods() 335 { 336 return _methods; 337 } 338 339 342 public void addPostLoadCallback(JMethod callback) 343 { 344 _postLoadCallbacks.add(callback); 345 } 346 347 350 public ArrayList <JMethod> getPostLoadCallbacks() 351 { 352 return _postLoadCallbacks; 353 } 354 355 358 public void addPrePersistCallback(JMethod callback) 359 { 360 _prePersistCallbacks.add(callback); 361 } 362 363 366 public ArrayList <JMethod> getPrePersistCallbacks() 367 { 368 return _prePersistCallbacks; 369 } 370 371 374 public void addPostPersistCallback(JMethod callback) 375 { 376 _postPersistCallbacks.add(callback); 377 } 378 379 382 public ArrayList <JMethod> getPostPersistCallbacks() 383 { 384 return _postPersistCallbacks; 385 } 386 387 390 public void addPreUpdateCallback(JMethod callback) 391 { 392 _preUpdateCallbacks.add(callback); 393 } 394 395 398 public ArrayList <JMethod> getPreUpdateCallbacks() 399 { 400 return _preUpdateCallbacks; 401 } 402 403 406 public void addPostUpdateCallback(JMethod callback) 407 { 408 _postUpdateCallbacks.add(callback); 409 } 410 411 414 public ArrayList <JMethod> getPostUpdateCallbacks() 415 { 416 return _postUpdateCallbacks; 417 } 418 419 422 public void addPreRemoveCallback(JMethod callback) 423 { 424 _preRemoveCallbacks.add(callback); 425 } 426 427 430 public ArrayList <JMethod> getPreRemoveCallbacks() 431 { 432 return _preRemoveCallbacks; 433 } 434 435 438 public void addPostRemoveCallback(JMethod callback) 439 { 440 _postRemoveCallbacks.add(callback); 441 } 442 443 446 public ArrayList <JMethod> getPostRemoveCallbacks() 447 { 448 return _postRemoveCallbacks; 449 } 450 451 454 public ArrayList <JMethod> getCallbacks(int callbackIndex) 455 { 456 switch (callbackIndex) { 457 case Listener.PRE_PERSIST: 458 return _prePersistCallbacks; 459 case Listener.POST_PERSIST: 460 return _postPersistCallbacks; 461 case Listener.PRE_REMOVE: 462 return _preRemoveCallbacks; 463 case Listener.POST_REMOVE: 464 return _postRemoveCallbacks; 465 case Listener.PRE_UPDATE: 466 return _preUpdateCallbacks; 467 case Listener.POST_UPDATE: 468 return _postUpdateCallbacks; 469 case Listener.POST_LOAD: 470 return _postLoadCallbacks; 471 } 472 473 return null; 474 } 475 476 479 public void addCallback(int callbackIndex, 480 JMethod callback) 481 { 482 switch (callbackIndex) { 483 case Listener.PRE_PERSIST: 484 _prePersistCallbacks.add(callback); 485 break; 486 case Listener.POST_PERSIST: 487 _postPersistCallbacks.add(callback); 488 break; 489 case Listener.PRE_REMOVE: 490 _preRemoveCallbacks.add(callback); 491 break; 492 case Listener.POST_REMOVE: 493 _postRemoveCallbacks.add(callback); 494 break; 495 case Listener.PRE_UPDATE: 496 _preUpdateCallbacks.add(callback); 497 break; 498 case Listener.POST_UPDATE: 499 _postUpdateCallbacks.add(callback); 500 break; 501 case Listener.POST_LOAD: 502 _postLoadCallbacks.add(callback); 503 break; 504 } 505 } 506 507 510 public String toString() 511 { 512 return "AbstractEnhancedType[" + _beanClass.getName() + "]"; 513 } 514 } 515 | Popular Tags |