blob: 419d71fada0958ed2505b129f4927049772b6052 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
/*
* Copyright 2013 Google Inc.
*
* Licensed 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 com.google.common.jimfs;
import static com.google.common.base.Preconditions.checkNotNull;
import java.io.IOException;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.nio.file.attribute.UserPrincipalNotFoundException;
/**
* {@link UserPrincipalLookupService} implementation.
*
* @author Colin Decker
*/
final class UserLookupService extends UserPrincipalLookupService {
private final boolean supportsGroups;
public UserLookupService(boolean supportsGroups) {
this.supportsGroups = supportsGroups;
}
@Override
public UserPrincipal lookupPrincipalByName(String name) {
return createUserPrincipal(name);
}
@Override
public GroupPrincipal lookupPrincipalByGroupName(String group) throws IOException {
if (!supportsGroups) {
throw new UserPrincipalNotFoundException(group); // required by spec
}
return createGroupPrincipal(group);
}
/** Creates a {@link UserPrincipal} for the given user name. */
static UserPrincipal createUserPrincipal(String name) {
return new JimfsUserPrincipal(name);
}
/** Creates a {@link GroupPrincipal} for the given group name. */
static GroupPrincipal createGroupPrincipal(String name) {
return new JimfsGroupPrincipal(name);
}
/** Base class for {@link UserPrincipal} and {@link GroupPrincipal} implementations. */
private abstract static class NamedPrincipal implements UserPrincipal {
protected final String name;
private NamedPrincipal(String name) {
this.name = checkNotNull(name);
}
@Override
public final String getName() {
return name;
}
@Override
public final int hashCode() {
return name.hashCode();
}
@Override
public final String toString() {
return name;
}
}
/** {@link UserPrincipal} implementation. */
static final class JimfsUserPrincipal extends NamedPrincipal {
private JimfsUserPrincipal(String name) {
super(name);
}
@Override
public boolean equals(Object obj) {
return obj instanceof JimfsUserPrincipal
&& getName().equals(((JimfsUserPrincipal) obj).getName());
}
}
/** {@link GroupPrincipal} implementation. */
static final class JimfsGroupPrincipal extends NamedPrincipal implements GroupPrincipal {
private JimfsGroupPrincipal(String name) {
super(name);
}
@Override
public boolean equals(Object obj) {
return obj instanceof JimfsGroupPrincipal && ((JimfsGroupPrincipal) obj).name.equals(name);
}
}
}
|