summaryrefslogtreecommitdiff
path: root/java/broker/src/main/java/org/apache/qpid/configuration/Configuration.java
blob: 0b63c6885466a545e4096bca4b16aa34017b7e08 (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
/*
 *  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.configuration;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.PosixParser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

public class Configuration
{
    public static final String QPID_HOME = "QPID_HOME";

    final String QPIDHOME = System.getProperty(QPID_HOME);

    private static Logger _devlog = LoggerFactory.getLogger(Configuration.class);

    public static final String DEFAULT_LOG_CONFIG_FILENAME = "log4j.xml";
    public static final String DEFAULT_CONFIG_FILE = "etc/config.xml";

    protected final Options _options = new Options();
    protected CommandLine _commandLine;
    protected File _configFile;


    public Configuration()
    {

    }

    public void processCommandline(String[] args) throws InitException
    {
        try
        {
            _commandLine = new PosixParser().parse(_options, args);
        }
        catch (ParseException e)
        {
            throw new InitException("Unable to parse commmandline", e);
        }

        final File defaultConfigFile = new File(QPIDHOME, DEFAULT_CONFIG_FILE);
        setConfig(new File(_commandLine.getOptionValue("c", defaultConfigFile.getPath())));
    }

    public void setConfig(File file)
    {
        _configFile = file;
    }

    /**
     * @param option The option to set.
     */
    public void setOption(Option option)
    {
        _options.addOption(option);
    }

    /**
     * getOptionValue from the configuration
     * @param option variable argument, first string is option to get, second if present is the default value.
     * @return the String for the given option or null if not present (if default value not specified)
     */
    public String getOptionValue(String... option)
    {
        if (option.length == 1)
        {
            return _commandLine.getOptionValue(option[0]);
        }
        else if (option.length == 2)
        {
            return _commandLine.getOptionValue(option[0], option[1]);
        }
        return null;
    }

    public void loadConfig(File file) throws InitException
    {
        setConfig(file);
        loadConfig();
    }

    private void loadConfig() throws InitException
    {
        if (!_configFile.exists())
        {
            String error = "File " + _configFile + " could not be found. Check the file exists and is readable.";

            if (QPIDHOME == null)
            {
                error = error + "\nNote: " + QPID_HOME + " is not set.";
            }

            throw new InitException(error, null);
        }
        else
        {
            _devlog.debug("Using configuration file " + _configFile.getAbsolutePath());
        }

//        String logConfig = _commandLine.getOptionValue("l");
//        String logWatchConfig = _commandLine.getOptionValue("w", "0");
//        if (logConfig != null)
//        {
//            File logConfigFile = new File(logConfig);
//            configureLogging(logConfigFile, logWatchConfig);
//        }
//        else
//        {
//            File configFileDirectory = _configFile.getParentFile();
//            File logConfigFile = new File(configFileDirectory, DEFAULT_LOG_CONFIG_FILENAME);
//            configureLogging(logConfigFile, logWatchConfig);
//        }
    }


//    private void configureLogging(File logConfigFile, String logWatchConfig)
//    {
//        int logWatchTime = 0;
//        try
//        {
//            logWatchTime = Integer.parseInt(logWatchConfig);
//        }
//        catch (NumberFormatException e)
//        {
//            _devlog.error("Log watch configuration value of " + logWatchConfig + " is invalid. Must be "
//                          + "a non-negative integer. Using default of zero (no watching configured");
//        }
//
//        if (logConfigFile.exists() && logConfigFile.canRead())
//        {
//            _devlog.info("Configuring logger using configuration file " + logConfigFile.getAbsolutePath());
//            if (logWatchTime > 0)
//            {
//                _devlog.info("log file " + logConfigFile.getAbsolutePath() + " will be checked for changes every "
//                             + logWatchTime + " seconds");
//                // log4j expects the watch interval in milliseconds
//                DOMConfigurator.configureAndWatch(logConfigFile.getAbsolutePath(), logWatchTime * 1000);
//            }
//            else
//            {
//                DOMConfigurator.configure(logConfigFile.getAbsolutePath());
//            }
//        }
//        else
//        {
//            System.err.println("Logging configuration error: unable to read file " + logConfigFile.getAbsolutePath());
//            System.err.println("Using basic log4j configuration");
//            BasicConfigurator.configure();
//        }
//    }

    public File getConfigFile()
    {
        return _configFile;
    }


    public static class InitException extends Exception
    {
        InitException(String msg, Throwable cause)
        {
            super(msg, cause);
        }
    }
}