You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by to...@apache.org on 2012/05/11 17:04:25 UTC
svn commit: r1337200 - in
/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src:
main/java/org/apache/hadoop/security/token/
test/java/org/apache/hadoop/security/token/ test/resources/META-INF/
test/resources/META-INF/services/
Author: tomwhite
Date: Fri May 11 15:04:24 2012
New Revision: 1337200
URL: http://svn.apache.org/viewvc?rev=1337200&view=rev
Log:
Merge -r 1337198:1337199 from trunk to branch-2. Fixes: MAPREDUCE-4148.
Added:
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/resources/META-INF/
- copied from r1337199, hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/resources/META-INF/
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/resources/META-INF/services/
- copied from r1337199, hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/resources/META-INF/services/
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/resources/META-INF/services/org.apache.hadoop.security.token.TokenIdentifier
- copied unchanged from r1337199, hadoop/common/trunk/hadoop-common-project/hadoop-common/src/test/resources/META-INF/services/org.apache.hadoop.security.token.TokenIdentifier
Modified:
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java
hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java
Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java?rev=1337200&r1=1337199&r2=1337200&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java (original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/security/token/Token.java Fri May 11 15:04:24 2012
@@ -18,10 +18,15 @@
package org.apache.hadoop.security.token;
+import com.google.common.collect.Maps;
+
+import java.io.ByteArrayInputStream;
import java.io.DataInput;
+import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
+import java.util.Map;
import java.util.ServiceLoader;
import org.apache.commons.codec.binary.Base64;
@@ -37,6 +42,7 @@ import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.io.WritableUtils;
+import org.apache.hadoop.util.ReflectionUtils;
/**
* The client-side form of the token.
@@ -45,6 +51,9 @@ import org.apache.hadoop.io.WritableUtil
@InterfaceStability.Evolving
public class Token<T extends TokenIdentifier> implements Writable {
public static final Log LOG = LogFactory.getLog(Token.class);
+
+ private static Map<Text, Class<? extends TokenIdentifier>> tokenKindMap;
+
private byte[] identifier;
private byte[] password;
private Text kind;
@@ -100,13 +109,49 @@ public class Token<T extends TokenIdenti
}
/**
- * Get the token identifier
- * @return the token identifier
+ * Get the token identifier's byte representation
+ * @return the token identifier's byte representation
*/
public byte[] getIdentifier() {
return identifier;
}
+ private static synchronized Class<? extends TokenIdentifier>
+ getClassForIdentifier(Text kind) {
+ if (tokenKindMap == null) {
+ tokenKindMap = Maps.newHashMap();
+ for (TokenIdentifier id : ServiceLoader.load(TokenIdentifier.class)) {
+ tokenKindMap.put(id.getKind(), id.getClass());
+ }
+ }
+ Class<? extends TokenIdentifier> cls = tokenKindMap.get(kind);
+ if (cls == null) {
+ LOG.warn("Cannot find class for token kind " + kind);
+ return null;
+ }
+ return cls;
+ }
+
+ /**
+ * Get the token identifier object, or null if it could not be constructed
+ * (because the class could not be loaded, for example).
+ * @return the token identifier, or null
+ * @throws IOException
+ */
+ @SuppressWarnings("unchecked")
+ public T decodeIdentifier() throws IOException {
+ Class<? extends TokenIdentifier> cls = getClassForIdentifier(getKind());
+ if (cls == null) {
+ return null;
+ }
+ TokenIdentifier tokenIdentifier = ReflectionUtils.newInstance(cls, null);
+ ByteArrayInputStream buf = new ByteArrayInputStream(identifier);
+ DataInputStream in = new DataInputStream(buf);
+ tokenIdentifier.readFields(in);
+ in.close();
+ return (T) tokenIdentifier;
+ }
+
/**
* Get the token password/secret
* @return the token password/secret
@@ -260,16 +305,31 @@ public class Token<T extends TokenIdenti
buffer.append(num);
}
}
+
+ private void identifierToString(StringBuilder buffer) {
+ T id = null;
+ try {
+ id = decodeIdentifier();
+ } catch (IOException e) {
+ // handle in the finally block
+ } finally {
+ if (id != null) {
+ buffer.append("(").append(id).append(")");
+ } else {
+ addBinaryBuffer(buffer, identifier);
+ }
+ }
+ }
@Override
public String toString() {
StringBuilder buffer = new StringBuilder();
- buffer.append("Ident: ");
- addBinaryBuffer(buffer, identifier);
- buffer.append(", Kind: ");
+ buffer.append("Kind: ");
buffer.append(kind.toString());
buffer.append(", Service: ");
buffer.append(service.toString());
+ buffer.append(", Ident: ");
+ identifierToString(buffer);
return buffer.toString();
}
Modified: hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java?rev=1337200&r1=1337199&r2=1337200&view=diff
==============================================================================
--- hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java (original)
+++ hadoop/common/branches/branch-2/hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/token/TestToken.java Fri May 11 15:04:24 2012
@@ -18,11 +18,15 @@
package org.apache.hadoop.security.token;
+import static junit.framework.Assert.assertEquals;
+
import java.io.*;
import java.util.Arrays;
import org.apache.hadoop.io.*;
import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
+import org.apache.hadoop.security.token.delegation.TestDelegationToken.TestDelegationTokenIdentifier;
+import org.apache.hadoop.security.token.delegation.TestDelegationToken.TestDelegationTokenSecretManager;
import junit.framework.TestCase;
@@ -94,5 +98,20 @@ public class TestToken extends TestCase
checkUrlSafe(encode);
}
}
+
+ public void testDecodeIdentifier() throws IOException {
+ TestDelegationTokenSecretManager secretManager =
+ new TestDelegationTokenSecretManager(0, 0, 0, 0);
+ secretManager.startThreads();
+ TestDelegationTokenIdentifier id = new TestDelegationTokenIdentifier(
+ new Text("owner"), new Text("renewer"), new Text("realUser"));
+
+ Token<TestDelegationTokenIdentifier> token =
+ new Token<TestDelegationTokenIdentifier>(id, secretManager);
+ TokenIdentifier idCopy = token.decodeIdentifier();
+
+ assertNotSame(id, idCopy);
+ assertEquals(id, idCopy);
+ }
}