Line data Source code
1 : // Copyright (C) 2014 The Android Open Source Project 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.google.gerrit.server.account; 16 : 17 : import static com.google.common.base.Preconditions.checkArgument; 18 : 19 : import com.google.common.collect.Iterables; 20 : import com.google.gerrit.common.Nullable; 21 : import com.google.gerrit.entities.Account; 22 : import com.google.gerrit.extensions.common.AccountInfo; 23 : import com.google.gerrit.server.account.AccountDirectory.FillOptions; 24 : import com.google.gerrit.server.permissions.PermissionBackendException; 25 : import com.google.inject.assistedinject.Assisted; 26 : import com.google.inject.assistedinject.AssistedInject; 27 : import java.util.ArrayList; 28 : import java.util.Collection; 29 : import java.util.Collections; 30 : import java.util.EnumSet; 31 : import java.util.HashMap; 32 : import java.util.List; 33 : import java.util.Map; 34 : import java.util.Set; 35 : 36 : public class AccountLoader { 37 108 : public static final Set<FillOptions> DETAILED_OPTIONS = 38 108 : Collections.unmodifiableSet( 39 108 : EnumSet.of( 40 : FillOptions.ID, 41 : FillOptions.NAME, 42 : FillOptions.EMAIL, 43 : FillOptions.USERNAME, 44 : FillOptions.DISPLAY_NAME, 45 : FillOptions.STATUS, 46 : FillOptions.STATE, 47 : FillOptions.AVATARS, 48 : FillOptions.TAGS)); 49 : 50 : public interface Factory { 51 : AccountLoader create(boolean detailed); 52 : 53 : AccountLoader create(Set<FillOptions> options); 54 : } 55 : 56 : private final InternalAccountDirectory directory; 57 : private final Set<FillOptions> options; 58 : private final Map<Account.Id, AccountInfo> created; 59 : private final List<AccountInfo> provided; 60 : 61 : @AssistedInject 62 : AccountLoader(InternalAccountDirectory directory, @Assisted boolean detailed) { 63 108 : this(directory, detailed ? DETAILED_OPTIONS : InternalAccountDirectory.ID_ONLY); 64 108 : } 65 : 66 : @AssistedInject 67 108 : AccountLoader(InternalAccountDirectory directory, @Assisted Set<FillOptions> options) { 68 108 : this.directory = directory; 69 108 : this.options = options; 70 108 : created = new HashMap<>(); 71 108 : provided = new ArrayList<>(); 72 108 : } 73 : 74 : @Nullable 75 : public synchronized AccountInfo get(@Nullable Account.Id id) { 76 108 : if (id == null) { 77 103 : return null; 78 : } 79 108 : AccountInfo info = created.get(id); 80 108 : if (info == null) { 81 108 : info = new AccountInfo(id.get()); 82 108 : created.put(id, info); 83 : } 84 108 : return info; 85 : } 86 : 87 : public synchronized void put(AccountInfo info) { 88 73 : checkArgument(info._accountId != null, "_accountId field required"); 89 73 : provided.add(info); 90 73 : } 91 : 92 : public void fill() throws PermissionBackendException { 93 108 : directory.fillAccountInfo(Iterables.concat(created.values(), provided), options); 94 108 : } 95 : 96 : public void fill(Collection<? extends AccountInfo> infos) throws PermissionBackendException { 97 30 : for (AccountInfo info : infos) { 98 30 : put(info); 99 30 : } 100 30 : fill(); 101 30 : } 102 : 103 : @Nullable 104 : public AccountInfo fillOne(@Nullable Account.Id id) throws PermissionBackendException { 105 7 : AccountInfo info = get(id); 106 7 : fill(); 107 7 : return info; 108 : } 109 : }