source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/security/UserGroupInformation.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.2 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18package org.apache.hadoop.security;
19
20import java.io.IOException;
21import java.security.AccessController;
22import java.security.Principal;
23import java.util.Set;
24
25import javax.security.auth.Subject;
26import javax.security.auth.login.LoginException;
27
28import org.apache.commons.logging.Log;
29import org.apache.commons.logging.LogFactory;
30import org.apache.hadoop.conf.Configuration;
31import org.apache.hadoop.io.Writable;
32
33/** A {@link Writable} abstract class for storing user and groups information.
34 */
35public abstract class UserGroupInformation implements Writable, Principal {
36  public static final Log LOG = LogFactory.getLog(UserGroupInformation.class);
37  private static UserGroupInformation LOGIN_UGI = null;
38 
39  private static final ThreadLocal<Subject> currentUser =
40    new ThreadLocal<Subject>();
41 
42  /** @return the {@link UserGroupInformation} for the current thread */ 
43  public static UserGroupInformation getCurrentUGI() {
44    Subject user = getCurrentUser();
45   
46    if (user == null) {
47      user = currentUser.get();
48      if (user == null) {
49        return null;
50      }
51    }
52   
53    Set<UserGroupInformation> ugiPrincipals = 
54      user.getPrincipals(UserGroupInformation.class);
55   
56    UserGroupInformation ugi = null;
57    if (ugiPrincipals != null && ugiPrincipals.size() == 1) {
58      ugi = ugiPrincipals.iterator().next();
59      if (ugi == null) {
60        throw new RuntimeException("Cannot find _current user_ UGI in the Subject!");
61      }
62    } else {
63      throw new RuntimeException("Cannot resolve current user from subject, " +
64                                       "which had " + ugiPrincipals.size() + 
65                                       " UGI principals!");
66    }
67    return ugi;
68  }
69
70  /**
71   * Set the {@link UserGroupInformation} for the current thread
72   * @deprecated Use {@link #setCurrentUser(UserGroupInformation)}
73   */ 
74  @Deprecated
75  public static void setCurrentUGI(UserGroupInformation ugi) {
76    setCurrentUser(ugi);
77  }
78
79  /**
80   * Return the current user <code>Subject</code>.
81   * @return the current user <code>Subject</code>
82   */
83  static Subject getCurrentUser() {
84    return Subject.getSubject(AccessController.getContext());
85  }
86 
87  /**
88   * Set the {@link UserGroupInformation} for the current thread
89   * WARNING - This method should be used only in test cases and other exceptional
90   * cases!
91   * @param ugi {@link UserGroupInformation} for the current thread
92   */
93  public static void setCurrentUser(UserGroupInformation ugi) {
94    Subject user = SecurityUtil.getSubject(ugi);
95    currentUser.set(user);
96  }
97 
98  /** Get username
99   *
100   * @return the user's name
101   */
102  public abstract String getUserName();
103 
104  /** Get the name of the groups that the user belong to
105   *
106   * @return an array of group names
107   */
108  public abstract String[] getGroupNames();
109
110  /** Login and return a UserGroupInformation object. */
111  public static UserGroupInformation login(Configuration conf
112      ) throws LoginException {
113    if (LOGIN_UGI == null) {
114      LOGIN_UGI = UnixUserGroupInformation.login(conf);
115    }
116    return LOGIN_UGI;
117  }
118
119  /** Read a {@link UserGroupInformation} from conf */
120  public static UserGroupInformation readFrom(Configuration conf
121      ) throws IOException {
122    try {
123      return UnixUserGroupInformation.readFromConf(conf,
124        UnixUserGroupInformation.UGI_PROPERTY_NAME);
125    } catch (LoginException e) {
126      throw (IOException)new IOException().initCause(e);
127    }
128  }
129}
Note: See TracBrowser for help on using the repository browser.