1 21 package oracle.toplink.essentials.mappings.converters; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import oracle.toplink.essentials.sessions.Session; 28 import oracle.toplink.essentials.mappings.DatabaseMapping; 29 import oracle.toplink.essentials.exceptions.ValidationException; 30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 31 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 32 33 43 public class EnumTypeConverter extends ObjectTypeConverter { 44 private Class m_enumClass; 45 private String m_enumClassName; 46 private boolean m_usesOrdinalValues; 47 48 51 public EnumTypeConverter(DatabaseMapping mapping, String enumClassName, boolean usesOrdinalValues) { 52 super(mapping); 53 54 m_enumClassName = enumClassName; 55 m_usesOrdinalValues = usesOrdinalValues; 56 } 57 58 65 public void convertClassNamesToClasses(ClassLoader classLoader){ 66 try { 67 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 68 try { 69 m_enumClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(m_enumClassName, true, classLoader)); 70 } catch (PrivilegedActionException exception) { 71 throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception.getException()); 72 } 73 } else { 74 m_enumClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(m_enumClassName, true, classLoader); 75 } 76 } catch (ClassNotFoundException exception){ 77 throw ValidationException.classNotFoundWhileConvertingClassNames(m_enumClassName, exception); 78 } 79 } 80 81 84 public void initialize(DatabaseMapping mapping, Session session) { 85 Iterator<Enum > i = EnumSet.allOf(m_enumClass).iterator(); 86 87 while (i.hasNext()) { 88 Enum theEnum = i.next(); 89 90 if (m_usesOrdinalValues) { 91 addConversionValue(theEnum.ordinal(), theEnum); 92 } else { 93 addConversionValue(theEnum.name(), theEnum); 94 } 95 } 96 97 super.initialize(mapping, session); 98 } 99 100 105 public boolean usesOrdinalValues() { 106 return m_usesOrdinalValues; 107 } 108 } 109 | Popular Tags |