You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by jo...@apache.org on 2013/02/27 09:18:10 UTC
svn commit: r1450670 - in /commons/proper/vfs/trunk/core/src:
main/java/org/apache/commons/vfs2/ main/java/org/apache/commons/vfs2/util/
test/java/org/apache/commons/vfs2/
Author: joehni
Date: Wed Feb 27 08:18:09 2013
New Revision: 1450670
URL: http://svn.apache.org/r1450670
Log:
Open UserAuthenticationData for arbitrary value types (VFS-466). Change requires new getter and setter of the data, deprecate old methods.
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java (with props)
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/UserAuthenticationData.java
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/UserAuthenticatorUtils.java
Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/UserAuthenticationData.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/UserAuthenticationData.java?rev=1450670&r1=1450669&r2=1450670&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/UserAuthenticationData.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/UserAuthenticationData.java Wed Feb 27 08:18:09 2013
@@ -16,9 +16,10 @@
*/
package org.apache.commons.vfs2;
-import java.util.Iterator;
+import java.lang.reflect.Array;
+import java.util.Arrays;
+import java.util.HashMap;
import java.util.Map;
-import java.util.TreeMap;
/**
* Contains various authentication data.
@@ -32,15 +33,43 @@ public class UserAuthenticationData
{
/** The type name */
private final String type;
+ private final Class<?> clazz;
/**
* Creates a new Type.
*
- * @param type the type
+ * @param type the type name
+ * @deprecated As of 2.1 use {@link #Type(String, Class)}
*/
+ @Deprecated
public Type(final String type)
{
+ this(type, char[].class);
+ }
+
+ /**
+ * Creates a new Type.
+ *
+ * @param type the type name
+ * @param clazz the class type
+ * @since 2.1
+ */
+ public Type(final String type, final Class<?> clazz)
+ {
this.type = type;
+ this.clazz = clazz;
+ }
+
+ /**
+ * Test the provided class type for compatibility.
+ *
+ * @param clz the class type to check
+ * @return {@code true} if class type is assignable
+ * @since 2.1
+ */
+ public final boolean isAssignable(final Class<?> clz)
+ {
+ return clazz.isAssignableFrom(clz);
}
@Override
@@ -55,9 +84,12 @@ public class UserAuthenticationData
return false;
}
- final Type type1 = (Type) o;
-
- if (type != null ? !type.equals(type1.type) : type1.type != null)
+ final Type other = (Type) o;
+ if (type != null ? !type.equals(other.type) : other.type != null)
+ {
+ return false;
+ }
+ if (clazz != null ? (other.clazz == null || !clazz.getName().equals(other.clazz.getName())) : other.clazz != null)
{
return false;
}
@@ -78,7 +110,11 @@ public class UserAuthenticationData
@Override
public int hashCode()
{
- return type != null ? type.hashCode() : 0;
+ final int prime = 7;
+ int result = 1;
+ result = prime * result + ((clazz == null) ? 0 : clazz.getName().hashCode());
+ result = prime * result + ((type == null) ? 0 : type.hashCode());
+ return result;
}
/**
@@ -93,16 +129,16 @@ public class UserAuthenticationData
}
/** The user name. */
- public static final Type USERNAME = new Type("username");
+ public static final Type USERNAME = new Type("username", char[].class);
/** The password. */
- public static final Type PASSWORD = new Type("password");
+ public static final Type PASSWORD = new Type("password", char[].class);
/** The user's domain. */
- public static final Type DOMAIN = new Type("domain");
+ public static final Type DOMAIN = new Type("domain", char[].class);
/** The authentication data. */
- private final Map<Type, char[]> authenticationData = new TreeMap<Type, char[]>();
+ private final Map<Type, Object> authenticationData = new HashMap<Type, Object>();
/**
* Creates a new uninitialized instance.
@@ -116,20 +152,65 @@ public class UserAuthenticationData
* Sets a data to this collection.
* @param type The Type to add
* @param data The data associated with the Type
+ * @deprecated As of 2.1 use {@link #setAuthData(Type, Object)}
*/
+ @Deprecated
public void setData(final Type type, final char[] data)
{
- authenticationData.put(type, data);
+ setAuthData(type, data);
}
/**
* Gets a data from the collection.
* @param type The Type to retrieve.
* @return a character array containing the data associated with the type.
+ * @deprecated As of 2.1 use {@link #getAuthData(Type)}
*/
+ @Deprecated
public char[] getData(final Type type)
{
- return authenticationData.get(type);
+ return getAuthData(type);
+ }
+
+ /**
+ * Sets a data to this collection.
+ * @param type The Type to add
+ * @param data The data associated with the Type
+ * @return {@code true} if the provided data is compatible with the Type
+ * @since 2.1
+ */
+ public <T> boolean setAuthData(final Type type, final T data)
+ {
+ if (data == null || type.isAssignable(data.getClass()))
+ {
+ authenticationData.put(type, data);
+ return true;
+ }
+ return false;
+ }
+
+ /**
+ * Gets a data from the collection.
+ * @param type The Type to retrieve.
+ * @return the data associated with the Type.
+ * @since 2.1
+ */
+ public <T> T getAuthData(final Type type)
+ {
+ final T result;
+ Object data = authenticationData.get(type);
+ if (data != null && type.isAssignable(data.getClass()))
+ {
+ @SuppressWarnings("unchecked")
+ T checked = (T) data;
+ result = checked;
+ }
+ else
+ {
+ result = null;
+ }
+
+ return result;
}
/**
@@ -137,21 +218,63 @@ public class UserAuthenticationData
*/
public void cleanup()
{
- // step 1: nullify character buffers
- final Iterator<char[]> iterAuthenticationData = authenticationData.values().iterator();
- while (iterAuthenticationData.hasNext())
+ for(Object data : authenticationData.values())
{
- final char[] data = iterAuthenticationData.next();
- if (data == null || data.length < 0)
+ if (data == null)
{
continue;
}
-
- for (int i = 0; i < data.length; i++)
+
+ // step 1: nullify arrays
+ if (data.getClass().isArray())
{
- data[i] = 0;
+ int length = Array.getLength(data);
+ if (length == 0)
+ {
+ continue;
+ }
+ if (data.getClass().getComponentType().isPrimitive())
+ {
+ if (data.getClass() == char[].class)
+ {
+ Arrays.fill((char[])data, '\0');
+ }
+ else if (data.getClass() == byte[].class)
+ {
+ Arrays.fill((byte[])data, (byte)0);
+ }
+ else if (data.getClass() == short[].class)
+ {
+ Arrays.fill((short[])data, (short)0);
+ }
+ else if (data.getClass() == int[].class)
+ {
+ Arrays.fill((int[])data, 0);
+ }
+ else if (data.getClass() == long[].class)
+ {
+ Arrays.fill((long[])data, 0L);
+ }
+ else if (data.getClass() == float[].class)
+ {
+ Arrays.fill((float[])data, 0.0f);
+ }
+ else if (data.getClass() == double[].class)
+ {
+ Arrays.fill((double[])data, 0.0);
+ }
+ else if (data.getClass() == boolean[].class)
+ {
+ Arrays.fill((boolean[])data, false);
+ }
+ }
+ else
+ {
+ Arrays.fill((Object[])data, null);
+ }
}
}
+
// step 2: allow data itself to gc
authenticationData.clear();
}
Modified: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/UserAuthenticatorUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/UserAuthenticatorUtils.java?rev=1450670&r1=1450669&r2=1450670&view=diff
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/UserAuthenticatorUtils.java (original)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/util/UserAuthenticatorUtils.java Wed Feb 27 08:18:09 2013
@@ -38,12 +38,35 @@ public final class UserAuthenticatorUtil
* @param type The type of the element to retrieve.
* @param overriddenValue The default value.
* @return The data of the given type as a character array or null if the data is not available.
+ * @deprecated As of 2.1 use {@link #getAuthData(UserAuthenticationData, org.apache.commons.vfs2.UserAuthenticationData.Type, Object)} instead
*/
+ @Deprecated
public static char[] getData(final UserAuthenticationData data, final UserAuthenticationData.Type type,
final char[] overriddenValue)
{
+ return getAuthData(data, type, overriddenValue);
+ }
+
+ /**
+ * Gets data of given type from the UserAuthenticationData or null if there is no data or data
+ * of this type available.
+ *
+ * @param data The UserAuthenticationData.
+ * @param type The type of the element to retrieve.
+ * @param overriddenValue The default value.
+ * @return The data of the given type as a character array or null if the data is not available.
+ * @since 2.1
+ */
+ public static <T, U extends T> T getAuthData(final UserAuthenticationData data, final UserAuthenticationData.Type type,
+ final U overriddenValue)
+ {
if (overriddenValue != null)
{
+ if (!type.isAssignable(overriddenValue.getClass()))
+ {
+ throw new IllegalArgumentException("overriddenValue");
+ }
+
return overriddenValue;
}
@@ -52,7 +75,7 @@ public final class UserAuthenticatorUtil
return null;
}
- return data.getData(type);
+ return data.getAuthData(type);
}
/**
Added: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java?rev=1450670&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java (added)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java Wed Feb 27 08:18:09 2013
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class UserAuthenticationDataTestCase
+{
+ @Test
+ public void testCharacterBasedDataWithLegacyMethods()
+ {
+ UserAuthenticationData data = new UserAuthenticationData();
+ char[] array = "PMC".toCharArray();
+ data.setData(UserAuthenticationData.USERNAME, array);
+ data.setData(UserAuthenticationData.DOMAIN, "Apache".toCharArray());
+ assertSame(array, data.getData(UserAuthenticationData.USERNAME));
+ assertArrayEquals("Apache".toCharArray(), data.getData(UserAuthenticationData.DOMAIN));
+ data.setData(UserAuthenticationData.DOMAIN, "Apache Commons".toCharArray());
+ assertArrayEquals("Apache Commons".toCharArray(), data.getData(UserAuthenticationData.DOMAIN));
+ assertNull(data.getData(UserAuthenticationData.PASSWORD));
+
+ data.cleanup();
+ assertNull(data.getData(UserAuthenticationData.USERNAME));
+ assertNull(data.getData(UserAuthenticationData.DOMAIN));
+ char[] nulls = {0,0,0};
+ assertArrayEquals(nulls, array);
+ }
+
+ @Test
+ public void testCharacterBasedData()
+ {
+ UserAuthenticationData data = new UserAuthenticationData();
+ char[] array = "PMC".toCharArray();
+ data.setAuthData(UserAuthenticationData.USERNAME, array);
+ data.setAuthData(UserAuthenticationData.DOMAIN, "Apache".toCharArray());
+ assertSame(array, data.getAuthData(UserAuthenticationData.USERNAME));
+ assertArrayEquals("Apache".toCharArray(), data.<char[]>getAuthData(UserAuthenticationData.DOMAIN));
+ data.setAuthData(UserAuthenticationData.DOMAIN, "Apache Commons".toCharArray());
+ assertArrayEquals("Apache Commons".toCharArray(), data.<char[]>getAuthData(UserAuthenticationData.DOMAIN));
+ assertNull(data.getAuthData(UserAuthenticationData.PASSWORD));
+
+ data.cleanup();
+ assertNull(data.getAuthData(UserAuthenticationData.USERNAME));
+ assertNull(data.getAuthData(UserAuthenticationData.DOMAIN));
+ char[] nulls = {0,0,0};
+ assertArrayEquals(nulls, array);
+ }
+
+ @Test
+ public void testCustomTypeWithArray()
+ {
+ UserAuthenticationData.Type type = new UserAuthenticationData.Type("JUNIT", UserAuthenticationDataTestCase[].class);
+ UserAuthenticationData data = new UserAuthenticationData();
+ UserAuthenticationDataTestCase[] array = { this };
+ data.setAuthData(type, array);
+ assertSame(array, data.getAuthData(type));
+
+ data.cleanup();
+ UserAuthenticationDataTestCase[] nulls = { null };
+ assertArrayEquals(nulls, array);
+ }
+
+ @Test
+ public void testCustomTypeWithHierarchy()
+ {
+ UserAuthenticationData.Type type = new UserAuthenticationData.Type("JUNIT", CharSequence.class);
+ UserAuthenticationData data = new UserAuthenticationData();
+ assertTrue(data.setAuthData(type, "test"));
+ assertEquals("test", data.getAuthData(type));
+ assertFalse(data.setAuthData(type, Integer.valueOf(42)));
+ assertEquals("test", data.getAuthData(type));
+ assertTrue(data.setAuthData(type, null));
+ assertNull(data.getAuthData(type));
+ }
+}
Propchange: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/UserAuthenticationDataTestCase.java
------------------------------------------------------------------------------
svn:keywords = Author Id HeadURL Revision