📄 xmlscriptinterpreter.java
字号:
this.requestId = atts.getValue("requestId"); String typeStr = atts.getValue("type"); if (typeStr!=null && typeStr.length() > 0) this.type = typeStr.getBytes()[0]; else this.type = MsgInfo.INVOKE_BYTE; // 'I' return; } if ("replaceTokens".equals(qName)) { this.replaceTokens = true; return; } if ("replaceKeyTokens".equals(qName)) { this.replaceKeyTokens = true; return; } if (KEY_TAG.equals(qName)) { this.inKey++; this.key.setLength(0); this.key.append(this.writeElementStart(qName, atts)); return; } if ("replaceQosTokens".equals(qName)) { this.replaceQosTokens = true; return; } if (QOS_TAG.equals(qName)) { this.inQos++; this.qos.setLength(0); this.qos.append(this.writeElementStart(qName, atts)); return; } if ("replaceContentTokens".equals(qName)) { this.replaceContentTokens = true; return; } if (CONTENT_TAG.equals(qName)) { this.inContent++; this.link = null; this.content.setLength(0); String sizeStr = atts.getValue("size"); // long String type = atts.getValue("type"); // byte[] String encoding = atts.getValue("encoding"); // base64 this.contentData = new EncodableData(CONTENT_TAG, null, type, encoding); try { if (sizeStr != null) { long size = Long.valueOf(sizeStr).longValue(); if (size > 0) this.contentData.setSize(size); } } catch (NumberFormatException e) {} String tmp = atts.getValue("link"); if (tmp != null && tmp.length() > 0) this.link = tmp; // this.content.append(this.writeElementStart(qName, atts)); return; } if (WAIT_TAG.equals(qName)) { String message = atts.getValue("message"); if (message != null) { message = replaceVariables(message); System.out.println(message); } this.waitNumUpdates = 0; String tmp = atts.getValue("updates"); tmp = replaceVariables(tmp); if (tmp != null) { try { this.waitNumUpdates = Integer.parseInt(tmp.trim()); } catch (Throwable e) { } } long delay = Integer.MAX_VALUE; tmp = atts.getValue("delay"); tmp = replaceVariables(tmp); if (tmp != null) { try { delay = Long.parseLong(tmp.trim()); if (delay == 0) delay = Integer.MAX_VALUE; } catch (Throwable e) { } } if (this.waitNumUpdates > 0 || delay > 0) { synchronized (this.waitMutex) { if (this.waitNumUpdates > 0 && this.updateCounter >= this.waitNumUpdates) { // updates have arrived already } else { try { waitMutex.wait(delay); } catch (InterruptedException e) { } if (this.waitNumUpdates == 0 || this.updateCounter < this.waitNumUpdates) { log.info("wait timeout occurred after " + delay + " milli."); } this.updateCounter = 0; } } } return; } if (ECHO_TAG.equals(qName)) { // console output <echo message="Hello"/> String message = atts.getValue("message"); if (message == null) { message = ""; } message = replaceVariables(message); System.out.println(message); return; } if (INPUT_TAG.equals(qName)) { // User input from console <input message="Hit a key: " delay="100"/> String inputMessage = atts.getValue("message"); if (inputMessage == null) { inputMessage = "Hit a key to continue> "; } inputMessage = replaceVariables(inputMessage); // this.validargs = atts.getValue("validargs"); "y/n" { // Wait a bit to have updates processed String tmp = atts.getValue("delay"); if (tmp == null) { tmp = "500"; } tmp = replaceVariables(tmp); try { long delay = Long.parseLong(tmp.trim()); Thread.sleep(delay); } catch (Throwable e) { } } /*int ret = */Global.waitOnKeyboardHit(inputMessage); return; } if (ROOT_TAG.equals(qName)) { // "xmlBlaster" String id = atts.getValue("id"); this.response = new StringBuffer("<").append(ROOTRESPONSE_TAG); if (id != null) this.response.append(" id='").append(id).append("'"); this.response.append(">\n"); this.needsRootEndTag = true; return; } if ("property".equals(qName)) { this.inProperty = true; // <property name='transactionId'>Something</property> this.propertyName = atts.getValue("name"); return; } } private String replaceVariables(String template) { if (!replaceTokens) return template; ReplaceVariable r = new ReplaceVariable(); String result = r.replace(template, new I_ReplaceVariable() { public String get(String key) { return glob.getProperty().get(key, key); } }); return result; } /** * Appends the end stream to the current StringBuffer * @param buf the StringBuffer to be used * @param qName the name of the command */ private void appendEndOfElement(StringBuffer buf, String qName) { buf.append("</"); buf.append(qName); buf.append('>'); } /** * Replace e.g. ${XY} with the variable from global properties. * @param text The complete string which may contain zero to many ${...} * variables, if null we return null * @return The new value where all found ${} are replaced. */ public String replaceVariable(String text) { if (text == null) return null; int lastFrom = -1; for (int i=0; i<20; i++) { // max recursion/replacement depth int from = text.indexOf("${"); if (from == -1) return text; if (lastFrom != -1 && lastFrom == from) return text; // recursion int to = text.indexOf("}", from); if (to == -1) return text; String key = text.substring(from+2, to); String value = glob.getProperty().get(key, "${"+key+"}"); text = text.substring(0,from) + value + text.substring(to+1); lastFrom = from; } return text; } /** * On each remote method invocation this function is called. * @param methodName * @param type 'I'=invoke 'R'=response 'E'=exception * @return true: The methodName was known and is successfully processed * false: The methodName is not known and nothing is processed * @throws XmlBlasterException Will lead to stop parsing further */ abstract public boolean fireMethod(MethodName methodName, String sessionId, String requestId, byte type) throws XmlBlasterException; /** * Set a property into Global scope. */ abstract public void setProperty(String key, String value) throws XmlBlasterException; /** * Fires the given xmlBlaster command and sends the response to the output stream * @param qName */ private void fireCommand(String qName) throws XmlBlasterException { if (replaceQosTokens) { String tmp = this.qos.toString(); this.qos.setLength(0); this.qos.append(replaceVariable(tmp)); } if (replaceKeyTokens) { String tmp = this.key.toString(); this.key.setLength(0); this.key.append(replaceVariable(tmp)); } if (replaceContentTokens) { String tmp = this.content.toString(); this.content.setLength(0); this.content.append(replaceVariable(tmp)); } if (QOS_TAG.equals(qName)) { this.inQos--; return; } if (KEY_TAG.equals(qName)) { this.inKey--; return; } if (CONTENT_TAG.equals(qName)) { this.inContent--; return; } boolean processed = fireMethod(MethodName.toMethodName(qName), this.sessionId, this.requestId, this.type); if (processed) { if (this.content != null) this.content.setLength(0); if (this.qos != null) this.qos.setLength(0); if (this.key != null) this.key.setLength(0); return; } } protected void flushResponse() throws XmlBlasterException { try { if (this.out != null) { if (log.isLoggable(Level.FINE)) log.fine("Sending response: " + this.response.toString()); synchronized(this.out) { this.out.write(this.response.toString().getBytes("UTF-8")); } } } catch (IOException ex) { log.severe("flushResponse exception occured " + ex.getMessage()); throw XmlBlasterException.convert(this.glob, ME, ErrorCode.INTERNAL_UNKNOWN.toString(), ex); } finally { this.response = new StringBuffer(); } } protected MsgUnit buildMsgUnit() throws XmlBlasterException { byte[] currentContent = null; if (this.link == null) currentContent = (this.contentData == null) ? new byte[0] : this.contentData.getBlobValue(); else { if (this.attachments != null && this.attachments.containsKey(this.link)) { Object obj = this.attachments.get(this.link); if (obj instanceof String) { if (this.contentData == null) this.contentData = new EncodableData(CONTENT_TAG, null, Constants.TYPE_STRING, Constants.ENCODING_NONE); this.contentData.setValueRaw((String)obj); currentContent = this.contentData.getBlobValue(); } else { currentContent = (byte[])obj; } } else { throw new XmlBlasterException(this.glob, ErrorCode.USER_ILLEGALARGUMENT, ME, "buildMsgUnit: the attachment '" + this.link + "' was not found"); // throw exception } } MsgUnit msgUnit = new MsgUnit(this.key.toString(), currentContent, this.qos.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -