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  import java.util.Collection;
20  import java.util.List;
21  
22  import org.apache.commons.configuration.XMLConfiguration;
23  import org.apache.commons.lang.StringUtils;
24  
25  /**
26   * This class represent the parsed XML configuration file.
27   * 
28   * @author Massimiliano Ziccardi
29   */
30  class XmlJNRPEConfiguration extends JNRPEConfiguration {
31  
32  	@Override
33  	public void load(final File confFile) throws ConfigurationException {
34  		// Parse an ini file
35  		ServerSection serverConf = getServerSection();
36  		CommandsSection commandSection = getCommandSection();
37  		try {
38  			XMLConfiguration confParser = new XMLConfiguration(confFile);
39  
40  			List<Object> vBindAddresses = confParser
41  					.getList("server.bind[@address]");
42  			int iBindListSize = vBindAddresses.size();
43  
44  			for (int i = 0; i < iBindListSize; i++) {
45  				String sAddress = confParser.getString("server.bind(" + i
46  						+ ")[@address]");
47  				String useSSL = confParser.getString("server.bind(" + i
48  						+ ")[@SSL]");
49  				if (useSSL == null) {
50  					useSSL = "false";
51  				}
52  
53  				serverConf.addBindAddress(sAddress,
54  						useSSL.equalsIgnoreCase("true"));
55  			}
56  
57  			String sAcceptParams = confParser.getString(
58  					"server[@accept-params]", "true");
59  
60  			serverConf.setAcceptParams(sAcceptParams.equalsIgnoreCase("true")
61  					|| sAcceptParams.equalsIgnoreCase("yes"));
62  
63  			serverConf.setPluginPath(confParser.getString(
64  					"server.plugin[@path]", "."));
65  
66  			serverConf.setBackLogSize(confParser.getInt(
67  					"server[@backlog-size]", ServerSection.DEFAULT_BACKLOG));
68  
69  			// TODO : move this to publicly accessible constants
70  			serverConf.setReadTimeout(confParser.getInteger(
71  					"server[@read-timeout]", 10));
72  			// TODO : move this to publicly accessible constants
73  			serverConf.setWriteTimeout(confParser.getInteger(
74  					"server[@write-timeout]", 60));
75  
76  			List<Object> vAllowedAddresses = confParser
77  					.getList("server.allow[@ip]");
78  			if (vAllowedAddresses != null) {
79  				for (Object address : vAllowedAddresses) {
80  					serverConf.addAllowedAddress((String) address);
81  				}
82  			}
83  
84  			// Get the commands count ( searching for a better way...)
85  			// int iCommandsCount =
86  			Object obj = confParser.getProperty("commands.command[@name]");
87  
88  			int iCount = 0;
89  
90  			if (obj != null) {
91  				if (obj instanceof List) {
92  					iCount = ((List) obj).size();
93  				} else {
94  					iCount = 1;
95  				}
96  			}
97  
98  			// Loop through configured commands
99  			for (int i = 0; i < iCount; i++) {
100 				String sCommandName = (String) confParser
101 						.getProperty("commands.command(" + i + ")[@name]");
102 				String sPluginName = (String) confParser
103 						.getProperty("commands.command(" + i
104 								+ ")[@plugin_name]");
105 				String sWholeCommandLine = (String) confParser
106 						.getProperty("commands.command(" + i + ")[@params]");
107 				if (sWholeCommandLine == null) {
108 					sWholeCommandLine = "";
109 				} else {
110 					sWholeCommandLine += " ";
111 				}
112 
113 				// Loop through command arguments...
114 
115 				Object argsObj = confParser.getProperty("commands.command(" + i
116 						+ ").arg[@name]");
117 				int iArgsCount = 0;
118 				if (argsObj != null) {
119 					if (argsObj instanceof List) {
120 						iArgsCount = ((List) argsObj).size();
121 					} else {
122 						iArgsCount = 1;
123 					}
124 				}
125 
126 				StringBuffer commandLineBuffer = new StringBuffer(
127 						sWholeCommandLine);
128 
129 				for (int j = 0; j < iArgsCount; j++) {
130 					String sArgName = (String) confParser
131 							.getProperty("commands.command(" + i + ").arg(" + j
132 									+ ")[@name]");
133 
134 					Object value = confParser.getProperty("commands.command("
135 							+ i + ").arg(" + j + ")[@value]");
136 
137 					String sArgValue = null;
138 					if (value instanceof String) {
139 						sArgValue = (String) value;
140 					} else if (value instanceof Collection) {
141 						sArgValue = StringUtils.join((Collection) value,
142 								confParser.getListDelimiter());
143 					}
144 
145 					if (sArgName.length() > 1) {
146 						commandLineBuffer.append("--");
147 					} else {
148 						commandLineBuffer.append("-");
149 					}
150 
151 					commandLineBuffer.append(sArgName).append(" ");
152 
153 					if (sArgValue != null) {
154 						boolean bQuote = sArgValue.indexOf(' ') != -1;
155 
156 						// FIXME : handle quote escaping...
157 						if (bQuote) {
158 							commandLineBuffer.append("\"");
159 						}
160 
161 						commandLineBuffer.append(sArgValue);
162 
163 						if (bQuote) {
164 							commandLineBuffer.append("\"");
165 						}
166 
167 						commandLineBuffer.append(" ");
168 
169 					}
170 				}
171 				sWholeCommandLine = commandLineBuffer.toString();
172 
173 				commandSection.addCommand(sCommandName, sPluginName,
174 						sWholeCommandLine);
175 			}
176 		} catch (org.apache.commons.configuration.ConfigurationException e) {
177 			throw new ConfigurationException(e);
178 		}
179 	}
180 }