KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > deployment > ResourcePrincipal


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 package com.sun.enterprise.deployment;
24
25 /**
26  * This class encapsulates the Resource Principal information needed
27  * to access the Resource.
28  *
29  * @author Tony Ng
30  */

31 public class ResourcePrincipal extends PrincipalImpl {
32     private String JavaDoc password;
33
34     static private final int NULL_HASH_CODE = new Integer JavaDoc(1).hashCode();
35
36     // start IASRI 4676199
37
// Mods:
38
// - Adding support for default principal cases where a principal
39
// is not needed to acquire a resource. Ex: when username and
40
// password are set on a jdbc datasource, no principal is need
41
// to call getConnection()
42

43     //used for hashCode()
44
private static final String JavaDoc DEFAULT_USERNAME = "__default__user__name__";
45     //used for hashCode()
46
private static final String JavaDoc DEFAULT_PASSWORD = "__default__password__";
47
48     private boolean defaultPrincipal = false;
49
50     /**
51      * This constructor is used to construct a default principal. a default
52      * principal is used when username and password are not required to
53      * acquire a resource.
54      */

55     public ResourcePrincipal() {
56         super(DEFAULT_USERNAME);
57         this.password = DEFAULT_PASSWORD;
58         defaultPrincipal = true;
59     }
60
61     // end IASRI 4676199
62

63     public ResourcePrincipal(String JavaDoc name, String JavaDoc password) {
64         super(name);
65         this.password = password;
66     }
67
68     public String JavaDoc getPassword() {
69         return password;
70     }
71
72     // start IASRI 4676199
73
/**
74      * @return true if this principal is a default principal
75      * @see ResourcePrincipal()
76      */

77     public boolean isDefault() {
78         return defaultPrincipal;
79     }
80     // end IASRI 4676199
81

82     public boolean equals(Object JavaDoc o) {
83         if (this == o) return true;
84         if (o == null) return false;
85         if (o instanceof ResourcePrincipal) {
86             ResourcePrincipal other = (ResourcePrincipal) o;
87
88             // start IASRI 4676199
89
// handle the default principal case
90
if (isDefault()) {
91                 return other.isDefault();
92             } else if (other.isDefault()) {
93                 return false;
94             }
95             // end IASRI 4676199
96

97             return ((isEqual(getName(), other.getName())) &&
98                     (isEqual(this.password, other.password)));
99         }
100         return false;
101     }
102
103     public int hashCode() {
104         int result = NULL_HASH_CODE;
105         String JavaDoc name = getName();
106         if (name != null) {
107             result += name.hashCode();
108         }
109         if (password != null) {
110             result += password.hashCode();
111         }
112         return result;
113     }
114
115     private boolean isEqual(Object JavaDoc a, Object JavaDoc b) {
116         if (a == null) {
117             return (b == null);
118         } else {
119             return (a.equals(b));
120         }
121     }
122
123 }
124
Popular Tags