summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/ConfigurationPlugin.java
blob: d08e3bc806770e10c0763c5b1ec1edfe79dbb1df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.qpid.server.configuration.plugins;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConversionException;
import org.apache.log4j.Logger;

import org.apache.qpid.server.configuration.ConfigurationManager;
import org.apache.qpid.server.registry.ApplicationRegistry;
import org.apache.qpid.server.registry.IApplicationRegistry;

import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

public abstract class ConfigurationPlugin
{
    protected static final Logger _logger = Logger.getLogger(ConfigurationPlugin.class);

    private Map<String, ConfigurationPlugin>
            _pluginConfiguration = new HashMap<String, ConfigurationPlugin>();

    private Configuration _config;

    /**
     * The Elements that this Plugin can process.
     *
     * For a Queues plugin that would be a list containing:
     * <ul>
     * <li>queue - the queue entries
     * <li>the alerting values for defaults
     * <li>exchange - the default exchange
     * <li>durable - set the default durablity
     * </ul>
     */
    abstract public String[] getElementsProcessed();

    /** Performs configuration validation. */
    public void validateConfiguration() throws ConfigurationException
    {
        // Override in sub-classes
    }

    public Configuration getConfig()
    {
        return _config;
    }

    public <C extends ConfigurationPlugin> C getConfiguration(String plugin)
    {
        return (C) _pluginConfiguration.get(plugin);
    }

    /**
     * Sets the configuration for this plugin
     *
     * @param path
     * @param configuration the configuration for this plugin.
     */
    public void setConfiguration(String path, Configuration configuration) throws ConfigurationException
    {
        _config = configuration;

        // Extract a list of elements for processing
        Iterator<?> keys = configuration.getKeys();

        Set<String> elements = new HashSet<String>();
        while (keys.hasNext())
        {
            String key = (String) keys.next();

            int elementNameIndex = key.indexOf(".");

            String element = key.trim();
            if (elementNameIndex != -1)
            {
                element = key.substring(0, elementNameIndex).trim();
            }

            // Trim any element properties
            elementNameIndex = element.indexOf("[");
            if (elementNameIndex > 0)
            {
                element = element.substring(0, elementNameIndex).trim();
            }

            elements.add(element);
        }

        //Remove the items we already expect in the configuration
        for (String tag : getElementsProcessed())
        {

            // Work round the issue with Commons configuration.
            // With an XMLConfiguration the key will be [@property]
            // but with a CompositeConfiguration it will be @property].
            // Hide this issue from our users so when/if we change the
            // configuration they don't have to. 
            int bracketIndex = tag.indexOf("[");
            if (bracketIndex != -1)
            {
                tag = tag.substring(bracketIndex + 1, tag.length());
            }

            elements.remove(tag);
        }

        if (_logger.isInfoEnabled())
        {
            if (!elements.isEmpty())
            {
                _logger.info("Elements to lookup:" + path);
                for (String tag : elements)
                {
                    _logger.info("Tag:'" + tag + "'");
                }
            }
        }

        offerRemainingConfigurationToOtherPlugins(path, configuration, elements);

        validateConfiguration();
    }

    private void offerRemainingConfigurationToOtherPlugins(String path,
            Configuration configuration, Set<String> elements) throws ConfigurationException
    {
        final IApplicationRegistry appRegistry = safeGetApplicationRegistryInstance();

        if (appRegistry == null)
        {
            // We see this happen during shutdown due to asynchronous reconfig using IO threads.
            // Need to remove the responsibility for offering configuration to other class.
            _logger.info("Cannot offer remaining config to other plugins, can't find app registry");
            return;
        }

        final ConfigurationManager configurationManager = appRegistry.getConfigurationManager();
        // Process the elements in the configuration
        for (String element : elements)
        {
            Configuration handled = element.length() == 0 ? configuration : configuration.subset(element);

            String configurationElement = element;
            if (path.length() > 0)
            {
                configurationElement = path + "." + configurationElement;
            }

            List<ConfigurationPlugin> handlers = configurationManager.getConfigurationPlugins(configurationElement, handled);

            if(_logger.isDebugEnabled())
            {
                _logger.debug("For '" + element + "' found handlers (" + handlers.size() + "):" + handlers);
            }
            
            for (ConfigurationPlugin plugin : handlers)
            {
                _pluginConfiguration.put(plugin.getClass().getName(), plugin);
            }
        }
    }

    private IApplicationRegistry safeGetApplicationRegistryInstance()
    {
        try
        {
            return ApplicationRegistry.getInstance();
        }
        catch (IllegalStateException ise)
        {
            return null;
        }
    }

    /** Helper method to print out list of keys in a {@link Configuration}. */
    public static final void showKeys(Configuration config)
    {
        if (config.isEmpty())
        {
            _logger.info("Configuration is empty");
        }
        else
        {
            Iterator<?> keys = config.getKeys();
            while (keys.hasNext())
            {
                String key = (String) keys.next();
                _logger.info("Configuration key: " + key);
            }
        }
    }

    protected boolean hasConfiguration()
    {
        return _config != null;
    }

    /// Getters

    protected double getDoubleValue(String property)
    {
        return getDoubleValue(property, 0.0);
    }

