001: /**
002: * $RCSfile: $
003: * $Revision: $
004: * $Date: $
005: *
006: * Copyright (C) 2007 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.openfire.plugin;
011:
012: import org.apache.mina.transport.socket.nio.SocketAcceptor;
013: import org.jivesoftware.openfire.XMPPServer;
014: import org.jivesoftware.openfire.container.Plugin;
015: import org.jivesoftware.openfire.container.PluginManager;
016: import org.jivesoftware.openfire.interceptor.InterceptorManager;
017: import org.jivesoftware.openfire.spi.ConnectionManagerImpl;
018: import org.jivesoftware.util.JiveGlobals;
019: import org.jivesoftware.util.PropertyEventDispatcher;
020: import org.jivesoftware.util.PropertyEventListener;
021:
022: import java.io.File;
023: import java.util.Map;
024:
025: /**
026: * Debugger plugin that prints XML traffic to stdout. By default it will only print
027: * raw XML traffic (by using a MINA filter). To turn on printing of interpreted XML
028: * (i.e. parsed XML) just enable the system property <tt>plugin.debugger.interpretedAllowed</tt>.
029: * There is no need to restart the plugin or the server.
030: *
031: * @author Gaston Dombiak
032: */
033: public class DebuggerPlugin implements Plugin, PropertyEventListener {
034: private RawPrintFilter defaultPortFilter;
035: private RawPrintFilter oldPortFilter;
036:
037: private InterpretedXMLPrinter interpretedPrinter;
038:
039: public void initializePlugin(PluginManager manager,
040: File pluginDirectory) {
041: // Add filter to filter chain builder
042: ConnectionManagerImpl connManager = (ConnectionManagerImpl) XMPPServer
043: .getInstance().getConnectionManager();
044: defaultPortFilter = new RawPrintFilter();
045: SocketAcceptor socketAcceptor = connManager.getSocketAcceptor();
046: if (socketAcceptor != null) {
047: socketAcceptor.getFilterChain().addBefore("xmpp",
048: "rawDebugger", defaultPortFilter);
049: }
050: oldPortFilter = new RawPrintFilter();
051: SocketAcceptor sslAcceptor = connManager.getSSLSocketAcceptor();
052: if (sslAcceptor != null) {
053: sslAcceptor.getFilterChain().addBefore("xmpp",
054: "rawDebugger", oldPortFilter);
055: }
056:
057: interpretedPrinter = new InterpretedXMLPrinter();
058: if (JiveGlobals
059: .getBooleanProperty("plugin.debugger.interpretedAllowed")) {
060: // Add the packet interceptor that prints interpreted XML
061: InterceptorManager.getInstance().addInterceptor(
062: interpretedPrinter);
063: }
064: // Listen to property events
065: PropertyEventDispatcher.addListener(this );
066: }
067:
068: public void destroyPlugin() {
069: // Stop listening to property events
070: PropertyEventDispatcher.removeListener(this );
071: // Remove filter from filter chain builder
072: ConnectionManagerImpl connManager = (ConnectionManagerImpl) XMPPServer
073: .getInstance().getConnectionManager();
074: if (connManager.getSocketAcceptor() != null
075: && connManager.getSocketAcceptor().getFilterChain()
076: .contains("rawDebugger")) {
077: connManager.getSocketAcceptor().getFilterChain().remove(
078: "rawDebugger");
079: }
080: if (connManager.getSSLSocketAcceptor() != null
081: && connManager.getSSLSocketAcceptor().getFilterChain()
082: .contains("rawDebugger")) {
083: connManager.getSSLSocketAcceptor().getFilterChain().remove(
084: "rawDebugger");
085: }
086: // Remove the filters from existing sessions
087: if (defaultPortFilter != null) {
088: defaultPortFilter.shutdown();
089: }
090: if (oldPortFilter != null) {
091: oldPortFilter.shutdown();
092: }
093:
094: // Remove the packet interceptor that prints interpreted XML
095: InterceptorManager.getInstance().removeInterceptor(
096: interpretedPrinter);
097:
098: defaultPortFilter = null;
099: oldPortFilter = null;
100: interpretedPrinter = null;
101: }
102:
103: public void propertySet(String property, Map<String, Object> params) {
104: if (property.equals("plugin.debugger.interpretedAllowed")) {
105: if (Boolean.parseBoolean((String) params.get("value"))) {
106: InterceptorManager.getInstance().addInterceptor(
107: interpretedPrinter);
108: } else {
109: InterceptorManager.getInstance().removeInterceptor(
110: interpretedPrinter);
111: }
112: }
113: }
114:
115: public void propertyDeleted(String property,
116: Map<String, Object> params) {
117: if (property.equals("plugin.debugger.interpretedAllowed")) {
118: InterceptorManager.getInstance().removeInterceptor(
119: interpretedPrinter);
120: }
121: }
122:
123: public void xmlPropertySet(String property,
124: Map<String, Object> params) {
125: // Do nothing
126: }
127:
128: public void xmlPropertyDeleted(String property,
129: Map<String, Object> params) {
130: // Do nothing
131: }
132: }
|