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 it.jnrpe.utils.StringUtils;
19  
20  import java.io.File;
21  import java.util.Iterator;
22  import java.util.List;
23  
24  import org.apache.commons.configuration.HierarchicalINIConfiguration;
25  import org.apache.commons.configuration.SubnodeConfiguration;
26  
27  /**
28   * This class represent an INI file configuration.
29   * 
30   * @author Massimiliano Ziccardi
31   */
32  class IniJNRPEConfiguration extends JNRPEConfiguration {
33  
34  	@Override
35  	public void load(final File confFile) throws ConfigurationException {
36  		// Parse an ini file
37  		ServerSection serverConf = getServerSection();
38  		CommandsSection commandSection = getCommandSection();
39  		try {
40  			HierarchicalINIConfiguration confParser = new HierarchicalINIConfiguration(
41  					confFile);
42  
43  			List<Object> vBindAddresses = confParser
44  					.getList("server.bind-address");
45  			if (vBindAddresses != null) {
46  				for (Object address : vBindAddresses) {
47  					serverConf.addBindAddress((String) address);
48  				}
49  			}
50  
51  			// Defaults accept params
52  			String sAcceptParams = confParser.getString("server.accept-params",
53  					"true");
54  			serverConf.setAcceptParams(sAcceptParams.equalsIgnoreCase("true")
55  					|| sAcceptParams.equalsIgnoreCase("yes"));
56  			serverConf.setPluginPath(confParser.getString("server.plugin-path",
57  					"."));
58  
59  			serverConf.setBackLogSize(confParser.getInt("server.backlog-size",
60  					ServerSection.DEFAULT_BACKLOG));
61  
62  			// TODO : move this to publicly accessible constants
63  			serverConf.setReadTimeout(confParser.getInteger(
64  					"server.read-timeout", 10));
65  			// TODO : move this to publicly accessible constants
66  			serverConf.setWriteTimeout(confParser.getInteger(
67  					"server.write-timeout", 60));
68  
69  			List<Object> vAllowedAddresses = confParser
70  					.getList("server.allow-address");
71  			if (vAllowedAddresses != null) {
72  				for (Object address : vAllowedAddresses) {
73  					serverConf.addAllowedAddress((String) address);
74  				}
75  			}
76  
77  			SubnodeConfiguration sc = confParser.getSection("commands");
78  
79  			if (sc != null) {
80  				for (Iterator<String> it = sc.getKeys(); it.hasNext();) {
81  					String sCommandName = it.next();
82  
83  					String sCommandLine = org.apache.commons.lang.StringUtils
84  							.join(sc.getStringArray(sCommandName), ",");
85  					// first element of the command line is the plugin name
86  
87  					String[] vElements = StringUtils.split(sCommandLine, false);
88  					String sPluginName = vElements[0];
89  
90  					// Rebuilding the commandline
91  					StringBuffer cmdLine = new StringBuffer();
92  
93  					for (int i = 1; i < vElements.length; i++) {
94  						cmdLine.append(quoteAndEscape(vElements[i]))
95  								.append(" ");
96  					}
97  
98  					commandSection.addCommand(sCommandName, sPluginName,
99  							cmdLine.toString());
100 				}
101 			}
102 		} catch (org.apache.commons.configuration.ConfigurationException e) {
103 			throw new ConfigurationException(e);
104 		}
105 
106 	}
107 
108 	/**
109 	 * Quotes a string.
110 	 * 
111 	 * @param string
112 	 *            The string to be quoted
113 	 * @return The quoted string
114 	 */
115 	private String quoteAndEscape(final String string) {
116 		if (string.indexOf(' ') == -1) {
117 			return string;
118 		}
119 
120 		String res = "\"" + string.replaceAll("\"", "\\\"") + "\"";
121 		return res;
122 	}
123 
124 }