📄 hostinfoprovider.java.real
字号:
this.setState(PROVIDER_ERROR); this.errorString = e.getMessage(); logger.error(errorString); break; } } logger.debug("Thread terminating."); synchronized (this) { this.notify(); } } public void terminate() { logger.debug("Sending thread termination message."); synchronized (this) { this.setState(PROVIDER_TERMINATED); } } private Document composeDocument() throws javax.xml.parsers.ParserConfigurationException { Document doc = XMLUtils.newDocument();try { Node imported; Enumeration en = this.components.elements(); // TODO: populate the host element with appropriate attributes. Element host;/* Element subcluster; Element cluster; */ ExecutionThread tr;/* tr = ((ExecutionThread)en.nextElement()); imported = tr.recentResult; imported = imported.getFirstChild(); cluster = (Element)doc.importNode(imported,true); tr = ((ExecutionThread)en.nextElement()); imported = tr.recentResult; imported = imported.getFirstChild(); subcluster = (Element)doc.importNode(imported,true);*/ tr = ((ExecutionThread)en.nextElement()); imported = tr.recentResult; imported = imported.getFirstChild(); host = (Element)doc.importNode(imported,true);/* subcluster.appendChild(host); cluster.appendChild(subcluster); doc.appendChild(cluster);*/ doc.appendChild(host); logger.debug("Composing document"); for(;en.hasMoreElements();) { try { ExecutionThread executionThread = ((ExecutionThread)en.nextElement()); imported = executionThread.recentResult; if(imported!=null) { imported = imported.getFirstChild(); if(executionThread.stripOuter) { imported = imported.getFirstChild(); } /* we could get a null pointer exception above, but it is caught by the exception handling below. */ while(imported!=null) { host.appendChild(doc.importNode(imported,true)); imported=imported.getNextSibling(); } } } catch(Exception e) { logger.error("Exception whilst handling a component: "+e); /* we don't kill the for loop here, so if one script's structures get messed up, we still look at the others. */ } }// if (logger.isDebugEnabled()) { logger.debug("Composite document is "+DOM2Writer.nodeToString(doc));// } return doc;} catch(NullPointerException npex){ return null;} /*catch(Exception ex) // TODO: better exception handling here{ logger.warn("Exception assembing document"+ex); return null;}*//* catch(Exception ex){ logger.error("General exception in composeDocument: "+ex); throw ex;} */ } /* * The SimpleDataProvider methods are implemented below as well */ /** this should perform a one-off query of the provider, but it probably won't work properly as we need to have started up most of the async stuff. */ public void update(String argStr) throws Exception { parseArgs(argStr); synchronized(this) { Document resultDoc = composeDocument(); if (resultDoc != null) this.doc = resultDoc; } } protected void parseArgs(String args) throws Exception { StringTokenizer toker = new StringTokenizer(args); String token = ""; while (toker.hasMoreTokens()) { token = toker.nextToken(); if (token.compareToIgnoreCase("-i") == 0) { this.frequencyMillis = Long.parseLong(toker.nextToken()); continue; } else if (token.compareToIgnoreCase("-f") == 0) { setSampleFile(toker.nextToken()); continue; } else throw new Exception("Unknown Argument: " + token); } } public void output(java.io.OutputStream outStream) throws Exception { synchronized(this) { XMLUtils.DocumentToStream(this.doc,outStream); } } public void run(String args, java.io.OutputStream outStream) throws Exception { synchronized(this) { try { this.update(args); this.output(outStream); } catch (Exception e) { logger.error(e.getMessage()); throw e; } } } private Document loadConfigFile() throws Exception { InputStream input = null; Document doc = null; try { input = new FileInputStream(this.configFile); if (input != null) { doc = XMLUtils.newDocument(input); } } catch (Exception e) { if (input != null) { input.close(); } this.errorString = "Config file loading error: " + e.getMessage(); logger.warn(errorString); } return doc; }/** One ExecutionThread is created to handle each script. */class ExecutionThread extends Thread{ /** refresh period, in ms */ private long period = 20000; private String commandLine = null; /** contains the most recent complete result. must either be null or a complete usable document. */ Document recentResult = null; /** if true, we'll strip of the outer element of the result and add in each contained element to the composite result separately. */ boolean stripOuter = false; ExecutionThread(String commandLine, long period, boolean stripOuter) { this.commandLine = commandLine; this.period = period; this.stripOuter = stripOuter; logger.debug("Creating ExecutionThread for "+commandLine+" with period of "+period+"ms"); } public void run() { /* we'll fall out of this loop (and hence the thread will go away) when the enclosing provider finishes or dies. */ while (getState() != PROVIDER_TERMINATED && getState() != PROVIDER_ERROR) { logger.debug("Executing "+commandLine); Runtime runtime = Runtime.getRuntime(); try { int scriptExitCode; Process process; // start the script process = runtime.exec(commandLine); // parse the results and store for later logger.debug("Parsing results of "+commandLine); InputStream processInputStream = process.getInputStream(); this.recentResult = XMLUtils.newDocument(processInputStream); // TODO: is it the case that builder.parse can return // non-null / non-valid Documents ?// if (logger.isDebugEnabled()) { logger.debug("Parsed result of "+commandLine+" is "+DOM2Writer.nodeToString(this.recentResult));// } // TODO: there should be a timeout on this so that // if a script never exits, we eventually kill it // (and it'll run again) /* waiting for the script to exit means that multiple instances of long running / hanging scripts won't build up. */ scriptExitCode = process.waitFor(); processInputStream.close(); process.destroy(); /* examine error code following standard unix semantics, we expect a zero exit code to indicate successful completion. if non-zero, we log a warning, but other than that we don't change the way that we handle the output. */ if(0!=scriptExitCode) { logger.warn(commandLine+" returned a non-zero exit code: "+scriptExitCode); } // notify upwards that a change has occurred synchronized(updateLock) { updateLock.notifyAll(); } } catch (Exception e) { String errorString = "Execution error: " + e.getMessage ()+ " while processing "+commandLine; logger.error(errorString); // can't send this exception anywhere // throw new Exception(errorString); } try { // now wait a while before we go round again logger.debug("Sleeping after "+commandLine); thread.sleep(this.period); } catch (Exception e) { String errorString = "Sleep error: " + e.getMessage ()+ " while processing "+commandLine; logger.error(errorString); // can't send this exception anywhere // throw new Exception(errorString); } } }}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -