() {
+ public AbstractFileSystem run() throws UnsupportedFileSystemException {
+ return AbstractFileSystem.get(uri, conf);
+ }
+ });
+ } catch (InterruptedException ex) {
+ LOG.error(ex);
+ throw new IOException("Failed to get the AbstractFileSystem for path: "
+ + uri, ex);
+ }
+ }
/**
* Protected Static Factory methods for getting a FileContexts
@@ -390,10 +417,23 @@ public final class FileContext {
* @return new FileContext for specified uri
* @throws UnsupportedFileSystemException If the file system with specified is
* not supported
+ * @throws RuntimeException If the file system specified is supported but
+ * could not be instantiated, or if login fails.
*/
public static FileContext getFileContext(final URI defaultFsUri,
final Configuration aConf) throws UnsupportedFileSystemException {
- return getFileContext(AbstractFileSystem.get(defaultFsUri, aConf), aConf);
+ UserGroupInformation currentUser = null;
+ AbstractFileSystem defaultAfs = null;
+ try {
+ currentUser = UserGroupInformation.getCurrentUser();
+ defaultAfs = getAbstractFileSystem(currentUser, defaultFsUri, aConf);
+ } catch (UnsupportedFileSystemException ex) {
+ throw ex;
+ } catch (IOException ex) {
+ LOG.error(ex);
+ throw new RuntimeException(ex);
+ }
+ return getFileContext(defaultAfs, aConf);
}
/**
@@ -477,6 +517,14 @@ public final class FileContext {
return workingDir;
}
+ /**
+ * Gets the ugi in the file-context
+ * @return UserGroupInformation
+ */
+ public UserGroupInformation getUgi() {
+ return ugi;
+ }
+
/**
*
* @return the umask of this FileContext
diff --git a/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java b/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java
index 203485eaa03..0e225325812 100644
--- a/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java
+++ b/src/test/core/org/apache/hadoop/fs/FileContextPermissionBase.java
@@ -18,6 +18,7 @@
package org.apache.hadoop.fs;
import java.io.IOException;
+import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
@@ -25,6 +26,7 @@ import java.util.StringTokenizer;
import junit.framework.Assert;
import org.apache.hadoop.fs.permission.FsPermission;
+import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.util.Shell;
import org.apache.hadoop.util.StringUtils;
import org.junit.After;
@@ -32,6 +34,7 @@ import org.junit.Before;
import org.junit.Test;
import static org.apache.hadoop.fs.FileContextTestHelper.*;
+import static org.junit.Assert.assertEquals;
/**
*
@@ -162,6 +165,22 @@ public abstract class FileContextPermissionBase {
}
finally {cleanupFile(fc, f);}
}
+
+ @Test
+ public void testUgi() throws IOException, InterruptedException {
+
+ UserGroupInformation otherUser = UserGroupInformation
+ .createRemoteUser("otherUser");
+ FileContext newFc = otherUser.doAs(new PrivilegedExceptionAction() {
+
+ public FileContext run() throws Exception {
+ FileContext newFc = FileContext.getFileContext();
+ return newFc;
+ }
+
+ });
+ assertEquals("otherUser",newFc.getUgi().getUserName());
+ }
static List getGroups() throws IOException {
List a = new ArrayList();