summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/configuration/plugins/AbstractConfiguration.java
blob: 3c17faef75482cc7b5b2faadbb3c733f802b42a9 (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
/*
 * 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 java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;

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

    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;
    }

    /**
     * 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 + "'");
                }
            }
        }

        validateConfiguration();
    }

    /** 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;
        }
    }

    public static String escapeTagName(String name)
    {
        return name.replaceAll("\\.", "\\.\\.");
    }

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