View Javadoc

    1   /*
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  package org.apache.commons.vfs;
    18
    19  import java.util.Map;
    20  import java.util.TreeMap;
    21  import java.util.Iterator;
    22
    23  /***
    24   * Container for various authentication data
    25   */
    26  public class UserAuthenticationData
    27  {
    28  	public static class Type implements Comparable
    29  	{
    30  		private final String type;
    31
    32  		public Type(String type)
    33  		{
    34  			this.type = type;
    35  		}
    36
    37  		public boolean equals(Object o)
    38  		{
    39  			if (this == o)
    40  			{
    41  				return true;
    42  			}
    43  			if (o == null || getClass() != o.getClass())
    44  			{
    45  				return false;
    46  			}
    47
    48  			Type type1 = (Type) o;
    49
    50  			if (type != null ? !type.equals(type1.type) : type1.type != null)
    51  			{
    52  				return false;
    53  			}
    54
    55  			return true;
    56  		}
    57
    58  		public int compareTo(Object o)
    59  		{
    60  			Type t = (Type) o;
    61
    62  			return type.compareTo(t.type);
    63  		}
    64  	}
    65
    66  	public static final Type USERNAME = new Type("username");
    67  	public static final Type PASSWORD = new Type("password");
    68  	public static final Type DOMAIN = new Type("domain");
    69
    70  	private Map authenticationData = new TreeMap();
    71
    72  	public UserAuthenticationData()
    73  	{
    74  	}
    75
    76  	/***
    77  	 * set a data to this collection
    78  	 */
    79  	public void setData(Type type, char[] data)
    80  	{
    81  		authenticationData.put(type, data);
    82  	}
    83
    84  	/***
    85  	 * get a data from the collection
    86  	 */
    87  	public char[] getData(Type type)
    88  	{
    89  		return (char[]) authenticationData.get(type);
    90  	}
    91
    92  	/***
    93  	 * deleted all data stored within this authenticator
    94  	 */
    95  	public void cleanup()
    96  	{
    97  		// step 1: nullify character buffers
    98  		Iterator iterAuthenticationData = authenticationData.values().iterator();
    99  		while (iterAuthenticationData.hasNext())
    100 		{
    101 			char[] data = (char[]) iterAuthenticationData.next();
    102 			if (data == null || data.length < 0)
    103 			{
    104 				continue;
    105 			}
    106
    107 			for (int i = 0; i<data.length; i++)
    108 			{
    109 				data[i]=0;
    110 			}
    111 		}
    112 		// step 2: allow data itself to gc
    113 		authenticationData.clear();
    114 	}
    115 }