1 21 package oracle.toplink.essentials.internal.descriptors; 23 24 import java.io.*; 25 import java.lang.reflect.*; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 29 import oracle.toplink.essentials.exceptions.*; 30 import oracle.toplink.essentials.internal.helper.*; 31 import oracle.toplink.essentials.internal.security.*; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 import oracle.toplink.essentials.descriptors.ClassDescriptor; 34 35 63 public class InstantiationPolicy implements Cloneable , Serializable { 64 65 69 protected String methodName; 70 71 72 protected transient Method method; 73 74 78 protected Class factoryClass; 79 protected String factoryClassName; 80 81 85 protected String factoryMethodName; 86 87 91 protected Object factory; 92 93 94 protected ClassDescriptor descriptor; 95 96 97 private transient Constructor defaultConstructor; 98 99 102 public InstantiationPolicy() { 103 super(); 104 } 105 106 109 public Object buildNewInstance() throws DescriptorException { 110 if (this.isUsingDefaultConstructor()) { 111 return this.buildNewInstanceUsingDefaultConstructor(); 112 } else { 113 return this.buildNewInstanceUsingFactory(); 114 } 115 } 116 117 120 protected Object buildNewInstanceUsingDefaultConstructor() throws DescriptorException { 121 try { 122 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 123 try { 124 return AccessController.doPrivileged(new PrivilegedInvokeConstructor(this.getDefaultConstructor(), (Object [])null)); 125 } catch (PrivilegedActionException exception) { 126 Exception throwableException = exception.getException(); 127 if (throwableException instanceof InvocationTargetException){ 128 throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 129 } else if (throwableException instanceof IllegalAccessException ){ 130 throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 131 } else { 132 throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 133 } 134 } 135 } else { 136 return PrivilegedAccessHelper.invokeConstructor(this.getDefaultConstructor(), (Object [])null); 137 } 138 } catch (InvocationTargetException exception) { 139 throw DescriptorException.targetInvocationWhileConstructorInstantiation(this.getDescriptor(), exception); 140 } catch (IllegalAccessException exception) { 141 throw DescriptorException.illegalAccessWhileConstructorInstantiation(this.getDescriptor(), exception); 142 } catch (InstantiationException exception) { 143 throw DescriptorException.instantiationWhileConstructorInstantiation(this.getDescriptor(), exception); 144 } catch (NoSuchMethodError exception) { 145 throw DescriptorException.noSuchMethodWhileConstructorInstantiation(this.getDescriptor(), exception); 147 } catch (NullPointerException exception) { 148 throw DescriptorException.nullPointerWhileConstructorInstantiation(this.getDescriptor(), exception); 150 } 151 } 152 153 157 protected Object buildNewInstanceUsingFactory() throws DescriptorException { 158 try { 159 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 161 try { 162 return AccessController.doPrivileged(new PrivilegedMethodInvoker(this.getMethod(), this.getFactory(), new Object [0])); 163 } catch (PrivilegedActionException exception) { 164 Exception throwableException = exception.getException(); 165 if (throwableException instanceof IllegalAccessException ) { 166 throw DescriptorException.illegalAccessWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), throwableException); 167 } else { 168 throw DescriptorException.targetInvocationWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), throwableException); 169 } 170 } 171 } else { 172 return PrivilegedAccessHelper.invokeMethod(this.getMethod(), this.getFactory(), new Object [0]); 173 } 174 } catch (IllegalAccessException exception) { 175 throw DescriptorException.illegalAccessWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception); 176 } catch (InvocationTargetException exception) { 177 throw DescriptorException.targetInvocationWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception); 178 } catch (NullPointerException exception) { 179 throw DescriptorException.nullPointerWhileMethodInstantiation(this.getMethod().toString(), this.getDescriptor(), exception); 181 } 182 } 183 184 188 public Object clone() { 189 try { 190 return super.clone(); 192 } catch (Exception exception) { 193 ; 194 } 195 return null; 196 } 197 198 201 protected Constructor getDefaultConstructor() throws DescriptorException { 202 if (defaultConstructor == null) { 204 this.setDefaultConstructor(this.buildDefaultConstructor()); 205 } 206 return defaultConstructor; 207 } 208 209 212 protected Constructor buildDefaultConstructor() throws DescriptorException { 213 return this.buildDefaultConstructorFor(this.getDescriptor().getJavaClass()); 214 } 215 216 219 protected Constructor buildDefaultConstructorFor(Class javaClass) throws DescriptorException { 220 try { 221 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 222 try { 223 return (Constructor)AccessController.doPrivileged(new PrivilegedGetDeclaredConstructorFor(javaClass, new Class [0], true)); 224 } catch (PrivilegedActionException exception) { 225 throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception.getException()); 226 } 227 } else { 228 return PrivilegedAccessHelper.getDeclaredConstructorFor(javaClass, new Class [0], true); 229 } 230 } catch (NoSuchMethodException exception) { 231 throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(javaClass.getName() + ".<Default Constructor>", getDescriptor(), exception); 232 } 233 } 234 235 protected ClassDescriptor getDescriptor() { 236 return descriptor; 237 } 238 239 public String getFactoryMethodName() { 240 return factoryMethodName; 241 } 242 243 public Object getFactory() { 244 return factory; 245 } 246 247 public Class getFactoryClass() { 248 return factoryClass; 249 } 250 251 public String getFactoryClassName() { 252 if ((factoryClassName == null) && (factoryClass != null)) { 253 factoryClassName = factoryClass.getName(); 254 } 255 return factoryClassName; 256 } 257 258 protected Method getMethod() { 259 return method; 260 } 261 262 public String getMethodName() { 263 return methodName; 264 } 265 266 269 public void initialize(AbstractSession session) throws DescriptorException { 270 if (this.isUsingDefaultConstructor()) { 271 return; 272 } 273 try { 274 if (this.getFactory() == null) { 276 this.setFactory(this.buildFactory()); 277 } 278 this.initializeMethod(); 279 } catch (DescriptorException ex) { 280 session.getIntegrityChecker().handleError(ex); 281 } 282 } 283 284 protected Object buildFactory() throws DescriptorException { 285 if (this.getFactoryClass() == null) { 288 return null; 289 } 290 291 if (this.getFactoryMethodName() == null) { 294 return this.buildFactoryUsingDefaultConstructor(); 295 } 296 297 return this.buildFactoryUsingStaticMethod(); 300 } 301 302 305 protected Object buildFactoryUsingDefaultConstructor() throws DescriptorException { 306 try { 307 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 308 try { 309 return AccessController.doPrivileged(new PrivilegedInvokeConstructor(this.buildFactoryDefaultConstructor(), (Object [])null)); 310 } catch (PrivilegedActionException exception) { 311 Exception throwableException = exception.getException(); 312 if (throwableException instanceof InvocationTargetException){ 313 throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 314 } else if (throwableException instanceof IllegalAccessException ){ 315 throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 316 } else { 317 throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), throwableException); 318 } 319 } 320 } else { 321 return PrivilegedAccessHelper.invokeConstructor(this.buildFactoryDefaultConstructor(), (Object [])null); 322 } 323 } catch (InvocationTargetException exception) { 324 throw DescriptorException.targetInvocationWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception); 325 } catch (IllegalAccessException exception) { 326 throw DescriptorException.illegalAccessWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception); 327 } catch (InstantiationException exception) { 328 throw DescriptorException.instantiationWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception); 329 } catch (NoSuchMethodError exception) { 330 throw DescriptorException.noSuchMethodWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception); 332 } catch (NullPointerException exception) { 333 throw DescriptorException.nullPointerWhileConstructorInstantiationOfFactory(this.getDescriptor(), exception); 335 } 336 } 337 338 341 protected Constructor buildFactoryDefaultConstructor() throws DescriptorException { 342 return this.buildDefaultConstructorFor(this.getFactoryClass()); 343 } 344 345 348 protected Object buildFactoryUsingStaticMethod() throws DescriptorException { 349 Method factoryMethod = this.buildMethod(this.getFactoryClass(), this.getFactoryMethodName(), new Class [0]); 350 351 try { 352 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 354 try { 355 return AccessController.doPrivileged(new PrivilegedMethodInvoker(factoryMethod, null, null)); 356 } catch (PrivilegedActionException exception) { 357 Exception throwableException = exception.getException(); 358 if (throwableException instanceof IllegalAccessException ) { 359 throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException); 360 } else { 361 throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), throwableException); 362 } 363 364 } 365 } else { 366 return PrivilegedAccessHelper.invokeMethod(factoryMethod, null, null); 367 } 368 } catch (IllegalAccessException exception) { 369 throw DescriptorException.illegalAccessWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception); 370 } catch (InvocationTargetException exception) { 371 throw DescriptorException.targetInvocationWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception); 372 } catch (NullPointerException exception) { 373 throw DescriptorException.nullPointerWhileMethodInstantiationOfFactory(getFactoryMethodName(), getDescriptor(), exception); 375 } 376 } 377 378 382 protected void initializeMethod() throws DescriptorException { 383 Class tempClass; 384 if (this.getFactory() == null) { 385 tempClass = this.getDescriptor().getJavaClass(); 386 } else { 387 tempClass = this.getFactory().getClass(); 388 } 389 this.setMethod(this.buildMethod(tempClass, this.getMethodName(), new Class [0])); 390 } 391 392 395 protected Method buildMethod(Class methodClass, String methodName, Class [] methodParameterTypes) throws DescriptorException { 396 try { 397 return Helper.getDeclaredMethod(methodClass, methodName, methodParameterTypes); 398 } catch (NoSuchMethodException exception) { 399 throw DescriptorException.noSuchMethodWhileInitializingInstantiationPolicy(methodClass.getName() + "." + methodName, this.getDescriptor(), exception); 400 } catch (SecurityException exception) { 401 throw DescriptorException.securityWhileInitializingInstantiationPolicy(methodClass.getName() + "." + methodName, this.getDescriptor(), exception); 402 } 403 } 404 405 408 public boolean isUsingDefaultConstructor() { 409 return this.getMethodName() == null; 410 } 411 412 protected void setDefaultConstructor(Constructor defaultConstructor) { 413 this.defaultConstructor = defaultConstructor; 414 } 415 416 public void setDescriptor(ClassDescriptor descriptor) { 417 this.descriptor = descriptor; 418 } 419 420 protected void setFactoryMethodName(String factoryMethodName) { 421 this.factoryMethodName = factoryMethodName; 422 } 423 424 protected void setFactory(Object factory) { 425 this.factory = factory; 426 } 427 428 protected void setFactoryClass(Class factoryClass) { 429 this.factoryClass = factoryClass; 430 } 431 432 protected void setFactoryClassName(String factoryClassName) { 433 this.factoryClassName = factoryClassName; 434 } 435 436 protected void setMethod(Method method) { 437 this.method = method; 438 } 439 440 public void setMethodName(String methodName) { 441 this.methodName = methodName; 442 } 443 444 451 public void convertClassNamesToClasses(ClassLoader classLoader){ 452 if (factoryClassName == null){ 453 return; 454 } 455 Class factoryClass = null; 456 try{ 457 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 458 try { 459 factoryClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(factoryClassName, true, classLoader)); 460 } catch (PrivilegedActionException exception) { 461 throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exception.getException()); 462 } 463 } else { 464 factoryClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(factoryClassName, true, classLoader); 465 } 466 } catch (ClassNotFoundException exc){ 467 throw ValidationException.classNotFoundWhileConvertingClassNames(factoryClassName, exc); 468 } 469 setFactoryClass(factoryClass); 470 } 471 472 public String toString() { 473 String mName = null; 474 if (this.isUsingDefaultConstructor()) { 475 mName = "<CONSTRUCTOR>"; 476 } else { 477 mName = this.getMethodName(); 478 } 479 return Helper.getShortClassName(this) + "(" + mName + ")"; 480 } 481 482 public void useDefaultConstructorInstantiationPolicy() { 483 setMethodName(null); 484 setFactory(null); 485 setFactoryClass(null); 486 setFactoryClassName(null); 487 setFactoryMethodName(null); 488 } 489 490 public void useFactoryInstantiationPolicy(Class factoryClass, String methodName) { 491 setMethodName(methodName); 492 setFactory(null); 493 setFactoryClass(factoryClass); 494 setFactoryClassName(factoryClass.getName()); 495 setFactoryMethodName(null); 496 } 497 498 public void useFactoryInstantiationPolicy(Class factoryClass, String methodName, String factoryMethodName) { 499 setMethodName(methodName); 500 setFactory(null); 501 setFactoryClass(factoryClass); 502 setFactoryClassName(factoryClass.getName()); 503 setFactoryMethodName(factoryMethodName); 504 } 505 506 public void useFactoryInstantiationPolicy(String factoryClassName, String methodName) { 507 setMethodName(methodName); 508 setFactory(null); 509 setFactoryClass(null); 510 setFactoryClassName(factoryClassName); 511 setFactoryMethodName(null); 512 } 513 514 public void useFactoryInstantiationPolicy(String factoryClassName, String methodName, String factoryMethodName) { 515 setMethodName(methodName); 516 setFactory(null); 517 setFactoryClass(null); 518 setFactoryClassName(factoryClassName); 519 setFactoryMethodName(factoryMethodName); 520 } 521 522 public void useFactoryInstantiationPolicy(Object factory, String methodName) { 523 setMethodName(methodName); 524 setFactory(factory); 525 setFactoryClass(null); 526 setFactoryClassName(null); 527 setFactoryMethodName(null); 528 } 529 530 public void useMethodInstantiationPolicy(String staticMethodName) { 531 setMethodName(staticMethodName); 532 setFactory(null); 533 setFactoryClass(null); 534 setFactoryClassName(null); 535 setFactoryMethodName(null); 536 } 537 } 538 | Popular Tags |