/** Starts specified clusters and connects to the hosts of this clusters. */ public void startClusters(final List<Cluster> selectedClusters) { final Set<Cluster> clusters = Tools.getConfigData().getClusters().getClusterSet(); if (clusters != null) { /* clusters */ for (final Cluster cluster : clusters) { if (selectedClusters != null && !selectedClusters.contains(cluster)) { continue; } Tools.getGUIData().addClusterTab(cluster); if (cluster.getHosts().isEmpty()) { continue; } final boolean ok = cluster.connect(null, true, 1); if (!ok) { Tools.getGUIData().getClustersPanel().removeTab(cluster); continue; } final Runnable runnable = new Runnable() { @Override public void run() { for (final Host host : cluster.getHosts()) { host.waitOnLoading(); } cluster.getClusterTab().addClusterView(); SwingUtilities.invokeLater( new Runnable() { public void run() { cluster.getClusterTab().requestFocus(); } }); } }; final Thread thread = new Thread(runnable); thread.start(); } } }
/** Saves data about clusters and hosts to the supplied output stream. */ public String saveXML(final OutputStream outputStream, final boolean saveAll) throws IOException { final String encoding = "UTF-8"; final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = null; try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) { assert false; } final Document doc = db.newDocument(); final Element root = (Element) doc.appendChild(doc.createElement("drbdgui")); if (Tools.getConfigData().getLoginSave()) { final String downloadUser = Tools.getConfigData().getDownloadUser(); final String downloadPasswd = Tools.getConfigData().getDownloadPassword(); if (downloadUser != null && downloadPasswd != null) { root.setAttribute(DOWNLOAD_USER_ATTR, downloadUser); root.setAttribute(DOWNLOAD_PASSWD_ATTR, downloadPasswd); } } final Element hosts = (Element) root.appendChild(doc.createElement("hosts")); for (final Host host : Tools.getConfigData().getHosts().getHostSet()) { if (!saveAll && !host.isSavable()) { continue; } host.setSavable(true); final String hostName = host.getHostname(); final String ip = host.getIp(); final String username = host.getUsername(); final String sshPort = host.getSSHPort(); final Boolean useSudo = host.isUseSudo(); final String color = host.getColor(); final Element hostNode = (Element) hosts.appendChild(doc.createElement(HOST_NODE_STRING)); hostNode.setAttribute(HOST_NAME_ATTR, hostName); hostNode.setAttribute(HOST_SSHPORT_ATTR, sshPort); if (color != null) { hostNode.setAttribute(HOST_COLOR_ATTR, color); } if (useSudo != null && useSudo) { hostNode.setAttribute(HOST_USESUDO_ATTR, "true"); } if (ip != null) { final Node ipNode = (Element) hostNode.appendChild(doc.createElement("ip")); ipNode.appendChild(doc.createTextNode(ip)); } if (username != null) { final Node usernameNode = (Element) hostNode.appendChild(doc.createElement("user")); usernameNode.appendChild(doc.createTextNode(username)); } } final Element clusters = (Element) root.appendChild(doc.createElement("clusters")); final Set<Cluster> clusterSet = Tools.getConfigData().getClusters().getClusterSet(); for (final Cluster cluster : clusterSet) { if (!saveAll && !cluster.isSavable()) { continue; } cluster.setSavable(true); final String clusterName = cluster.getName(); final Element clusterNode = (Element) clusters.appendChild(doc.createElement("cluster")); clusterNode.setAttribute(CLUSTER_NAME_ATTR, clusterName); for (final Host host : cluster.getHosts()) { final String hostName = host.getHostname(); final Element hostNode = (Element) clusterNode.appendChild(doc.createElement(HOST_NODE_STRING)); hostNode.appendChild(doc.createTextNode(hostName)); } } final TransformerFactory tf = TransformerFactory.newInstance(); Transformer t = null; try { t = tf.newTransformer(); t.setOutputProperty(OutputKeys.INDENT, "yes"); t.setOutputProperty(OutputKeys.METHOD, "xml"); t.setOutputProperty(OutputKeys.ENCODING, encoding); } catch (TransformerConfigurationException tce) { assert false; } final DOMSource doms = new DOMSource(doc); final StreamResult sr = new StreamResult(outputStream); try { t.transform(doms, sr); } catch (TransformerException te) { final IOException ioe = new IOException(); ioe.initCause(te); throw ioe; } return ""; }