|
| 1 | +/*- |
| 2 | + * ========================LICENSE_START================================= |
| 3 | + * restheart-commons |
| 4 | + * %% |
| 5 | + * Copyright (C) 2019 - 2026 SoftInstigate |
| 6 | + * %% |
| 7 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | + * you may not use this file except in compliance with the License. |
| 9 | + * You may obtain a copy of the License at |
| 10 | + * |
| 11 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | + * |
| 13 | + * Unless required by applicable law or agreed to in writing, software |
| 14 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | + * See the License for the specific language governing permissions and |
| 17 | + * limitations under the License. |
| 18 | + * =========================LICENSE_END================================== |
| 19 | + */ |
| 20 | +package org.restheart.security; |
| 21 | + |
| 22 | +import java.util.LinkedHashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import org.restheart.configuration.ConfigurationException; |
| 28 | + |
| 29 | +/** |
| 30 | + * Default {@link AclVarsRegistry} implementation: a process-wide singleton, reachable both via |
| 31 | + * dependency injection (see {@link AclVarsRegistryProvider}, for plugins registering a |
| 32 | + * {@link VarResolver}) and via {@link #getInstance()} (for {@link AclVarsInterpolator}'s static |
| 33 | + * methods, which cannot receive an injected instance). |
| 34 | + * |
| 35 | + * <p>Built-in resolvers are registered in the constructor, i.e. at class-loading time — before |
| 36 | + * any plugin {@code @OnInit} can run — so they always win a name collision (see |
| 37 | + * {@link #register(VarResolver)}). |
| 38 | + */ |
| 39 | +public class AclVarsRegistryImpl implements AclVarsRegistry { |
| 40 | + private static final AclVarsRegistryImpl HOLDER = new AclVarsRegistryImpl(); |
| 41 | + |
| 42 | + private final Map<String, VarResolver> resolvers = new LinkedHashMap<>(); |
| 43 | + |
| 44 | + private AclVarsRegistryImpl() { |
| 45 | + BuiltInVarResolvers.all().forEach(resolver -> resolvers.put(resolver.name(), resolver)); |
| 46 | + } |
| 47 | + |
| 48 | + static AclVarsRegistryImpl getInstance() { |
| 49 | + return HOLDER; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void register(VarResolver resolver) throws ConfigurationException { |
| 54 | + var name = resolver.name(); |
| 55 | + |
| 56 | + if (resolvers.containsKey(name)) { |
| 57 | + throw new ConfigurationException( |
| 58 | + "Cannot register VarResolver '" + name + "': a resolver with this name is already registered"); |
| 59 | + } |
| 60 | + |
| 61 | + resolvers.put(name, resolver); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Looks up a registered resolver by name, without the leading {@code @}. |
| 66 | + */ |
| 67 | + Optional<VarResolver> resolver(String name) { |
| 68 | + return Optional.ofNullable(resolvers.get(name)); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * All registered resolver names, without the leading {@code @}, built-in and custom — |
| 73 | + * in registration order (built-ins first). |
| 74 | + */ |
| 75 | + Set<String> names() { |
| 76 | + return resolvers.keySet(); |
| 77 | + } |
| 78 | +} |
0 commit comments