1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
29
30
31
32 class IniJNRPEConfiguration extends JNRPEConfiguration {
33
34 @Override
35 public void load(final File confFile) throws ConfigurationException {
36
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
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
63 serverConf.setReadTimeout(confParser.getInteger(
64 "server.read-timeout", 10));
65
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
86
87 String[] vElements = StringUtils.split(sCommandLine, false);
88 String sPluginName = vElements[0];
89
90
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
110
111
112
113
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 }