|
| 1 | +/* |
| 2 | + * See the NOTICE file distributed with this work for additional |
| 3 | + * information regarding copyright ownership. |
| 4 | + * |
| 5 | + * This is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU Lesser General Public License as |
| 7 | + * published by the Free Software Foundation; either version 2.1 of |
| 8 | + * the License, or (at your option) any later version. |
| 9 | + * |
| 10 | + * This software is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this software; if not, write to the Free |
| 17 | + * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA |
| 18 | + * 02110-1301 USA, or see the FSF site: http://www.fsf.org. |
| 19 | + */ |
| 20 | +package org.xwiki.configuration.internal; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.LinkedHashSet; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Set; |
| 26 | + |
| 27 | +import jakarta.inject.Inject; |
| 28 | + |
| 29 | +import org.xwiki.component.descriptor.ComponentDescriptor; |
| 30 | +import org.xwiki.component.manager.ComponentLookupException; |
| 31 | +import org.xwiki.component.manager.ComponentManager; |
| 32 | +import org.xwiki.component.phase.Initializable; |
| 33 | +import org.xwiki.component.phase.InitializationException; |
| 34 | +import org.xwiki.configuration.ConfigurationSource; |
| 35 | + |
| 36 | +/** |
| 37 | + * Helper to implement {@link ConfigurationSource}. |
| 38 | + * |
| 39 | + * @version $Id$ |
| 40 | + * @since 17.5.0RC1 |
| 41 | + */ |
| 42 | +public abstract class AbstractSystemOverwriteConfigurationSource extends AbstractConfigurationSource |
| 43 | + implements Initializable |
| 44 | +{ |
| 45 | + @Inject |
| 46 | + protected ComponentDescriptor<AbstractSystemOverwriteConfigurationSource> componentDescriptor; |
| 47 | + |
| 48 | + @Inject |
| 49 | + protected ComponentManager componentManager; |
| 50 | + |
| 51 | + /** |
| 52 | + * True if it should be possible to overwrite this configuration source using system environment variables or |
| 53 | + * properties. |
| 54 | + */ |
| 55 | + protected boolean systemOverwriteEnabled; |
| 56 | + |
| 57 | + protected ConfigurationSource systemConfigurationSource; |
| 58 | + |
| 59 | + @Override |
| 60 | + public void initialize() throws InitializationException |
| 61 | + { |
| 62 | + if (this.systemOverwriteEnabled) { |
| 63 | + try { |
| 64 | + this.systemConfigurationSource = this.componentManager.getInstance(ConfigurationSource.class, "system"); |
| 65 | + } catch (ComponentLookupException e) { |
| 66 | + throw new InitializationException("Failed to lookup the system configuration source", e); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * @param key the key |
| 73 | + * @return the key to use with the system ConfigurationSource |
| 74 | + */ |
| 75 | + protected String toSystemOverwriteKey(String key) |
| 76 | + { |
| 77 | + return this.componentDescriptor.getRoleHint() + "." + key; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public <T> T getProperty(String key) |
| 82 | + { |
| 83 | + if (this.systemOverwriteEnabled) { |
| 84 | + String systemOverwriteKey = toSystemOverwriteKey(key); |
| 85 | + if (this.systemConfigurationSource.containsKey(systemOverwriteKey)) { |
| 86 | + return this.systemConfigurationSource.getProperty(systemOverwriteKey); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return getPropertyInternal(key); |
| 91 | + } |
| 92 | + |
| 93 | + protected abstract <T> T getPropertyInternal(String key); |
| 94 | + |
| 95 | + @Override |
| 96 | + public <T> T getProperty(String key, T defaultValue) |
| 97 | + { |
| 98 | + if (this.systemOverwriteEnabled) { |
| 99 | + String systemOverwriteKey = toSystemOverwriteKey(key); |
| 100 | + if (this.systemConfigurationSource.containsKey(systemOverwriteKey)) { |
| 101 | + return this.systemConfigurationSource.getProperty(systemOverwriteKey, defaultValue); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return getPropertyInternal(key, defaultValue); |
| 106 | + } |
| 107 | + |
| 108 | + protected abstract <T> T getPropertyInternal(String key, T defaultValue); |
| 109 | + |
| 110 | + @Override |
| 111 | + public <T> T getProperty(String key, Class<T> valueClass) |
| 112 | + { |
| 113 | + if (this.systemOverwriteEnabled) { |
| 114 | + String systemOverwriteKey = toSystemOverwriteKey(key); |
| 115 | + if (this.systemConfigurationSource.containsKey(systemOverwriteKey)) { |
| 116 | + return this.systemConfigurationSource.getProperty(systemOverwriteKey, valueClass); |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return getPropertyInternal(key, valueClass); |
| 121 | + } |
| 122 | + |
| 123 | + protected abstract <T> T getPropertyInternal(String key, Class<T> valueClass); |
| 124 | + |
| 125 | + @Override |
| 126 | + public <T> T getProperty(String key, Class<T> valueClass, T defaultValue) |
| 127 | + { |
| 128 | + if (this.systemOverwriteEnabled) { |
| 129 | + String systemOverwriteKey = toSystemOverwriteKey(key); |
| 130 | + if (this.systemConfigurationSource.containsKey(systemOverwriteKey)) { |
| 131 | + return this.systemConfigurationSource.getProperty(systemOverwriteKey, valueClass, defaultValue); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return getPropertyInternal(key, valueClass, defaultValue); |
| 136 | + } |
| 137 | + |
| 138 | + protected abstract <T> T getPropertyInternal(String key, Class<T> valueClass, T defaultValue); |
| 139 | + |
| 140 | + @Override |
| 141 | + public List<String> getKeys() |
| 142 | + { |
| 143 | + if (this.systemOverwriteEnabled) { |
| 144 | + Set<String> keys = new LinkedHashSet<>(); |
| 145 | + |
| 146 | + // Add current keys |
| 147 | + keys.addAll(getKeysInternal()); |
| 148 | + |
| 149 | + // Add system ones |
| 150 | + keys.addAll(this.systemConfigurationSource.getKeys(toSystemOverwriteKey(""))); |
| 151 | + |
| 152 | + return new ArrayList<>(keys); |
| 153 | + } |
| 154 | + |
| 155 | + return getKeysInternal(); |
| 156 | + } |
| 157 | + |
| 158 | + protected abstract List<String> getKeysInternal(); |
| 159 | + |
| 160 | + @Override |
| 161 | + public List<String> getKeys(String prefix) |
| 162 | + { |
| 163 | + if (this.systemOverwriteEnabled) { |
| 164 | + Set<String> keys = new LinkedHashSet<>(); |
| 165 | + |
| 166 | + // Add current keys |
| 167 | + keys.addAll(getKeysInternal()); |
| 168 | + |
| 169 | + // Add system ones |
| 170 | + keys.addAll(this.systemConfigurationSource.getKeys(toSystemOverwriteKey(prefix))); |
| 171 | + |
| 172 | + return new ArrayList<>(keys); |
| 173 | + } |
| 174 | + |
| 175 | + return getKeysInternal(prefix); |
| 176 | + } |
| 177 | + |
| 178 | + protected abstract List<String> getKeysInternal(String prefix); |
| 179 | + |
| 180 | + @Override |
| 181 | + public boolean containsKey(String key) |
| 182 | + { |
| 183 | + if (this.systemOverwriteEnabled && this.systemConfigurationSource.containsKey(toSystemOverwriteKey(key))) { |
| 184 | + return true; |
| 185 | + } |
| 186 | + |
| 187 | + return containsKeyInternal(key); |
| 188 | + } |
| 189 | + |
| 190 | + protected abstract boolean containsKeyInternal(String key); |
| 191 | + |
| 192 | + @Override |
| 193 | + public boolean isEmpty() |
| 194 | + { |
| 195 | + if (this.systemOverwriteEnabled && !this.systemConfigurationSource.isEmpty(toSystemOverwriteKey(""))) { |
| 196 | + return false; |
| 197 | + } |
| 198 | + |
| 199 | + return isEmptyInternal(); |
| 200 | + } |
| 201 | + |
| 202 | + protected abstract boolean isEmptyInternal(); |
| 203 | + |
| 204 | + @Override |
| 205 | + public boolean isEmpty(String prefix) |
| 206 | + { |
| 207 | + if (this.systemOverwriteEnabled && !this.systemConfigurationSource.isEmpty(toSystemOverwriteKey(prefix))) { |
| 208 | + return false; |
| 209 | + } |
| 210 | + |
| 211 | + return isEmptyInternal(prefix); |
| 212 | + } |
| 213 | + |
| 214 | + protected abstract boolean isEmptyInternal(String prefix); |
| 215 | +} |
0 commit comments