View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2007, 2014 Massimiliano Ziccardi
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *******************************************************************************/
16  package it.jnrpe.server;
17  
18  import java.io.File;
19  
20  /**
21   * Factory of configuration object.
22   *
23   * @author Massimiliano Ziccardi
24   */
25  final class JNRPEConfigurationFactory {
26  
27      /**
28       * Default constructor.
29       */
30      private JNRPEConfigurationFactory() {
31  
32      }
33  
34      /**
35       * Creates a configuration object from the passed in configuration file.
36       *
37       * @param configurationFilePath
38       *            Path to the configuration file
39       * @return The configuration object
40       * @throws ConfigurationException
41       *             -
42       */
43      public static JNRPEConfiguration createConfiguration(
44              final String configurationFilePath) throws ConfigurationException {
45          JNRPEConfiguration conf = null;
46  
47          if (configurationFilePath.toLowerCase().endsWith(".conf")
48                  || configurationFilePath.toLowerCase().endsWith(".ini")) {
49              conf = new IniJNRPEConfiguration();
50          } else if (configurationFilePath.toLowerCase().endsWith(".xml")) {
51              conf = new XmlJNRPEConfiguration();
52          }
53  
54          if (conf == null) {
55              throw new ConfigurationException(
56                      "Config file name must end with either '.ini' "
57                      + "(ini file) or '.xml' (xml file)");
58          }
59  
60          conf.load(new File(configurationFilePath));
61  
62          return conf;
63      }
64  }