1 21 package oracle.toplink.essentials.internal.ejb.cmp3.base; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 import java.security.AccessController ; 27 import java.security.PrivilegedActionException ; 28 import java.util.*; 29 import oracle.toplink.essentials.descriptors.*; 30 import oracle.toplink.essentials.internal.helper.ConversionManager; 31 import oracle.toplink.essentials.internal.localization.ExceptionLocalization; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 import oracle.toplink.essentials.mappings.DatabaseMapping; 34 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping; 35 import oracle.toplink.essentials.exceptions.*; 36 import oracle.toplink.essentials.internal.helper.DatabaseField; 37 import oracle.toplink.essentials.internal.descriptors.ObjectBuilder; 38 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 39 import oracle.toplink.essentials.internal.security.PrivilegedGetField; 40 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 41 import oracle.toplink.essentials.internal.security.PrivilegedGetMethod; 42 import oracle.toplink.essentials.internal.security.PrivilegedGetValueFromField; 43 import oracle.toplink.essentials.internal.security.PrivilegedSetValueInField; 44 import oracle.toplink.essentials.internal.security.PrivilegedMethodInvoker; 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 46 47 53 54 public class CMP3Policy extends CMPPolicy { 56 57 58 protected KeyElementAccessor[] keyClassFields; 59 60 protected String pkClassName; 62 63 protected Class pkClass; 65 66 public CMP3Policy() { 67 super(); 68 } 69 70 77 public void convertClassNamesToClasses(ClassLoader classLoader){ 78 if(getPKClassName() != null){ 79 try{ 80 Class pkClass = null; 81 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 82 try { 83 pkClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(getPKClassName(), true, classLoader)); 84 } catch (PrivilegedActionException exception) { 85 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("pk_class_not_found", new Object [] {this.pkClassName}), exception.getException()); 86 87 } 88 } else { 89 pkClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(getPKClassName(), true, classLoader); 90 } 91 setPKClass(pkClass); 92 } catch (ClassNotFoundException exc){ 93 throw new IllegalArgumentException (ExceptionLocalization.buildMessage("pk_class_not_found", new Object [] {this.pkClassName}), exc); 94 } 95 } 96 } 97 98 102 public boolean isCMP3Policy() { 103 return true; 104 } 105 106 109 public void setPrimaryKeyClassName(String pkClassName) { 110 this.pkClassName = pkClassName; 111 } 112 113 116 public Class getPKClass() { 117 if(this.pkClass == null && getPKClassName() == null) { 118 initializePrimaryKeyFields(null); 119 } 120 return this.pkClass; 121 } 122 123 126 public void setPKClass(Class pkClass) { 127 this.pkClass = pkClass; 128 } 129 130 133 public String getPKClassName() { 134 return pkClassName; 135 } 136 137 140 public Object getPKClassInstance() { 141 try { 142 return getPKClass().newInstance(); 143 } catch (Exception ex) { 144 return null; 145 } 147 } 148 149 159 public Vector createPkVectorFromKey(Object key, AbstractSession session) { 160 KeyElementAccessor[] pkElementArray = this.getKeyClassFields(key.getClass()); 164 Vector pkVector = new Vector(pkElementArray.length); 165 for (int index = 0; index < pkElementArray.length; index++) { 166 DatabaseMapping mapping = this.getDescriptor().getObjectBuilder().getMappingForAttributeName(pkElementArray[index].getAttributeName()); 167 if (mapping == null) { 168 mapping = this.getDescriptor().getObjectBuilder().getMappingForField(pkElementArray[index].getDatabaseField()); 169 } 170 while (mapping.isAggregateObjectMapping()) { 171 mapping = mapping.getReferenceDescriptor().getObjectBuilder().getMappingForAttributeName(pkElementArray[index].getAttributeName()); 172 if (mapping == null) { mapping = this.getDescriptor().getObjectBuilder().getMappingForField(pkElementArray[index].getDatabaseField()); 174 } 175 } 176 Object fieldValue = null; 177 if (mapping.isDirectToFieldMapping()) { 178 fieldValue = ((AbstractDirectMapping)mapping).getFieldValue(pkElementArray[index].getValue(key), (oracle.toplink.essentials.internal.sessions.AbstractSession)session); 179 } else { 180 fieldValue = pkElementArray[index].getValue(key); 181 } 182 pkVector.add(fieldValue); 183 } 184 return pkVector; 185 } 186 187 191 public Object createPrimaryKeyInstance(Object key, AbstractSession session) { 192 Object keyInstance = getPKClassInstance(); 193 ObjectBuilder builder = getDescriptor().getObjectBuilder(); 194 KeyElementAccessor[] pkElementArray = this.getKeyClassFields(getPKClass()); 195 196 for (int index = 0; index < pkElementArray.length; index++) { 197 KeyElementAccessor accessor = pkElementArray[index]; 198 DatabaseMapping mapping = builder.getMappingForAttributeName(accessor.getAttributeName()); 199 202 while (mapping.isAggregateObjectMapping()) { 203 mapping = mapping.getReferenceDescriptor().getObjectBuilder().getMappingForAttributeName(pkElementArray[index].getAttributeName()); 204 205 if (mapping == null) { mapping = builder.getMappingForField(accessor.getDatabaseField()); 207 } 208 } 209 210 Object fieldValue = mapping.getRealAttributeValueFromObject(key, (oracle.toplink.essentials.internal.sessions.AbstractSession) session); 211 accessor.setValue(keyInstance, fieldValue); 212 } 213 214 return keyInstance; 215 } 216 217 218 230 protected Object createBeanUsingKey(Object key, AbstractSession session) { 231 try { 232 Object bean = this.getDescriptor().getInstantiationPolicy().buildNewInstance(); 233 KeyElementAccessor[] keyElements = this.getKeyClassFields(key.getClass()); 234 for (int index = 0; index < keyElements.length; ++index) { 235 Object toWriteInto = bean; 236 Object keyFieldValue = keyElements[index].getValue(key); 237 DatabaseField field = keyElements[index].getDatabaseField(); 238 DatabaseMapping mapping = this.getDescriptor().getObjectBuilder().getMappingForAttributeName(keyElements[index].getAttributeName()); 239 if (mapping == null) { mapping = this.getDescriptor().getObjectBuilder().getMappingForField(field); 241 } 242 while (mapping.isAggregateObjectMapping()) { 243 Object aggregate = mapping.getRealAttributeValueFromObject(toWriteInto, session); 244 if (aggregate == null) { 245 aggregate = mapping.getReferenceDescriptor().getJavaClass().newInstance(); 246 mapping.setRealAttributeValueInObject(toWriteInto, aggregate); 247 } 248 mapping = mapping.getReferenceDescriptor().getObjectBuilder().getMappingForAttributeName(keyElements[index].getAttributeName()); 249 if (mapping == null) { mapping = this.getDescriptor().getObjectBuilder().getMappingForField(field); 251 } 252 253 toWriteInto = aggregate; 256 } 257 mapping.setRealAttributeValueInObject(toWriteInto, keyFieldValue); 258 } 259 return bean; 260 } catch (Exception e) { 261 throw DescriptorException.errorUsingPrimaryKey(key, this.getDescriptor(), e); 262 } 263 } 264 265 274 protected KeyElementAccessor[] initializePrimaryKeyFields(Class keyClass) { 275 KeyElementAccessor[] pkAttributes = null; 276 ClassDescriptor descriptor = this.getDescriptor(); 277 278 pkAttributes = new KeyElementAccessor[descriptor.getObjectBuilder().getPrimaryKeyMappings().size()]; 279 280 Iterator attributesIter = descriptor.getPrimaryKeyFields().iterator(); 281 282 for (int i = 0; attributesIter.hasNext(); i++) { 284 DatabaseField field = (DatabaseField)attributesIter.next(); 285 Vector allMappings = null; 286 287 allMappings = descriptor.getObjectBuilder().getReadOnlyMappingsForField(field); 291 if (allMappings == null) { 292 allMappings = new Vector(1); 293 } 294 allMappings.add(descriptor.getObjectBuilder().getMappingForField(field)); 295 Exception elementIsFound = null; for (int index = (allMappings.size() - 1); index >= 0; --index) { DatabaseMapping mapping = (DatabaseMapping)allMappings.get(index); 298 if (mapping.isAggregateMapping()) { Vector aggregateMappings = mapping.getReferenceDescriptor().getObjectBuilder().getReadOnlyMappingsForField(field); 300 if ((aggregateMappings != null) && (!aggregateMappings.isEmpty())) { 301 allMappings.add(aggregateMappings); 303 index = (allMappings.size() - 1); 304 } 305 mapping = mapping.getReferenceDescriptor().getObjectBuilder().getMappingForField(field); 306 } 307 String fieldName = mapping.getAttributeName(); 308 if (keyClass == null){ 309 pkAttributes[i] = new KeyIsElementAccessor(mapping.getAttributeName(), field); 311 setPKClass(ConversionManager.getObjectClass(mapping.getAttributeClassification())); 312 elementIsFound = null; 313 } else { 314 try { 315 Field keyField = null; 316 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 317 try { 318 keyField = (Field )AccessController.doPrivileged(new PrivilegedGetField(keyClass, fieldName, true)); 319 } catch (PrivilegedActionException exception) { 320 throw (NoSuchFieldException )exception.getException(); 321 } 322 } else { 323 keyField = PrivilegedAccessHelper.getField(keyClass, fieldName, true); 324 } 325 pkAttributes[i] = new FieldAccessor(keyField, fieldName, field); 326 elementIsFound = null; 327 } catch (NoSuchFieldException ex) { 328 StringBuffer buffer = new StringBuffer (); 330 buffer.append("get"); 331 buffer.append(fieldName.substring(0, 1).toUpperCase()); 332 buffer.append(fieldName.substring(1)); 333 try { 334 Method method = null; 335 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 336 try { 337 method = (Method )AccessController.doPrivileged(new PrivilegedGetMethod(keyClass, buffer.toString(), new Class [] { }, true)); 338 } catch (PrivilegedActionException exception) { 339 throw (NoSuchMethodException )exception.getException(); 340 } 341 } else { 342 method = PrivilegedAccessHelper.getMethod(keyClass, buffer.toString(), new Class [] { }, true); 343 } 344 pkAttributes[i] = new PropertyAccessor(method, fieldName, field); 345 elementIsFound = null; 346 } catch (NoSuchMethodException exs) { 347 if (descriptor.getObjectBuilder().getPrimaryKeyMappings().size() == 1) { 349 pkAttributes[i] = new KeyIsElementAccessor(mapping.getAttributeName(), field); 351 setPKClass(ConversionManager.getObjectClass(mapping.getAttributeClassification())); 352 elementIsFound = null; 353 } else { 354 elementIsFound = exs; 355 } 356 } 357 } 358 } 359 360 if (elementIsFound == null) { 361 break; } 363 } 364 if (elementIsFound != null) { 365 throw DescriptorException.errorUsingPrimaryKey(keyClass, getDescriptor(), elementIsFound); 366 } 367 } 368 return pkAttributes; 369 } 370 371 375 protected KeyElementAccessor[] getKeyClassFields(Class clazz) { 376 if (this.keyClassFields == null){ 377 this.keyClassFields = initializePrimaryKeyFields(this.pkClass == null? clazz : this.pkClass); 378 } 379 return this.keyClassFields; 380 } 381 382 386 private interface KeyElementAccessor { 387 public String getAttributeName(); 388 public DatabaseField getDatabaseField(); 389 public Object getValue(Object object); 390 public void setValue(Object object, Object value); 391 } 392 393 397 private class PropertyAccessor implements KeyElementAccessor { 398 protected Method method; 399 protected String attributeName; 400 protected DatabaseField databaseField; 401 402 public PropertyAccessor(Method method, String attributeName, DatabaseField field) { 403 this.method = method; 404 this.attributeName = attributeName; 405 this.databaseField = field; 406 } 407 408 public String getAttributeName() { 409 return this.attributeName; 410 } 411 412 public DatabaseField getDatabaseField() { 413 return this.databaseField; 414 } 415 416 public Object getValue(Object object) { 417 try { 418 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 419 try { 420 return AccessController.doPrivileged(new PrivilegedMethodInvoker(method, object, new Object [] { })); 421 } catch (PrivilegedActionException exception) { 422 Exception throwableException = exception.getException(); 423 if (throwableException instanceof IllegalAccessException ) { 424 throw (IllegalAccessException )throwableException; 425 } else { 426 throw (java.lang.reflect.InvocationTargetException )throwableException; 427 } 428 } 429 } else { 430 return PrivilegedAccessHelper.invokeMethod(method, object, new Object [] { }); 431 } 432 } catch (Exception ex) { 433 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), ex); 434 } 435 } 436 437 public void setValue(Object object, Object value) { 438 try { 439 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 440 try { 441 AccessController.doPrivileged(new PrivilegedMethodInvoker(MetadataHelper.getSetMethod(method, object.getClass()), object, new Object [] {value})); 442 } catch (PrivilegedActionException exception) { 443 Exception throwableException = exception.getException(); 444 if (throwableException instanceof IllegalAccessException ) { 445 throw (IllegalAccessException )throwableException; 446 } else { 447 throw (java.lang.reflect.InvocationTargetException )throwableException; 448 } 449 } 450 } else { 451 PrivilegedAccessHelper.invokeMethod(MetadataHelper.getSetMethod(method, object.getClass()), object, new Object [] {value}); 452 } 453 } catch (Exception ex) { 454 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), ex); 455 } 456 } 457 } 458 459 463 private class FieldAccessor implements KeyElementAccessor { 464 protected Field field; 465 protected String attributeName; 466 protected DatabaseField databaseField; 467 468 public FieldAccessor(Field field, String attributeName, DatabaseField databaseField) { 469 this.field = field; 470 this.attributeName = attributeName; 471 this.databaseField = databaseField; 472 } 473 474 public String getAttributeName() { 475 return this.attributeName; 476 } 477 478 public DatabaseField getDatabaseField() { 479 return this.databaseField; 480 } 481 482 public Object getValue(Object object) { 483 try { 484 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 485 try { 486 return AccessController.doPrivileged(new PrivilegedGetValueFromField(field, object)); 487 } catch (PrivilegedActionException exception) { 488 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), exception.getException()); } 489 } else { 490 return oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getValueFromField(field, object); 491 } 492 } catch (Exception ex) { 493 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), ex); 494 } 495 } 496 497 public void setValue(Object object, Object value) { 498 try { 499 Field pkField = null; 500 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 501 try { 502 pkField = (Field )AccessController.doPrivileged(new PrivilegedGetField(object.getClass(), field.getName(), true)); 503 AccessController.doPrivileged(new PrivilegedSetValueInField(pkField, object, value)); 504 } catch (PrivilegedActionException exception) { 505 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), exception.getException()); 506 } 507 } else { 508 pkField = PrivilegedAccessHelper.getField(object.getClass(), field.getName(), true); 509 PrivilegedAccessHelper.setValueInField(pkField, object, value); 510 } 511 } catch (Exception ex) { 512 throw DescriptorException.errorUsingPrimaryKey(object, getDescriptor(), ex); 513 } 514 } 515 } 516 517 521 private class KeyIsElementAccessor implements KeyElementAccessor { 522 protected String attributeName; 523 protected DatabaseField databaseField; 524 525 public KeyIsElementAccessor(String attributeName, DatabaseField databaseField) { 526 this.attributeName = attributeName; 527 this.databaseField = databaseField; 528 } 529 530 public String getAttributeName() { 531 return attributeName; 532 } 533 534 public DatabaseField getDatabaseField() { 535 return this.databaseField; 536 } 537 538 public Object getValue(Object object) { 539 return object; 540 } 541 542 public void setValue(Object object, Object value) { 543 } 545 } 546 } 547 | Popular Tags |