📄 eventplugin.java
字号:
/** * Create an XML xmlBlaster dump which contains the most important status informations. * Follows the admin.commands markup (without the root tag <xmlBlaster> * @param g The global of the running server instance * @return The XML dump * @see <a href="http://www.xmlBlaster.org/xmlBlaster/doc/requirements/admin.commands.html">The admin.commands requirement</a> */ public static String createStatusDump(org.xmlBlaster.engine.ServerScope g, String summary, String description, String eventType, String errorCode) { // Change to be configurable with ${amdincommands} replacements RequestBroker r = g.getRequestBroker(); XmlBuffer buf = new XmlBuffer(10000); //buf.append("\n").append("<xmlBlaster>"); // Root tag not added, so we easily can collect different nodes to a big xml dump buf.append("\n ").append("<node id='").appendEscaped(g.getId()).append("'>"); if (summary != null) buf.append("\n ").append("<_summary>").appendEscaped(summary).append("</_summary>"); if (description != null) buf.append("\n ").append("<_description>").appendEscaped(description).append("</_description>"); if (eventType != null) buf.append("\n ").append("<_eventType>").append(eventType).append("</_eventType>"); if (errorCode != null) buf.append("\n ").append("<_errorCode>").append(errorCode).append("</_errorCode>"); buf.append("\n ").append("<uptime>").append(r.getUptime()).append("</uptime>"); buf.append("\n ").append("<runlevel>").append(g.getRunlevelManager().getCurrentRunlevel()).append("</runlevel>"); buf.append("\n ").append("<instanceId>").appendEscaped(g.getInstanceId()).append("</instanceId>"); buf.append("\n ").append("<version>").append(g.getVersion()).append("</version>"); buf.append("\n ").append("<revisionNumber>").append(g.getRevisionNumber()).append("</revisionNumber>"); buf.append("\n ").append("<freeMem>").append(r.getFreeMem()).append("</freeMem>"); buf.append("\n ").append("<maxFreeMem>").append(r.getMaxFreeMem()).append("</maxFreeMem>"); buf.append("\n ").append("<maxMem>").append(r.getMaxMem()).append("</maxMem>"); buf.append("\n ").append("<usedMem>").append(r.getUsedMem()).append("</usedMem>"); buf.append("\n ").append("<serverTimestamp>").append(new java.sql.Timestamp(new java.util.Date().getTime()).toString()).append("</serverTimestamp>"); buf.append("\n ").append("<numClients>").append(r.getNumClients()).append("</numClients>"); buf.append("\n ").append("<clientList>").appendEscaped(r.getClientList()).append("</clientList>"); SubjectInfo[] clients = r.getAuthenticate().getSubjectInfoArr(); for (int c=0; c<clients.length; c++) { SubjectInfo subjectInfo = clients[c]; if (subjectInfo.getLoginName().startsWith("__")) continue;// Ignore internal sessions buf.append("\n ").append("<client id='").appendEscaped(subjectInfo.getLoginName()).append("'>"); SessionInfo[] sessions = subjectInfo.getSessions(); for (int s=0; s<sessions.length; s++) { SessionInfo sessionInfo = sessions[s]; buf.append("\n ").append("<session id='").append(sessionInfo.getPublicSessionId()).append("'>"); buf.append("\n ").append("<state>").append(sessionInfo.getConnectionState()).append("</state>"); ClientProperty[] props = sessionInfo.getRemotePropertyArr(); for (int p=0; p<props.length; p++) buf.append(props[p].toXml(" ", "remoteProperty", true)); buf.append("\n ").append("</session>"); } buf.append("\n ").append("</client>"); } buf.append("\n ").append("<numTopics>").append(r.getNumTopics()).append("</numTopics>"); buf.append("\n ").append("<topicList>").appendEscaped(r.getTopicList()).append("</topicList>"); buf.append("\n ").append("<numGet>").append(r.getNumGet()).append("</numGet>"); buf.append("\n ").append("<numPublish>").append(r.getNumPublish()).append("</numPublish>"); buf.append("\n ").append("<numUpdate>").append(r.getNumUpdate()).append("</numUpdate>"); // " encoding='base64'" if string contains CDATA? String warning = ReplaceVariable.replaceAll(r.getLastWarning(), "<![CDATA[", "<![CDATA["); warning = ReplaceVariable.replaceAll(warning, "]]>", "]]>"); String error = ReplaceVariable.replaceAll(r.getLastError(), "<![CDATA[", "<![CDATA["); error = ReplaceVariable.replaceAll(error, "]]>", "]]>"); buf.append("\n ").append("<lastWarning><![CDATA[").append(warning).append("]]></lastWarning>"); buf.append("\n ").append("<lastError><![CDATA[").append(error).append("]]></lastError>"); XmlBlasterException e = new XmlBlasterException(g, ErrorCode.COMMUNICATION_NOCONNECTION, ME, ""); buf.append("\n ").append("<versionInfo><![CDATA[").appendEscaped(e.getVersionInfo()).append("]]></versionInfo>"); buf.append("\n ").append("<see>").append("http://www.xmlBlaster.org/xmlBlaster/doc/requirements/admin.events.html").append("</see>"); buf.append("\n ").append("</node>"); //buf.append("\n").append("</xmlBlaster>"); return buf.toString(); } /** * Initialize email sending. * @param destination The configuration string, a comma separated list of key/value properties, e.g. * <code>mail.smtp.from=xmlBlaster@localhost,mail.smtp.to=demo@localhost,mail.collectMillis=10000</code> * @throws XmlBlasterException */ public void setupSmtpSink(String destination) throws XmlBlasterException { if (destination != null && destination.trim().length() > 0) { synchronized(this.smtpDestinationMonitor) { this.smtpDestinationHelper = new SmtpDestinationHelper(getSmtpClient(), destination); //if (this.smtpDestination.collectIntervall > 0) this.smtpTimeout = new Timeout("EventPlugin-SmtpTimer"); // we need it allways to synchronize } } } /** * Replace some $_{} tokens. * * @param str The string to check and replace * @param summary The value for a $_{summary}, can be null * @param description The value for a $_{description}, can be null * @param eventType The value for a $_{eventType}, can be null * @param errorCode The value for a $_{errorCode}, can be null * @return Resolved string */ protected String replaceTokens(String str, String summary, String description, String eventType, String errorCode) { if (str == null || str.indexOf("$") == -1) return str; str = ReplaceVariable.replaceAll(str, "$_{datetime}", new java.sql.Timestamp(new java.util.Date().getTime()).toString()); str = ReplaceVariable.replaceAll(str, "$_{summary}", (summary==null)?"":summary); str = ReplaceVariable.replaceAll(str, "$_{description}", (description==null)?"":description); str = ReplaceVariable.replaceAll(str, "$_{instanceId}", this.engineGlob.getInstanceId()); // "/xmlBlaster/node/heron/instanceId/1136220586692" str = ReplaceVariable.replaceAll(str, "$_{nodeId}", this.engineGlob.getId()); // "heron" str = ReplaceVariable.replaceAll(str, "$_{id}", this.engineGlob.getId()); // "heron" str = ReplaceVariable.replaceAll(str, "$_{eventType}", (eventType==null)?"":eventType); str = ReplaceVariable.replaceAll(str, "$_{errorCode}", (errorCode==null)?"":errorCode); if (str.indexOf("$_{clientList}") != -1) { // To support backward compatibility with "userListEvent=true" __sys__UserList str = ReplaceVariable.replaceAll(str, "$_{clientList}", this.requestBroker.getClientList()); } if (str.indexOf("$_{versionInfo}") != -1) { XmlBlasterException e = new XmlBlasterException(this.engineGlob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, ""); str = ReplaceVariable.replaceAll(str, "$_{versionInfo}", e.getVersionInfo()); } if (str.indexOf("$_{xml}") != -1) { str = ReplaceVariable.replaceAll(str, "$_{xml}", createStatusDump(this.engineGlob, summary, description, eventType, errorCode)); return str; } // TODO: Support admin commands or JMX calls like // $_{org.xmlBlaster:nodeClass=node,node="heron"/action=getFreeMemStr'} return str; } /** * @return the plugin type, defaults to "EventPlugin" * @see org.xmlBlaster.util.plugin.I_Plugin#getType() */ public String getType() { if (this.pluginConfig != null) return this.pluginConfig.getType(); return ME; } /** * @return the plugin version, defaults to "1.0" * @see org.xmlBlaster.util.plugin.I_Plugin#getVersion() */ public String getVersion() { if (this.pluginConfig != null) return this.pluginConfig.getVersion(); return "1.0"; } /** * Shutdown the plugin * * @see org.xmlBlaster.util.plugin.I_Plugin#shutdown() */ public void shutdown() throws XmlBlasterException { if (this.isShutdown) return; // TODO: Check if we unregister everything! // TODO: Protect each call with catch Throwable // we shut down the resources for the queue and storage events ServerScope serverScope = requestBroker.getServerScope(); StoragePluginManager storagePluginManager = serverScope.getStoragePluginManager(); StorageEventHandler mapEventHandler = storagePluginManager.getEventHandler(uniqueInstanceName); if (mapEventHandler != null) { storagePluginManager.setEventHandler(uniqueInstanceName, null); mapEventHandler.unRegisterEventHelpers(this); } QueuePluginManager queuePluginManager = serverScope.getQueuePluginManager(); StorageEventHandler queueEventHandler = queuePluginManager.getEventHandler(uniqueInstanceName); if (queueEventHandler != null) { queueEventHandler.unRegisterEventHelpers(this); queuePluginManager.setEventHandler(uniqueInstanceName, null); } if (this.loggingSet != null) this.loggingSet = null; XbNotifyHandler hh = XbNotifyHandler.instance(); hh.unregister(Level.WARNING.intValue(), this); hh.unregister(Level.SEVERE.intValue(), this); if (this.engineGlob != null && this.mbeanHandle != null) this.engineGlob.unregisterMBean(this.mbeanHandle); if (this.runlevelSet != null) this.runlevelSet = null; if (this.engineGlob != null) { this.engineGlob.getRunlevelManager().removeRunlevelListener(this); } if (this.clientSet != null) this.clientSet = null; if (this.requestBroker != null) this.requestBroker.getAuthenticate().removeClientListener(this); this.requestBroker.removeRemotePropertiesListener(this); /* if (this.subscribeSet != null) this.subscribeSet = null; if (this.unSubscribeSet != null) this.unSubscribeSet = null; */ this.requestBroker.removeSubscriptionListener(this); if (this.topicSet != null) this.topicSet = null; this.engineGlob.getTopicAccessor().removeTopicListener(this); this.isShutdown = true; } /** * TODO: Put into engine.Global and util.Global (see EmailExecutor.java) * * @return * @throws XmlBlasterException */ public SmtpClient getSmtpClient() throws XmlBlasterException { if (this.smtpClient == null) { this.smtpClient = (SmtpClient) this.engineGlob .getObjectEntry(SmtpClient.OBJECTENTRY_KEY); if (this.smtpClient == null) { String text = "Please register SmtpClient in xmlBlasterPlugins.xml to have 'email' support"; throw new XmlBlasterException(this.engineGlob, ErrorCode.COMMUNICATION_NOCONNECTION, ME, text); } } return this.smtpClient; } /** * Redirect logging. * * @see org.xmlBlaster.util.log.I_LogListener#log(LogRecord) */ public void log(LogRecord record) { // We may not do any log.xxx() call here because of recursion!! //if (Logger.LOG_WARN != level && LogChannel.LOG_ERROR != level) // return; if (record == null) return; if (this.loggingSet == null) return; Level level = record.getLevel(); String source = record.getSourceClassName()+"."+record.getSourceMethodName(); String str = record.getMessage(); String found = (Level.WARNING.equals(level)) ? "logging/warning/" : "logging/severe/"; String foundEvent = found + "*"; // "logging/warning/*" if (!this.loggingSet.contains(foundEvent)) return; // How to extract the Logger name like "core"? try { if (source == null) source = ""; String description = (str == null) ? "" : str; String summary = "[" + new java.sql.Timestamp(record.getMillis()).toString() + " " + level.toString() + " " + Thread.currentThread().getName() + " " + source + "]"; String eventType = foundEvent; // extract errorCode e.g. "... errorCode=communication.noConnection bla bla..." String errorCode=null; try { int index = str.lastIndexOf("errorCode="); if (index != -1) { errorCode = str.substring(index+10); int end = errorCode.indexOf(" "); if (end != -1) errorCode = errorCode.substring(0,end);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -