Configuration.java
001 /*
002  *  Licensed to the Apache Software Foundation (ASF) under one
003  *  or more contributor license agreements.  See the NOTICE file
004  *  distributed with this work for additional information
005  *  regarding copyright ownership.  The ASF licenses this file
006  *  to you under the Apache License, Version 2.0 (the
007  *  "License"); you may not use this file except in compliance
008  *  with the License.  You may obtain a copy of the License at
009  *
010  *    http://www.apache.org/licenses/LICENSE-2.0
011  *
012  *  Unless required by applicable law or agreed to in writing,
013  *  software distributed under the License is distributed on an
014  *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015  *  KIND, either express or implied.  See the License for the
016  *  specific language governing permissions and limitations
017  *  under the License.    
018  
019  */
020 package org.apache.qpid.configuration;
021 
022 import org.apache.commons.cli.CommandLine;
023 import org.apache.commons.cli.Option;
024 import org.apache.commons.cli.Options;
025 import org.apache.commons.cli.ParseException;
026 import org.apache.commons.cli.PosixParser;
027 import org.slf4j.Logger;
028 import org.slf4j.LoggerFactory;
029 
030 import java.io.File;
031 
032 public class Configuration
033 {
034     public static final String QPID_HOME = "QPID_HOME";
035 
036     final String QPIDHOME = System.getProperty(QPID_HOME);
037 
038     private static Logger _devlog = LoggerFactory.getLogger(Configuration.class);
039 
040     public static final String DEFAULT_LOG_CONFIG_FILENAME = "log4j.xml";
041     public static final String DEFAULT_CONFIG_FILE = "etc/config.xml";
042 
043     protected final Options _options = new Options();
044     protected CommandLine _commandLine;
045     protected File _configFile;
046 
047 
048     public Configuration()
049     {
050 
051     }
052 
053     public void processCommandline(String[] argsthrows InitException
054     {
055         try
056         {
057             _commandLine = new PosixParser().parse(_options, args);
058         }
059         catch (ParseException e)
060         {
061             throw new InitException("Unable to parse commmandline", e);
062         }
063 
064         final File defaultConfigFile = new File(QPIDHOME, DEFAULT_CONFIG_FILE);
065         setConfig(new File(_commandLine.getOptionValue("c", defaultConfigFile.getPath())));
066     }
067 
068     public void setConfig(File file)
069     {
070         _configFile = file;
071     }
072 
073     /**
074      @param option The option to set.
075      */
076     public void setOption(Option option)
077     {
078         _options.addOption(option);
079     }
080 
081     /**
082      * getOptionValue from the configuration
083      @param option variable argument, first string is option to get, second if present is the default value.
084      @return the String for the given option or null if not present (if default value not specified)
085      */
086     public String getOptionValue(String... option)
087     {
088         if (option.length == 1)
089         {
090             return _commandLine.getOptionValue(option[0]);
091         }
092         else if (option.length == 2)
093         {
094             return _commandLine.getOptionValue(option[0], option[1]);
095         }
096         return null;
097     }
098 
099     public void loadConfig(File filethrows InitException
100     {
101         setConfig(file);
102         loadConfig();
103     }
104 
105     private void loadConfig() throws InitException
106     {
107         if (!_configFile.exists())
108         {
109             String error = "File " + _configFile + " could not be found. Check the file exists and is readable.";
110 
111             if (QPIDHOME == null)
112             {
113                 error = error + "\nNote: " + QPID_HOME + " is not set.";
114             }
115 
116             throw new InitException(error, null);
117         }
118         else
119         {
120             _devlog.debug("Using configuration file " + _configFile.getAbsolutePath());
121         }
122 
123 //        String logConfig = _commandLine.getOptionValue("l");
124 //        String logWatchConfig = _commandLine.getOptionValue("w", "0");
125 //        if (logConfig != null)
126 //        {
127 //            File logConfigFile = new File(logConfig);
128 //            configureLogging(logConfigFile, logWatchConfig);
129 //        }
130 //        else
131 //        {
132 //            File configFileDirectory = _configFile.getParentFile();
133 //            File logConfigFile = new File(configFileDirectory, DEFAULT_LOG_CONFIG_FILENAME);
134 //            configureLogging(logConfigFile, logWatchConfig);
135 //        }
136     }
137 
138 
139 //    private void configureLogging(File logConfigFile, String logWatchConfig)
140 //    {
141 //        int logWatchTime = 0;
142 //        try
143 //        {
144 //            logWatchTime = Integer.parseInt(logWatchConfig);
145 //        }
146 //        catch (NumberFormatException e)
147 //        {
148 //            _devlog.error("Log watch configuration value of " + logWatchConfig + " is invalid. Must be "
149 //                          + "a non-negative integer. Using default of zero (no watching configured");
150 //        }
151 //
152 //        if (logConfigFile.exists() && logConfigFile.canRead())
153 //        {
154 //            _devlog.info("Configuring logger using configuration file " + logConfigFile.getAbsolutePath());
155 //            if (logWatchTime > 0)
156 //            {
157 //                _devlog.info("log file " + logConfigFile.getAbsolutePath() + " will be checked for changes every "
158 //                             + logWatchTime + " seconds");
159 //                // log4j expects the watch interval in milliseconds
160 //                DOMConfigurator.configureAndWatch(logConfigFile.getAbsolutePath(), logWatchTime * 1000);
161 //            }
162 //            else
163 //            {
164 //                DOMConfigurator.configure(logConfigFile.getAbsolutePath());
165 //            }
166 //        }
167 //        else
168 //        {
169 //            System.err.println("Logging configuration error: unable to read file " + logConfigFile.getAbsolutePath());
170 //            System.err.println("Using basic log4j configuration");
171 //            BasicConfigurator.configure();
172 //        }
173 //    }
174 
175     public File getConfigFile()
176     {
177         return _configFile;
178     }
179 
180 
181     public class InitException extends Exception
182     {
183         InitException(String msg, Throwable cause)
184         {
185             super(msg, cause);
186         }
187     }
188 }