KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > security > SecurableObjectHolder


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.security;
23
24 import java.security.AccessController JavaDoc;
25 import java.security.PrivilegedActionException JavaDoc;
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 /**
32  * Holder of a SecurableObject. Securable objects should not be held onto
33  * directly, instead they should be accessed via this holder.
34  *
35  * @author Guy Pelletier
36  * @date June 26, 2003
37  */

38 public class SecurableObjectHolder {
39
40     /** The JCE encryption class name */
41     private final static String JavaDoc JCE_ENCRYPTION_CLASS_NAME = "oracle.toplink.essentials.internal.security.JCEEncryptor";
42
43     /** The encryption class name **/
44     private String JavaDoc m_securableClassName;
45
46     /** The actual encryption object **/
47     private Securable m_securableObject;
48
49     public SecurableObjectHolder() {
50         this(null);
51     }
52
53     public SecurableObjectHolder(String JavaDoc securableClassName) {
54         m_securableObject = null;
55         m_securableClassName = securableClassName;
56     }
57
58     public void setEncryptionClassName(String JavaDoc 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     /**
75        * Convert a String into a Securable object
76        * Class name must be fully qualified, eg. oracle.toplink.essentials.internal.security.JCEEncryptor
77      * Default is the JCEEncryptor
78        */

79     private void initSecurableObject() {
80         boolean initPassThroughEncryptor = false;
81
82         if (m_securableClassName == null) {
83             // Since we are defaulting, hence, assuming they can initialize the JCE
84
// libraries, if the init fails, this flag tells us to assume no encryption.
85
// However, if the JCE init does work, the JCEEncryptor will need to
86
// determine that a password was not encrypted by it, therefore, assume
87
// clear text. See JCEEncryptor.
88
initPassThroughEncryptor = true;
89             m_securableClassName = JCE_ENCRYPTION_CLASS_NAME;
90         }
91
92         try {
93             ConversionManager cm = ConversionManager.getDefaultManager();
94             Class JavaDoc securableClass = (Class JavaDoc)cm.convertObject(m_securableClassName, Class JavaDoc.class);
95             if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){
96                 try {
97                     m_securableObject = (Securable)AccessController.doPrivileged(new PrivilegedNewInstanceFromClass(securableClass));
98                 } catch (PrivilegedActionException JavaDoc exception) {
99                     throw exception.getException();
100                 }
101             } else {
102                 m_securableObject = (Securable)PrivilegedAccessHelper.newInstanceFromClass(securableClass);
103             }
104         } catch (Throwable JavaDoc e) {
105             if (initPassThroughEncryptor) {// default failed, so perform no encryption.
106
m_securableObject = new PassThroughEncryptor();
107             } else {
108                 throw ValidationException.invalidEncryptionClass(m_securableClassName, e);
109             }
110         }
111     }
112
113     /*
114      * If we default to JCE and the initialization fails, our fall back is to do
115      * no encryption. This covers the case where the user is running against JDK 1.3
116      * At runtime, no encryption will be made and the passwords will be assummed to
117      * be clear text.
118      */

119     private class PassThroughEncryptor implements Securable {
120         public String JavaDoc encryptPassword(String JavaDoc pswd) {
121             return pswd;
122         }
123
124         public String JavaDoc decryptPassword(String JavaDoc encryptedPswd) {
125             return encryptedPswd;
126         }
127     }
128 }
129
Popular Tags