1 21 package oracle.toplink.essentials.internal.security; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 27 import oracle.toplink.essentials.exceptions.ValidationException; 28 import oracle.toplink.essentials.internal.helper.ConversionManager; 29 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 30 31 38 public class SecurableObjectHolder { 39 40 41 private final static String JCE_ENCRYPTION_CLASS_NAME = "oracle.toplink.essentials.internal.security.JCEEncryptor"; 42 43 44 private String m_securableClassName; 45 46 47 private Securable m_securableObject; 48 49 public SecurableObjectHolder() { 50 this(null); 51 } 52 53 public SecurableObjectHolder(String securableClassName) { 54 m_securableObject = null; 55 m_securableClassName = securableClassName; 56 } 57 58 public void setEncryptionClassName(String securableClassName) { 59 m_securableClassName = securableClassName; 60 } 61 62 public Securable getSecurableObject() { 63 if (m_securableObject == null) { 64 initSecurableObject(); 65 } 66 67 return m_securableObject; 68 } 69 70 public boolean hasSecurableObject() { 71 return m_securableObject != null; 72 } 73 74 79 private void initSecurableObject() { 80 boolean initPassThroughEncryptor = false; 81 82 if (m_securableClassName == null) { 83 initPassThroughEncryptor = true; 89 m_securableClassName = JCE_ENCRYPTION_CLASS_NAME; 90 } 91 92 try { 93 ConversionManager cm = ConversionManager.getDefaultManager(); 94 Class securableClass = (Class )cm.convertObject(m_securableClassName, Class .class); 95 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 96 try { 97 m_securableObject = (Securable)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(securableClass)); 98 } catch (PrivilegedActionException exception) { 99 throw exception.getException(); 100 } 101 } else { 102 m_securableObject = (Securable)PrivilegedAccessHelper.newInstanceFromClass(securableClass); 103 } 104 } catch (Throwable e) { 105 if (initPassThroughEncryptor) { m_securableObject = new PassThroughEncryptor(); 107 } else { 108 throw ValidationException.invalidEncryptionClass(m_securableClassName, e); 109 } 110 } 111 } 112 113 119 private class PassThroughEncryptor implements Securable { 120 public String encryptPassword(String pswd) { 121 return pswd; 122 } 123 124 public String decryptPassword(String encryptedPswd) { 125 return encryptedPswd; 126 } 127 } 128 } 129 | Popular Tags |