Line data Source code
1 : // Copyright (C) 2015 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 java.util.stream.Collectors.joining; 18 : 19 : import com.google.common.base.Strings; 20 : import com.google.common.flogger.FluentLogger; 21 : import com.google.gerrit.entities.Account; 22 : import com.google.gerrit.entities.RefNames; 23 : import com.google.gerrit.server.git.ValidationError; 24 : import com.google.gerrit.server.git.meta.VersionedMetaData; 25 : import java.io.IOException; 26 : import java.util.ArrayList; 27 : import java.util.List; 28 : import org.eclipse.jgit.errors.ConfigInvalidException; 29 : import org.eclipse.jgit.lib.CommitBuilder; 30 : 31 : /** 32 : * Named Queries for user accounts. 33 : * 34 : * <p>Users can define aliases for change queries. These are stored as versioned account data and 35 : * (de)serialized with this class. 36 : */ 37 : public class VersionedAccountQueries extends VersionedMetaData { 38 4 : private static final FluentLogger logger = FluentLogger.forEnclosingClass(); 39 : 40 : public static VersionedAccountQueries forUser(Account.Id id) { 41 4 : return new VersionedAccountQueries(RefNames.refsUsers(id)); 42 : } 43 : 44 : private final String ref; 45 : private QueryList queryList; 46 : 47 4 : private VersionedAccountQueries(String ref) { 48 4 : this.ref = ref; 49 4 : } 50 : 51 : @Override 52 : protected String getRefName() { 53 4 : return ref; 54 : } 55 : 56 : public QueryList getQueryList() { 57 4 : return queryList; 58 : } 59 : 60 : public void setQueryList(String text) throws IOException, ConfigInvalidException { 61 4 : List<ValidationError> errors = new ArrayList<>(); 62 4 : QueryList newQueryList = QueryList.parse(text, error -> errors.add(error)); 63 4 : if (!errors.isEmpty()) { 64 0 : String messages = errors.stream().map(ValidationError::getMessage).collect(joining(", ")); 65 0 : throw new ConfigInvalidException("Invalid named queries: " + messages); 66 : } 67 4 : queryList = newQueryList; 68 4 : } 69 : 70 : @Override 71 : protected void onLoad() throws IOException, ConfigInvalidException { 72 4 : queryList = 73 4 : QueryList.parse( 74 4 : readUTF8(QueryList.FILE_NAME), 75 : error -> 76 0 : logger.atSevere().log( 77 0 : "Error parsing file %s: %s", QueryList.FILE_NAME, error.getMessage())); 78 4 : } 79 : 80 : @Override 81 : protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException { 82 4 : if (Strings.isNullOrEmpty(commit.getMessage())) { 83 4 : commit.setMessage("Updated named queries\n"); 84 : } 85 4 : saveUTF8(QueryList.FILE_NAME, queryList.asText()); 86 4 : return true; 87 : } 88 : }