• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2013 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.jimfs;
18 
19 import static com.google.common.base.Preconditions.checkNotNull;
20 
21 import java.io.IOException;
22 import java.nio.file.attribute.GroupPrincipal;
23 import java.nio.file.attribute.UserPrincipal;
24 import java.nio.file.attribute.UserPrincipalLookupService;
25 import java.nio.file.attribute.UserPrincipalNotFoundException;
26 
27 /**
28  * {@link UserPrincipalLookupService} implementation.
29  *
30  * @author Colin Decker
31  */
32 final class UserLookupService extends UserPrincipalLookupService {
33 
34   private final boolean supportsGroups;
35 
UserLookupService(boolean supportsGroups)36   public UserLookupService(boolean supportsGroups) {
37     this.supportsGroups = supportsGroups;
38   }
39 
40   @Override
lookupPrincipalByName(String name)41   public UserPrincipal lookupPrincipalByName(String name) {
42     return createUserPrincipal(name);
43   }
44 
45   @Override
lookupPrincipalByGroupName(String group)46   public GroupPrincipal lookupPrincipalByGroupName(String group) throws IOException {
47     if (!supportsGroups) {
48       throw new UserPrincipalNotFoundException(group); // required by spec
49     }
50     return createGroupPrincipal(group);
51   }
52 
53   /** Creates a {@link UserPrincipal} for the given user name. */
createUserPrincipal(String name)54   static UserPrincipal createUserPrincipal(String name) {
55     return new JimfsUserPrincipal(name);
56   }
57 
58   /** Creates a {@link GroupPrincipal} for the given group name. */
createGroupPrincipal(String name)59   static GroupPrincipal createGroupPrincipal(String name) {
60     return new JimfsGroupPrincipal(name);
61   }
62 
63   /** Base class for {@link UserPrincipal} and {@link GroupPrincipal} implementations. */
64   private abstract static class NamedPrincipal implements UserPrincipal {
65 
66     protected final String name;
67 
NamedPrincipal(String name)68     private NamedPrincipal(String name) {
69       this.name = checkNotNull(name);
70     }
71 
72     @Override
getName()73     public final String getName() {
74       return name;
75     }
76 
77     @Override
hashCode()78     public final int hashCode() {
79       return name.hashCode();
80     }
81 
82     @Override
toString()83     public final String toString() {
84       return name;
85     }
86   }
87 
88   /** {@link UserPrincipal} implementation. */
89   static final class JimfsUserPrincipal extends NamedPrincipal {
90 
JimfsUserPrincipal(String name)91     private JimfsUserPrincipal(String name) {
92       super(name);
93     }
94 
95     @Override
equals(Object obj)96     public boolean equals(Object obj) {
97       return obj instanceof JimfsUserPrincipal
98           && getName().equals(((JimfsUserPrincipal) obj).getName());
99     }
100   }
101 
102   /** {@link GroupPrincipal} implementation. */
103   static final class JimfsGroupPrincipal extends NamedPrincipal implements GroupPrincipal {
104 
JimfsGroupPrincipal(String name)105     private JimfsGroupPrincipal(String name) {
106       super(name);
107     }
108 
109     @Override
equals(Object obj)110     public boolean equals(Object obj) {
111       return obj instanceof JimfsGroupPrincipal && ((JimfsGroupPrincipal) obj).name.equals(name);
112     }
113   }
114 }
115