summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/server/configuration/Configurator.java
blob: 379da94aa3ec9d50ac1553cdb1be13c862b8b550 (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
/*
 *
 * 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;

import org.apache.commons.configuration.Configuration;
import org.apache.log4j.Logger;
import org.apache.qpid.configuration.Configured;
import org.apache.qpid.configuration.PropertyUtils;
import org.apache.qpid.configuration.PropertyException;
import org.apache.qpid.server.registry.ApplicationRegistry;

import java.lang.reflect.Field;

/**
 * This class contains utilities for populating classes automatically from values pulled from configuration
 * files.
 */
public class Configurator
{
    private static final Logger _logger = Logger.getLogger(Configurator.class);


    /**
     * Configure a given instance using the supplied configuration. Note that superclasses are <b>not</b>
     * currently configured but this could easily be added if required.
     * @param instance the instance to configure
     * @param config the configuration to use to configure the object
     */
    public static void configure(Object instance, Configuration config)
    {

        for (Field f : instance.getClass().getDeclaredFields())
        {
            Configured annotation = f.getAnnotation(Configured.class);
            if (annotation != null)
            {
                setValueInField(f, instance, config, annotation);
            }
        }
    }



    /**
     * Configure a given instance using the application configuration. Note that superclasses are <b>not</b>
     * currently configured but this could easily be added if required.
     * @param instance the instance to configure
     */
    public static void configure(Object instance)
    {
        configure(instance, ApplicationRegistry.getInstance().getConfiguration());
    }

    private static void setValueInField(Field f, Object instance, Configuration config, Configured annotation)
    {
        Class fieldClass = f.getType();
        String configPath = annotation.path();
        try
        {
            if (fieldClass == String.class)
            {
                String val = config.getString(configPath, annotation.defaultValue());
                val = PropertyUtils.replaceProperties(val);
                f.set(instance, val);
            }
            else if (fieldClass == int.class)
            {
                int val = config.getInt(configPath, Integer.parseInt(annotation.defaultValue()));
                f.setInt(instance, val);
            }
            else if (fieldClass == long.class)
            {
                long val = config.getLong(configPath, Long.parseLong(annotation.defaultValue()));
                f.setLong(instance, val);
            }
            else if (fieldClass == double.class)
            {
                double val = config.getDouble(configPath, Double.parseDouble(annotation.defaultValue()));
                f.setDouble(instance, val);
            }
            else if (fieldClass == boolean.class)
            {
                boolean val = config.getBoolean(configPath, Boolean.parseBoolean(annotation.defaultValue()));
                f.setBoolean(instance, val);
            }
            else
            {
                _logger.error("Unsupported field type " + fieldClass + " for " + f + " IGNORING configured value");
            }
        }
        catch (PropertyException e)
        {
            _logger.error("Unable to expand property: " + e + " INGORING field " + f, e);
        }
        catch (IllegalAccessException e)
        {
            _logger.error("Unable to access field " + f + " IGNORING configured value");
        }
    }
}