KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > jcr > SimpleCredentials


1 /*
2  * $Id: SimpleCredentials.java,v 1.2 2004/07/24 00:16:21 benjmestrallet Exp $
3  *
4  * Copyright 2002-2004 Day Management AG, Switzerland.
5  *
6  * Licensed under the Day RI License, Version 2.0 (the "License"),
7  * as a reference implementation of the following specification:
8  *
9  * Content Repository API for Java Technology, revision 0.12
10  * <http://www.jcp.org/en/jsr/detail?id=170>
11  *
12  * You may not use this file except in compliance with the License.
13  * You may obtain a copy of the License files at
14  *
15  * http://www.day.com/content/en/licenses/day-ri-license-2.0
16  * http://www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an "AS IS" BASIS,
20  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */

24 package javax.jcr;
25
26 import java.io.Serializable JavaDoc;
27 import java.util.HashMap JavaDoc;
28
29 /**
30  * <code>SimpleCredentials</code> implements the <code>Credentials</code>
31  * interface and represents simple user ID/password credentials.
32  *
33  * @author Stefan Guggisberg
34  */

35 public final class SimpleCredentials implements Credentials, Serializable JavaDoc {
36
37   private final String JavaDoc userId;
38   private final char[] password;
39   private final HashMap JavaDoc attributes = new HashMap JavaDoc();
40
41   /**
42    * Create a new <code>SimpleCredentials</code> object, given a user ID
43    * and password.
44    * <p/>
45    * Note that the given user password is cloned before it is stored
46    * in the new <code>SimpleCredentials</code> object. This should
47    * avoid the risk of having unnecessary references to password data
48    * lying around in memory.
49    * <p/>
50    *
51    * @param userId the user ID
52    * @param password the user's password
53    */

54   public SimpleCredentials(String JavaDoc userId, char[] password) {
55     this.userId = userId;
56     this.password = (char[]) password.clone();
57   }
58
59   /**
60    * Returns the user password.
61    * <p/>
62    * Note that this method returns a reference to the password.
63    * It is the caller's responsibility to zero out the password information
64    * after it is no longer needed.
65    *
66    * @return the password
67    */

68   public char[] getPassword() {
69     return password;
70   }
71
72   /**
73    * Returns the user ID.
74    *
75    * @return the user ID.
76    */

77   public String JavaDoc getUserId() {
78     return userId;
79   }
80
81   /**
82    * Stores an attribute in this credentials instance.
83    *
84    * @param name a <code>String</code> specifying the name of the attribute
85    * @param value the <code>Object</code> to be stored
86    */

87   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
88 // name cannot be null
89
if (name == null) {
90       throw new IllegalArgumentException JavaDoc("name cannot be null");
91     }
92
93 // null value is the same as removeAttribute()
94
if (value == null) {
95       removeAttribute(name);
96       return;
97     }
98
99     synchronized (attributes) {
100       attributes.put(name, value);
101     }
102   }
103
104   /**
105    * Returns the value of the named attribute as an <code>Object</code>,
106    * or <code>null</code> if no attribute of the given name exists.
107    *
108    * @param name a <code>String</code> specifying the name of the attribute
109    * @return an <code>Object</code> containing the value of the attribute,
110    * or <code>null</code> if the attribute does not exist
111    */

112   public Object JavaDoc getAttribute(String JavaDoc name) {
113     synchronized (attributes) {
114       return (attributes.get(name));
115     }
116   }
117
118   /**
119    * Removes an attribute from this credentials instance.
120    *
121    * @param name a <code>String</code> specifying the name of the attribute
122    * to remove
123    */

124   public void removeAttribute(String JavaDoc name) {
125     synchronized (attributes) {
126       attributes.remove(name);
127     }
128   }
129
130   /**
131    * Returns the names of the attributes available to this
132    * credentials instance. This method returns an empty array
133    * if the credentials instance has no attributes available to it.
134    * <p/>
135    * <b>Level 1 and 2</b>
136    * <p/>
137    *
138    * @return a string array containing the names of the stored attributes
139    */

140   public String JavaDoc[] getAttributeNames() {
141     synchronized (attributes) {
142       return (String JavaDoc[]) attributes.keySet().toArray(new String JavaDoc[attributes.keySet().size()]);
143     }
144   }
145 }
146
Popular Tags