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.restapi.config; 16 : 17 : import static com.google.gerrit.common.data.GlobalCapability.FLUSH_CACHES; 18 : import static com.google.gerrit.common.data.GlobalCapability.MAINTAIN_SERVER; 19 : 20 : import com.google.common.cache.Cache; 21 : import com.google.gerrit.extensions.annotations.RequiresAnyCapability; 22 : import com.google.gerrit.extensions.registration.DynamicMap; 23 : import com.google.gerrit.extensions.registration.Extension; 24 : import com.google.gerrit.extensions.registration.PluginName; 25 : import com.google.gerrit.extensions.restapi.AuthException; 26 : import com.google.gerrit.extensions.restapi.BadRequestException; 27 : import com.google.gerrit.extensions.restapi.Response; 28 : import com.google.gerrit.extensions.restapi.RestCollectionModifyView; 29 : import com.google.gerrit.extensions.restapi.UnprocessableEntityException; 30 : import com.google.gerrit.server.config.CacheResource; 31 : import com.google.gerrit.server.config.ConfigResource; 32 : import com.google.gerrit.server.permissions.PermissionBackendException; 33 : import com.google.gerrit.server.restapi.config.PostCaches.Input; 34 : import com.google.inject.Inject; 35 : import com.google.inject.Singleton; 36 : import java.util.ArrayList; 37 : import java.util.List; 38 : 39 : @RequiresAnyCapability({FLUSH_CACHES, MAINTAIN_SERVER}) 40 : @Singleton 41 : public class PostCaches implements RestCollectionModifyView<ConfigResource, CacheResource, Input> { 42 : public static class Input { 43 : public Operation operation; 44 : public List<String> caches; 45 : 46 2 : public Input() {} 47 : 48 : public Input(Operation op) { 49 1 : this(op, null); 50 1 : } 51 : 52 1 : public Input(Operation op, List<String> c) { 53 1 : operation = op; 54 1 : caches = c; 55 1 : } 56 : } 57 : 58 1 : public enum Operation { 59 1 : FLUSH_ALL, 60 1 : FLUSH 61 : } 62 : 63 : private final DynamicMap<Cache<?, ?>> cacheMap; 64 : private final FlushCache flushCache; 65 : 66 : @Inject 67 138 : public PostCaches(DynamicMap<Cache<?, ?>> cacheMap, FlushCache flushCache) { 68 138 : this.cacheMap = cacheMap; 69 138 : this.flushCache = flushCache; 70 138 : } 71 : 72 : @Override 73 : public Response<String> apply(ConfigResource rsrc, Input input) 74 : throws AuthException, BadRequestException, UnprocessableEntityException, 75 : PermissionBackendException { 76 2 : if (input == null || input.operation == null) { 77 1 : throw new BadRequestException("operation must be specified"); 78 : } 79 : 80 1 : switch (input.operation) { 81 : case FLUSH_ALL: 82 1 : if (input.caches != null) { 83 1 : throw new BadRequestException( 84 : "specifying caches is not allowed for operation 'FLUSH_ALL'"); 85 : } 86 1 : flushAll(); 87 1 : return Response.ok(); 88 : case FLUSH: 89 1 : if (input.caches == null || input.caches.isEmpty()) { 90 1 : throw new BadRequestException("caches must be specified for operation 'FLUSH'"); 91 : } 92 1 : flush(input.caches); 93 1 : return Response.ok(); 94 : default: 95 0 : throw new BadRequestException("unsupported operation: " + input.operation); 96 : } 97 : } 98 : 99 : private void flushAll() throws AuthException, PermissionBackendException { 100 1 : for (Extension<Cache<?, ?>> e : cacheMap) { 101 1 : CacheResource cacheResource = 102 1 : new CacheResource(e.getPluginName(), e.getExportName(), e.getProvider()); 103 1 : if (FlushCache.WEB_SESSIONS.equals(cacheResource.getName())) { 104 1 : continue; 105 : } 106 1 : flushCache.apply(cacheResource, null); 107 1 : } 108 1 : } 109 : 110 : private void flush(List<String> cacheNames) 111 : throws UnprocessableEntityException, AuthException, PermissionBackendException { 112 1 : List<CacheResource> cacheResources = new ArrayList<>(cacheNames.size()); 113 : 114 1 : for (String n : cacheNames) { 115 1 : String pluginName = PluginName.GERRIT; 116 1 : String cacheName = n; 117 1 : int i = cacheName.lastIndexOf('-'); 118 1 : if (i != -1) { 119 0 : pluginName = cacheName.substring(0, i); 120 0 : cacheName = cacheName.length() > i + 1 ? cacheName.substring(i + 1) : ""; 121 : } 122 : 123 1 : Cache<?, ?> cache = cacheMap.get(pluginName, cacheName); 124 1 : if (cache != null) { 125 1 : cacheResources.add(new CacheResource(pluginName, cacheName, cache)); 126 : } else { 127 1 : throw new UnprocessableEntityException(String.format("cache %s not found", n)); 128 : } 129 1 : } 130 : 131 1 : for (CacheResource rsrc : cacheResources) { 132 1 : flushCache.apply(rsrc, null); 133 1 : } 134 1 : } 135 : }