use of org.opennms.netmgt.config.vmware.VmwareConfig in project opennms by OpenNMS.
the class VmwareRequisitionTool method main.
public static void main(String[] args) throws Exception {
final Options options = new Options();
final CommandLineParser parser = new PosixParser();
final CommandLine cmd = parser.parse(options, args);
@SuppressWarnings("unchecked") List<String> arguments = (List<String>) cmd.getArgList();
if (arguments.size() < 1) {
usage(options, cmd);
System.exit(1);
}
// Internal trick to avoid confusions.
String urlString = arguments.remove(0).replaceFirst("vmware", "http");
URL url = new URL(urlString);
// Parse vmware-config.xml and retrieve the credentials to avoid initialize Spring
if (!url.getQuery().contains("username") && url.getUserInfo() == null) {
File cfg = new File(ConfigFileConstants.getFilePathString(), "vmware-config.xml");
if (cfg.exists()) {
String username = null;
String password = null;
VmwareConfig config = JaxbUtils.unmarshal(VmwareConfig.class, cfg);
for (VmwareServer srv : config.getVmwareServerCollection()) {
if (srv.getHostname().equals(url.getHost())) {
username = srv.getUsername();
password = srv.getPassword();
}
}
if (username == null || password == null) {
throw new IllegalArgumentException("Can't retrieve credentials for " + url.getHost() + " from " + cfg);
}
// Add credentials to URL
urlString = urlString + ";username=" + username + ";password=" + password;
url = new URL(urlString);
}
}
VmwareRequisitionUrlConnection c = new VmwareRequisitionUrlConnection(url) {
@Override
protected Requisition getExistingRequisition(String foreignSource) {
// This is not elegant but it is necessary to avoid booting Spring
File req = new File(ConfigFileConstants.getFilePathString(), "imports" + File.separator + foreignSource + ".xml");
if (req.exists()) {
return JaxbUtils.unmarshal(Requisition.class, req);
}
return null;
}
};
c.connect();
InputStream is = c.getInputStream();
if (is == null) {
System.err.println("Couldn't generate requisition from " + urlString);
System.exit(1);
} else {
System.out.println(IOUtils.toString(is, StandardCharsets.UTF_8));
}
}
Aggregations