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.mappings.foundation.AbstractDirectMapping; 29 import oracle.toplink.essentials.sessions.*; 30 import oracle.toplink.essentials.exceptions.ConversionException; 31 import oracle.toplink.essentials.exceptions.ValidationException; 32 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 33 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 34 import oracle.toplink.essentials.internal.sessions.AbstractSession; 35 36 43 public class TypeConversionConverter implements Converter { 44 protected DatabaseMapping mapping; 45 46 47 protected Class dataClass; 48 protected String dataClassName; 49 50 51 protected Class objectClass; 52 protected String objectClassName; 53 54 58 public TypeConversionConverter() { 59 } 60 61 65 public TypeConversionConverter(DatabaseMapping mapping) { 66 this.mapping = mapping; 67 } 68 69 77 public void convertClassNamesToClasses(ClassLoader classLoader){ 78 Class dataClass = null; 79 Class objectClass = null; 80 try{ 81 if (dataClassName != null){ 82 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 83 try { 84 dataClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(dataClassName, true, classLoader)); 85 } catch (PrivilegedActionException exception) { 86 throw ValidationException.classNotFoundWhileConvertingClassNames(dataClassName, exception.getException()); 87 } 88 } else { 89 dataClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(dataClassName, true, classLoader); 90 } 91 setDataClass(dataClass); 92 } 93 } catch (ClassNotFoundException exc){ 94 throw ValidationException.classNotFoundWhileConvertingClassNames(dataClassName, exc); 95 } 96 try { 97 if (objectClassName != null){ 98 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 99 try { 100 objectClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(objectClassName, true, classLoader)); 101 } catch (PrivilegedActionException exception) { 102 throw ValidationException.classNotFoundWhileConvertingClassNames(objectClassName, exception.getException()); 103 } 104 } else { 105 objectClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(objectClassName, true, classLoader); 106 } 107 setObjectClass(objectClass); 108 } 109 } catch (ClassNotFoundException exc){ 110 throw ValidationException.classNotFoundWhileConvertingClassNames(objectClassName, exc); 111 } 112 }; 113 114 118 public Object convertDataValueToObjectValue(Object fieldValue, Session session) { 119 Object attributeValue = fieldValue; 120 if (attributeValue != null) { 121 try { 122 attributeValue = ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getDataClass()); 123 } catch (ConversionException e) { 124 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e); 125 } 126 127 try { 128 attributeValue = ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getObjectClass()); 129 } catch (ConversionException e) { 130 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e); 131 } 132 } 133 134 return attributeValue; 135 } 136 137 141 public Class getObjectClass() { 142 return objectClass; 143 } 144 145 149 public String getObjectClassName() { 150 if ((objectClassName == null) && (objectClass != null)) { 151 objectClassName = objectClass.getName(); 152 } 153 return objectClassName; 154 } 155 156 160 public Class getDataClass() { 161 return dataClass; 162 } 163 164 168 public String getDataClassName() { 169 if ((dataClassName == null) && (dataClass != null)) { 170 dataClassName = dataClass.getName(); 171 } 172 return dataClassName; 173 } 174 175 179 public void setDataClass(Class dataClass) { 180 this.dataClass = dataClass; 181 } 182 183 187 public void setDataClassName(String dataClassName) { 188 this.dataClassName = dataClassName; 189 } 190 191 195 public void setObjectClass(Class objectClass) { 196 this.objectClass = objectClass; 197 } 198 199 203 public void setObjectClassName(String objectClassName) { 204 this.objectClassName = objectClassName; 205 } 206 207 211 public Object convertObjectValueToDataValue(Object attributeValue, Session session) { 212 try { 213 return ((AbstractSession)session).getDatasourcePlatform().convertObject(attributeValue, getDataClass()); 214 } catch (ConversionException e) { 215 throw ConversionException.couldNotBeConverted(mapping, mapping.getDescriptor(), e); 216 } 217 } 218 219 223 public void initialize(DatabaseMapping mapping, Session session) { 224 this.mapping = mapping; 225 if (getMapping().isDirectToFieldMapping()) { 227 AbstractDirectMapping directMapping = (AbstractDirectMapping)getMapping(); 228 229 if (directMapping.getFieldClassification() == null) { 231 directMapping.setFieldClassification(getDataClass()); 232 } 233 234 if (getObjectClass() == null) { 236 setObjectClass(directMapping.getAttributeClassification()); 237 } 238 } 239 } 240 241 245 protected DatabaseMapping getMapping() { 246 return mapping; 247 } 248 249 255 public boolean isMutable() { 256 return false; 257 } 258 } 259 | Popular Tags |