|
42 | 42 | import org.apache.commons.cli.Options; |
43 | 43 | import org.apache.commons.cli.ParseException; |
44 | 44 | import org.apache.commons.collections4.CollectionUtils; |
| 45 | +import org.apache.commons.configuration2.Configuration; |
45 | 46 | import org.apache.commons.configuration2.PropertiesConfiguration; |
46 | 47 | import org.apache.commons.configuration2.PropertiesConfigurationLayout; |
| 48 | +import org.apache.commons.configuration2.builder.fluent.Parameters; |
| 49 | +import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; |
| 50 | +import org.apache.commons.configuration2.FileBasedConfiguration; |
47 | 51 | import org.apache.commons.configuration2.ex.ConfigurationException; |
48 | 52 | import org.apache.commons.io.FileUtils; |
49 | 53 | import org.apache.commons.io.FilenameUtils; |
@@ -145,28 +149,45 @@ private void run(String[] args) { |
145 | 149 | System.exit(0); |
146 | 150 | } |
147 | 151 |
|
148 | | - Properties configDefaults = new Properties(); |
| 152 | + Parameters params = new Parameters(); |
| 153 | + // Read data from this file |
| 154 | + File propertiesFile = new File(line.getOptionValue("c", "conf" + File.separator + "mirth-cli-config.properties")); |
| 155 | + |
| 156 | + FileBasedConfigurationBuilder<PropertiesConfiguration> builder = |
| 157 | + new FileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class) |
| 158 | + .configure(params.fileBased() |
| 159 | + .setFile(propertiesFile)); |
| 160 | + |
| 161 | + Configuration config = null; |
149 | 162 | try { |
150 | | - configDefaults.load(new FileInputStream(line.getOptionValue("c", "conf" + File.separator + "mirth-cli-config.properties"))); |
151 | | - } catch (IOException e) { |
152 | | - // Only error out if they tried to load the config |
| 163 | + // Load properties using Apache Commons Configuration2. The resulting config |
| 164 | + // instance supports variable interpolation, including environment variables |
| 165 | + // using the ${env:VARIABLE_NAME} syntax (for example ${env:MC_SERVER_URL}). |
| 166 | + config = builder.getConfiguration(); |
| 167 | + } catch (ConfigurationException cex) { |
| 168 | + // loading of the configuration file failed |
153 | 169 | if (line.hasOption("c")) { |
154 | | - error("We could not find the file: " + line.getOptionValue("c"), null); |
| 170 | + // An explicit configuration file was specified with -c; fail fast with a clear error |
| 171 | + error("Failed to load the configuration file specified with -c: " + line.getOptionValue("c"), cex); |
155 | 172 | System.exit(2); |
156 | 173 | } |
| 174 | + error("Loading configuration failed.", null); |
| 175 | + System.exit(2); |
157 | 176 | } |
| 177 | + if (config != null) { |
| 178 | + String server = line.getOptionValue("a", config.getString("address")); |
| 179 | + String user = line.getOptionValue("u", config.getString("user")); |
| 180 | + String password = line.getOptionValue("p", config.getString("password")); |
| 181 | + String script = line.getOptionValue("s", config.getString("script")); |
158 | 182 |
|
159 | | - String server = line.getOptionValue("a", configDefaults.getProperty("address")); |
160 | | - String user = line.getOptionValue("u", configDefaults.getProperty("user")); |
161 | | - String password = line.getOptionValue("p", configDefaults.getProperty("password")); |
162 | | - String script = line.getOptionValue("s", configDefaults.getProperty("script")); |
| 183 | + if ((server != null) && (user != null) && (password != null)) { |
| 184 | + runShell(server, user, password, script, line.hasOption("d")); |
| 185 | + } else { |
| 186 | + new HelpFormatter().printHelp("Shell", options); |
| 187 | + error("all of address, user, password, and version options must be supplied as arguments or in the default configuration file", null); |
| 188 | + System.exit(2); |
| 189 | + } |
163 | 190 |
|
164 | | - if ((server != null) && (user != null) && (password != null)) { |
165 | | - runShell(server, user, password, script, line.hasOption("d")); |
166 | | - } else { |
167 | | - new HelpFormatter().printHelp("Shell", options); |
168 | | - error("all of address, user, password, and version options must be supplied as arguments or in the default configuration file", null); |
169 | | - System.exit(2); |
170 | 191 | } |
171 | 192 | } catch (ParseException e) { |
172 | 193 | error("Could not parse input arguments.", e); |
|
0 commit comments