Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
Role.java
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.catalog;
16 
17 import java.util.List;
18 import java.util.Set;
19 import java.util.concurrent.atomic.AtomicInteger;
20 
21 import com.cloudera.impala.thrift.TCatalogObjectType;
22 import com.cloudera.impala.thrift.TRole;
23 import com.google.common.base.Preconditions;
24 import com.google.common.collect.Lists;
25 import com.google.common.collect.Sets;
26 
30 public class Role implements CatalogObject {
31  private final TRole role_;
32  // The last role ID assigned, starts at 0.
33  private static AtomicInteger roleId_ = new AtomicInteger(0);
35 
36  private final CatalogObjectCache<RolePrivilege> rolePrivileges_ =
37  new CatalogObjectCache<RolePrivilege>();
38 
39  public Role(String roleName, Set<String> grantGroups) {
40  role_ = new TRole();
41  role_.setRole_name(roleName);
42  role_.setRole_id(roleId_.incrementAndGet());
43  role_.setGrant_groups(Lists.newArrayList(grantGroups));
44  }
45 
46  private Role(TRole role) {
47  role_ = role;
48  }
49 
54  public boolean addPrivilege(RolePrivilege privilege) {
55  return rolePrivileges_.add(privilege);
56  }
57 
62  public List<RolePrivilege> getPrivileges() {
63  return Lists.newArrayList(rolePrivileges_.getValues());
64  }
65 
70  public Set<String> getPrivilegeNames() {
71  return Sets.newHashSet(rolePrivileges_.keySet());
72  }
73 
78  public RolePrivilege getPrivilege(String privilegeName) {
79  return rolePrivileges_.get(privilegeName);
80  }
81 
86  public RolePrivilege removePrivilege(String privilegeName) {
87  return rolePrivileges_.remove(privilegeName);
88  }
89 
93  public synchronized void addGrantGroup(String groupName) {
94  if (role_.getGrant_groups().contains(groupName)) return;
95  role_.addToGrant_groups(groupName);
96  }
97 
101  public synchronized void removeGrantGroup(String groupName) {
102  role_.getGrant_groups().remove(groupName);
103  // Should never have duplicates in the list of groups.
104  Preconditions.checkState(!role_.getGrant_groups().contains(groupName));
105  }
106 
110  public TRole toThrift() {
111  return role_;
112  }
113 
117  public static Role fromThrift(TRole thriftRole) {
118  return new Role(thriftRole);
119  }
120 
125  public Set<String> getGrantGroups() {
126  return Sets.newHashSet(role_.getGrant_groups());
127  }
128  @Override
129  public TCatalogObjectType getCatalogObjectType() { return TCatalogObjectType.ROLE; }
130  @Override
131  public String getName() { return role_.getRole_name(); }
132  public int getId() { return role_.getRole_id(); }
133  @Override
134  public synchronized long getCatalogVersion() { return catalogVersion_; }
135  @Override
136  public synchronized void setCatalogVersion(long newVersion) {
137  catalogVersion_ = newVersion;
138  }
139  @Override
140  public boolean isLoaded() { return true; }
141 }
List< RolePrivilege > getPrivileges()
Definition: Role.java:62
Role(String roleName, Set< String > grantGroups)
Definition: Role.java:39
synchronized void addGrantGroup(String groupName)
Definition: Role.java:93
boolean addPrivilege(RolePrivilege privilege)
Definition: Role.java:54
final CatalogObjectCache< RolePrivilege > rolePrivileges_
Definition: Role.java:36
static final long INITIAL_CATALOG_VERSION
Definition: Catalog.java:57
synchronized long getCatalogVersion()
Definition: Role.java:134
static Role fromThrift(TRole thriftRole)
Definition: Role.java:117
static AtomicInteger roleId_
Definition: Role.java:33
Set< String > getPrivilegeNames()
Definition: Role.java:70
Set< String > getGrantGroups()
Definition: Role.java:125
RolePrivilege getPrivilege(String privilegeName)
Definition: Role.java:78
synchronized void removeGrantGroup(String groupName)
Definition: Role.java:101
TCatalogObjectType getCatalogObjectType()
Definition: Role.java:129
RolePrivilege removePrivilege(String privilegeName)
Definition: Role.java:86
synchronized void setCatalogVersion(long newVersion)
Definition: Role.java:136