1 21 package oracle.toplink.essentials.mappings.converters; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 27 import oracle.toplink.essentials.mappings.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.mappings.foundation.AbstractDirectMapping; 30 import oracle.toplink.essentials.sessions.*; 31 import oracle.toplink.essentials.exceptions.*; 32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 33 import oracle.toplink.essentials.internal.security.PrivilegedNewInstanceFromClass; 34 import oracle.toplink.essentials.internal.sessions.AbstractSession; 35 36 42 public class ClassInstanceConverter implements Converter { 43 protected DatabaseMapping mapping; 44 45 49 public ClassInstanceConverter() { 50 } 51 52 56 public Object convertDataValueToObjectValue(Object fieldValue, Session session) { 57 Object attributeValue = null; 58 if (fieldValue != null) { 59 Class attributeClass = (Class )((AbstractSession)session).getDatasourcePlatform().convertObject(fieldValue, ClassConstants.CLASS); 60 try { 61 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 62 try { 63 attributeValue = AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(attributeClass)); 64 } catch (PrivilegedActionException exception) { 65 throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception.getException()); 66 } 67 } else { 68 attributeValue = PrivilegedAccessHelper.newInstanceFromClass(attributeClass); 69 } 70 } catch (Exception exception) { 71 throw ConversionException.couldNotBeConverted(fieldValue, attributeClass, exception); 72 } 73 } 74 75 return attributeValue; 76 } 77 78 82 public Object convertObjectValueToDataValue(Object attributeValue, Session session) { 83 if (attributeValue == null) { 84 return null; 85 } 86 return attributeValue.getClass().getName(); 87 } 88 89 93 public void initialize(DatabaseMapping mapping, Session session) { 94 this.mapping = mapping; 95 if (getMapping().isDirectToFieldMapping()) { 97 AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping(); 98 99 if (directMapping.getFieldClassification() == null) { 101 directMapping.setFieldClassification(ClassConstants.STRING); 102 } 103 } 104 } 105 106 110 protected DatabaseMapping getMapping() { 111 return mapping; 112 } 113 114 120 public boolean isMutable() { 121 return false; 122 } 123 } 124 | Popular Tags |