KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > gjc > util > SecurityUtils


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 in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
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 Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23  
24 package com.sun.gjc.util;
25
26 import javax.security.auth.Subject JavaDoc;
27 import java.security.AccessController JavaDoc;
28 import java.security.PrivilegedAction JavaDoc;
29 import javax.resource.spi.security.PasswordCredential JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.resource.spi.*;
32 import com.sun.gjc.spi.ConnectionRequestInfo;
33 import com.sun.gjc.common.DataSourceObjectBuilder;
34 import java.util.Set JavaDoc;
35 import java.util.Iterator JavaDoc;
36 import com.sun.enterprise.util.i18n.StringManager;
37 /**
38  * SecurityUtils for Generic JDBC Connector.
39  *
40  * @version 1.0, 02/07/22
41  * @author Evani Sai Surya Kiran
42  */

43 public class SecurityUtils {
44     
45     static private StringManager sm = StringManager.getManager(
46         DataSourceObjectBuilder.class );
47
48     /**
49      * This method returns the <code>PasswordCredential</code> object, given
50      * the <code>ManagedConnectionFactory</code>, subject and the
51      * <code>ConnectionRequestInfo</code>. It first checks if the
52      * <code>ConnectionRequestInfo</code> is null or not. If it is not null,
53      * it constructs a <code>PasswordCredential</code> object with
54      * the user and password fields from the <code>ConnectionRequestInfo</code> and returns this
55      * <code>PasswordCredential</code> object. If the <code>ConnectionRequestInfo</code>
56      * is null, it retrieves the <code>PasswordCredential</code> objects from
57      * the <code>Subject</code> parameter and returns the first
58      * <code>PasswordCredential</code> object which contains a
59      * <code>ManagedConnectionFactory</code>, instance equivalent
60      * to the <code>ManagedConnectionFactory</code>, parameter.
61      *
62      * @param mcf <code>ManagedConnectionFactory</code>
63      * @param subject <code>Subject</code>
64      * @param info <code>ConnectionRequestInfo</code>
65      * @return <code>PasswordCredential</code>
66      * @throws <code>ResourceException</code> generic exception if operation fails
67      * @throws <code>SecurityException</code> if access to the <code>Subject</code> instance is denied
68      */

69     public static PasswordCredential JavaDoc getPasswordCredential(final ManagedConnectionFactory mcf,
70          final Subject JavaDoc subject, javax.resource.spi.ConnectionRequestInfo JavaDoc info) throws ResourceException JavaDoc {
71
72     if (info == null) {
73             if (subject == null) {
74                 return null;
75             } else {
76                 PasswordCredential JavaDoc pc = (PasswordCredential JavaDoc) AccessController.doPrivileged
77                     (new PrivilegedAction JavaDoc() {
78                         public Object JavaDoc run() {
79                             Set JavaDoc passwdCredentialSet = subject.getPrivateCredentials(PasswordCredential JavaDoc.class);
80                             Iterator JavaDoc iter = passwdCredentialSet.iterator();
81                             while (iter.hasNext()) {
82                                 PasswordCredential JavaDoc temp = (PasswordCredential JavaDoc) iter.next();
83                                 if (temp.getManagedConnectionFactory().equals(mcf)) {
84                                     return temp;
85                                 }
86                             }
87                             return null;
88                         }
89                     });
90                 if (pc == null) {
91             String JavaDoc msg = sm.getString( "su.no_passwd_cred");
92                     throw new javax.resource.spi.SecurityException JavaDoc(msg);
93                 } else {
94                     return pc;
95                 }
96             }
97         } else {
98             com.sun.gjc.spi.ConnectionRequestInfo cxReqInfo = (com.sun.gjc.spi.ConnectionRequestInfo) info;
99             PasswordCredential JavaDoc pc = new PasswordCredential JavaDoc(cxReqInfo.getUser(), cxReqInfo.getPassword().toCharArray());
100             pc.setManagedConnectionFactory(mcf);
101             return pc;
102         }
103     }
104     
105     /**
106      * Returns true if two strings are equal; false otherwise
107      *
108      * @param str1 <code>String</code>
109      * @param str2 <code>String</code>
110      * @return true if the two strings are equal
111      * false otherwise
112      */

113     static private boolean isEqual(String JavaDoc str1, String JavaDoc str2) {
114         if (str1 == null) {
115             return (str2 == null);
116         } else {
117             return str1.equals(str2);
118         }
119     }
120
121     /**
122      * Returns true if two <code>PasswordCredential</code> objects are equal; false otherwise
123      *
124      * @param pC1 <code>PasswordCredential</code>
125      * @param pC2 <code>PasswordCredential</code>
126      * @return true if the two PasswordCredentials are equal
127      * false otherwise
128      */

129     static public boolean isPasswordCredentialEqual(PasswordCredential JavaDoc pC1, PasswordCredential JavaDoc pC2) {
130         if (pC1 == pC2)
131             return true;
132         if(pC1 == null || pC2 == null)
133             return (pC1 == pC2);
134         if (!isEqual(pC1.getUserName(), pC2.getUserName())) {
135             return false;
136         }
137         String JavaDoc p1 = null;
138         String JavaDoc p2 = null;
139         if (pC1.getPassword() != null) {
140             p1 = new String JavaDoc(pC1.getPassword());
141         }
142         if (pC2.getPassword() != null) {
143             p2 = new String JavaDoc(pC2.getPassword());
144         }
145         return (isEqual(p1, p2));
146     }
147 }
148
Popular Tags