Line data Source code
1 : // Copyright (C) 2009 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.args4j; 16 : 17 : import static com.google.gerrit.util.cli.Localizable.localizable; 18 : 19 : import com.google.gerrit.entities.AccountGroup; 20 : import com.google.gerrit.entities.GroupDescription; 21 : import com.google.gerrit.entities.GroupReference; 22 : import com.google.gerrit.entities.InternalGroup; 23 : import com.google.gerrit.server.account.GroupBackend; 24 : import com.google.gerrit.server.account.GroupBackends; 25 : import com.google.gerrit.server.account.GroupCache; 26 : import com.google.gerrit.server.group.InternalGroupDescription; 27 : import com.google.inject.Inject; 28 : import com.google.inject.assistedinject.Assisted; 29 : import java.util.Optional; 30 : import org.kohsuke.args4j.CmdLineException; 31 : import org.kohsuke.args4j.CmdLineParser; 32 : import org.kohsuke.args4j.OptionDef; 33 : import org.kohsuke.args4j.spi.OptionHandler; 34 : import org.kohsuke.args4j.spi.Parameters; 35 : import org.kohsuke.args4j.spi.Setter; 36 : 37 : public class AccountGroupUUIDHandler extends OptionHandler<AccountGroup.UUID> { 38 : private final GroupBackend groupBackend; 39 : private final GroupCache groupCache; 40 : 41 : @Inject 42 : public AccountGroupUUIDHandler( 43 : final GroupBackend groupBackend, 44 : @Assisted final CmdLineParser parser, 45 : @Assisted final OptionDef option, 46 : @Assisted final Setter<AccountGroup.UUID> setter, 47 : GroupCache groupCache) { 48 5 : super(parser, option, setter); 49 5 : this.groupBackend = groupBackend; 50 5 : this.groupCache = groupCache; 51 5 : } 52 : 53 : @Override 54 : public final int parseArguments(Parameters params) throws CmdLineException { 55 1 : final String n = params.getParameter(0); 56 1 : AccountGroup.UUID uuid = AccountGroup.uuid(n); 57 1 : if (groupBackend.handles(uuid)) { 58 0 : GroupDescription.Basic d = groupBackend.get(uuid); 59 0 : if (d != null) { 60 0 : setter.addValue(uuid); 61 0 : return 1; 62 : } 63 : } 64 : 65 : // Might be a numeric AccountGroup.Id. -> Internal group. 66 1 : if (n.matches("^[1-9][0-9]*$")) { 67 : try { 68 0 : AccountGroup.Id groupId = AccountGroup.Id.parse(n); 69 0 : Optional<InternalGroup> groupInternal = groupCache.get(groupId); 70 0 : if (groupInternal.isPresent()) { 71 0 : uuid = new InternalGroupDescription(groupInternal.get()).getGroupUUID(); 72 0 : setter.addValue(uuid); 73 0 : return 1; 74 : } 75 0 : } catch (IllegalArgumentException e) { 76 : // Ignored 77 0 : } 78 : } 79 : 80 1 : GroupReference group = GroupBackends.findExactSuggestion(groupBackend, n); 81 1 : if (group == null) { 82 1 : throw new CmdLineException(owner, localizable("Group \"%s\" does not exist"), n); 83 : } 84 1 : setter.addValue(group.getUUID()); 85 1 : return 1; 86 : } 87 : 88 : @Override 89 : public final String getDefaultMetaVariable() { 90 1 : return "GROUP"; 91 : } 92 : }