diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/BrokerPluginSupport.java b/activemq-core/src/main/java/org/apache/activemq/broker/BrokerPluginSupport.java new file mode 100644 index 0000000000..5591d5e308 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/broker/BrokerPluginSupport.java @@ -0,0 +1,35 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker; + +/** + * A useful base class for implementing broker plugins. + * + * @version $Revision$ + */ +public abstract class BrokerPluginSupport extends MutableBrokerFilter implements BrokerPlugin { + + public BrokerPluginSupport() { + super(null); + } + + public Broker installPlugin(Broker broker) throws Exception { + setNext(broker); + return this; + } + +} diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/util/LoggingBrokerPlugin.java b/activemq-core/src/main/java/org/apache/activemq/broker/util/LoggingBrokerPlugin.java new file mode 100644 index 0000000000..6a5372b78c --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/broker/util/LoggingBrokerPlugin.java @@ -0,0 +1,95 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util; + +import org.apache.activemq.broker.BrokerPluginSupport; +import org.apache.activemq.broker.ConnectionContext; +import org.apache.activemq.command.Message; +import org.apache.activemq.command.MessageAck; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +/** + * A simple Broker interceptor which allows you to enable/disable logging. + * + * @org.apache.xbean.XBean + * + * @version $Revision$ + */ +public class LoggingBrokerPlugin extends BrokerPluginSupport { + + private Log log = LogFactory.getLog(LoggingBrokerPlugin.class); + private Log sendLog; + private Log ackLog; + + public void send(ConnectionContext context, Message messageSend) throws Exception { + if (sendLog == null) { + sendLog = createLog("Send"); + } + if (sendLog.isInfoEnabled()) { + sendLog.info("Sending: " + messageSend); + } + super.send(context, messageSend); + } + + public void acknowledge(ConnectionContext context, MessageAck ack) throws Exception { + if (ackLog == null) { + ackLog = createLog("Ack"); + } + if (ackLog.isInfoEnabled()) { + ackLog.info("Acknowledge: " + ack); + } + super.acknowledge(context, ack); + } + + // Properties + // ------------------------------------------------------------------------- + public Log getAckLog() { + return ackLog; + } + + public void setAckLog(Log ackLog) { + this.ackLog = ackLog; + } + + public Log getLog() { + return log; + } + + public void setLog(Log log) { + this.log = log; + } + + public Log getSendLog() { + return sendLog; + } + + public void setSendLog(Log sendLog) { + this.sendLog = sendLog; + } + + // Implementation methods + // ------------------------------------------------------------------------- + + /** + * Lazily creates a new child log + */ + protected Log createLog(String name) { + return LogFactory.getLog(log.toString() + "." + name); + } + +} diff --git a/activemq-core/src/main/java/org/apache/activemq/broker/util/package.html b/activemq-core/src/main/java/org/apache/activemq/broker/util/package.html new file mode 100755 index 0000000000..bbd79c54c5 --- /dev/null +++ b/activemq-core/src/main/java/org/apache/activemq/broker/util/package.html @@ -0,0 +1,9 @@ + +
+ + + +Some utility Broker Plugins + + + diff --git a/activemq-core/src/test/java/org/apache/activemq/broker/util/LoggingBrokerTest.java b/activemq-core/src/test/java/org/apache/activemq/broker/util/LoggingBrokerTest.java new file mode 100644 index 0000000000..e1a7760fb6 --- /dev/null +++ b/activemq-core/src/test/java/org/apache/activemq/broker/util/LoggingBrokerTest.java @@ -0,0 +1,55 @@ +/** + * + * Copyright 2005-2006 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.activemq.broker.util; + +import org.apache.activemq.broker.BrokerFactory; +import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.test.JmsTopicSendReceiveTest; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.net.URI; + +/** + * + * @version $Revision$ + */ +public class LoggingBrokerTest extends JmsTopicSendReceiveTest { + private static final Log log = LogFactory.getLog(LoggingBrokerTest.class); + private BrokerService broker; + + protected void setUp() throws Exception { + broker = createBroker(); + super.setUp(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + if (broker != null) { + broker.stop(); + } + } + + protected BrokerService createBroker() throws Exception { + return createBroker("org/apache/activemq/util/logging-broker.xml"); + } + + protected BrokerService createBroker(String uri) throws Exception { + log.info("Loading broker configuration from the classpath with URI: " + uri); + return BrokerFactory.createBroker(new URI("xbean:" + uri)); + } +} diff --git a/activemq-core/src/test/resources/org/apache/activemq/util/logging-broker.xml b/activemq-core/src/test/resources/org/apache/activemq/util/logging-broker.xml new file mode 100644 index 0000000000..22ba5c2ca9 --- /dev/null +++ b/activemq-core/src/test/resources/org/apache/activemq/util/logging-broker.xml @@ -0,0 +1,34 @@ + + + + + +