    protected double getDoubleValue(String property, double defaultValue)
    {
        return _config.getDouble(property, defaultValue);
    }

    protected long getLongValue(String property)
    {
        return getLongValue(property, 0);
    }

    protected long getLongValue(String property, long defaultValue)
    {
        return _config.getLong(property, defaultValue);
    }

    protected int getIntValue(String property)
    {
        return getIntValue(property, 0);
    }

    protected int getIntValue(String property, int defaultValue)
    {
        return _config.getInt(property, defaultValue);
    }

    protected String getStringValue(String property)
    {
        return getStringValue(property, null);
    }

    protected String getStringValue(String property, String defaultValue)
    {
        return _config.getString(property, defaultValue);
    }

    protected boolean getBooleanValue(String property)
    {
        return getBooleanValue(property, false);
    }

    protected boolean getBooleanValue(String property, boolean defaultValue)
    {
        return _config.getBoolean(property, defaultValue);
    }

    protected List getListValue(String property)
    {
        return getListValue(property, Collections.EMPTY_LIST);
    }

    protected List getListValue(String property, List defaultValue)
    {
        return _config.getList(property, defaultValue);
    }

    /// Validation Helpers

    protected boolean contains(String property)
    {
        return _config.getProperty(property) != null;
    }

    /**
     * Provide mechanism to validate Configuration contains a Postiive Long Value
     *
     * @param property
     *
     * @throws ConfigurationException
     */
    protected void validatePositiveLong(String property) throws ConfigurationException
    {
        try
        {
            if (!containsPositiveLong(property))
            {
                throw new ConfigurationException(this.getClass().getSimpleName()
                                                 + ": '" + property +
                                                 "' must be a Positive Long value.");
            }
        }
        catch (Exception e)
        {
            Throwable last = e;

            // Find the first cause
            if (e instanceof ConversionException)
            {
                Throwable t = e.getCause();
                while (t != null)
                {
                    last = t;
                    t = last.getCause();
                }
            }

            throw new ConfigurationException(this.getClass().getSimpleName() +
                                             ": unable to configure invalid " +
                                             property + ":" +
                                             _config.getString(property),
                                             last);
        }
    }

    protected boolean containsLong(String property)
    {
        try
        {
            _config.getLong(property);
            return true;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }
    }

    protected boolean containsPositiveLong(String property)
    {
        try
        {
            long value = _config.getLong(property);
            return value > 0;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }

    }

    protected boolean containsInt(String property)
    {
        try
        {
            _config.getInt(property);
            return true;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }
    }

    protected boolean containsBoolean(String property)
    {
        try
        {
            _config.getBoolean(property);
            return true;
        }
        catch (NoSuchElementException e)
        {
            return false;
        }
    }

    /**
     * Given another configuration merge the configuration into our own config
     *
     * The new values being merged in will take precedence over existing values.
     *
     * In the simplistic case this means something like:
     *
     * So if we have configuration set
     * name = 'fooo'
     *
     * And the new configuration contains a name then that will be reset.
     * name = 'new'
     *
     * However this plugin will simply contain other plugins so the merge will
     * be called until we end up at a base plugin that understand how to merge
     * items. i.e Alerting values. Where the provided configuration will take
     * precedence.
     *
     * @param configuration the config to merge in to our own.
     */
    public void addConfiguration(ConfigurationPlugin configuration)
    {
        // If given configuration is null then there is nothing to process.
        if (configuration == null)
        {
            return;
        }

        // Merge all the sub configuration items
        for (Map.Entry<String, ConfigurationPlugin> newPlugins : configuration._pluginConfiguration.entrySet())
        {
            String key = newPlugins.getKey();
            ConfigurationPlugin config = newPlugins.getValue();

            if (_pluginConfiguration.containsKey(key))
            {
                //Merge the configuration if we already have this type of config
                _pluginConfiguration.get(key).mergeConfiguration(config);
            }
            else
            {
                //otherwise just add it to our config.
                _pluginConfiguration.put(key, config);
            }
        }

        //Merge the configuration itself
        String key = configuration.getClass().getName();
        if (_pluginConfiguration.containsKey(key))
        {
            //Merge the configuration if we already have this type of config
            _pluginConfiguration.get(key).mergeConfiguration(configuration);
        }
        else
        {
            //If we are adding a configuration of our own type then merge
            if (configuration.getClass() == this.getClass())
            {
                mergeConfiguration(configuration);
            }
            else
            {
                // just store this in case someone else needs it.
                _pluginConfiguration.put(key, configuration);
            }

        }

    }

    protected void mergeConfiguration(ConfigurationPlugin configuration)
    {
         _config = configuration.getConfig();
    }

    public String toString()
    {
        StringBuilder sb = new StringBuilder();

        sb.append("\n").append(getClass().getSimpleName());
        sb.append("=[ (").append(formatToString()).append(")");

        for(Map.Entry<String,ConfigurationPlugin> item : _pluginConfiguration.entrySet())
        {
            sb.append("\n").append(item.getValue());
        }

        sb.append("]\n");

        return sb.toString();
    }

    public String formatToString()
    {
        return super.toString();
    }

    protected void setConfig(Configuration config)
    {
        _config = config;
    }
}