You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/09/06 06:54:44 UTC
[1/3] Remove mom package from hierarchy.
Repository: logging-log4j2
Updated Branches:
refs/heads/messaging-module 7aec0b5ab -> f4721c24e
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
new file mode 100644
index 0000000..55268cc
--- /dev/null
+++ b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
@@ -0,0 +1,122 @@
+package org.apache.logging.log4j.jms.appender;
+
+import javax.jms.Message;
+import javax.jms.ObjectMessage;
+import javax.jms.TextMessage;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.util.JndiCloser;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.mockejb.jms.MockQueue;
+import org.mockejb.jms.MockTopic;
+import org.mockejb.jms.QueueConnectionFactoryImpl;
+import org.mockejb.jms.TopicConnectionFactoryImpl;
+import org.mockejb.jndi.MockContextFactory;
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.junit.Assert.*;
+
+public class JmsAppenderTest {
+
+ private static final String CONNECTION_FACTORY_NAME = "jms/queues";
+ private static final String TOPIC_FACTORY_NAME = "jms/topics";
+ private static final String DESTINATION_NAME = "jms/destination";
+ private static final String QUEUE_NAME = "jms/queue";
+ private static final String TOPIC_NAME = "jms/topic";
+ private static final String LOG_MESSAGE = "Hello, world!";
+
+ private static Context context;
+
+ private static MockQueue destination;
+ private static MockQueue queue;
+ private static MockTopic topic;
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ MockContextFactory.setAsInitial();
+ context = new InitialContext();
+ context.rebind(CONNECTION_FACTORY_NAME, new QueueConnectionFactoryImpl());
+ context.rebind(TOPIC_FACTORY_NAME, new TopicConnectionFactoryImpl());
+ destination = new MockQueue(DESTINATION_NAME);
+ context.rebind(DESTINATION_NAME, destination);
+ queue = new MockQueue(QUEUE_NAME);
+ context.rebind(QUEUE_NAME, queue);
+ topic = new MockTopic(TOPIC_NAME);
+ context.rebind(TOPIC_NAME, topic);
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ JndiCloser.close(context);
+ }
+
+ @Rule
+ public InitialLoggerContext ctx = new InitialLoggerContext("JmsAppenderTest.xml");
+
+ @Test
+ public void testAppendToQueue() throws Exception {
+ assertEquals(0, destination.size());
+ final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsAppender");
+ final LogEvent event = createLogEvent();
+ appender.append(event);
+ assertEquals(1, destination.size());
+ final Message message = destination.getMessageAt(0);
+ assertNotNull(message);
+ assertThat(message, instanceOf(TextMessage.class));
+ final TextMessage textMessage = (TextMessage) message;
+ assertEquals(LOG_MESSAGE, textMessage.getText());
+ }
+
+ @Test
+ public void testJmsQueueAppenderCompatibility() throws Exception {
+ assertEquals(0, queue.size());
+ final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQueueAppender");
+ final LogEvent expected = createLogEvent();
+ appender.append(expected);
+ assertEquals(1, queue.size());
+ final Message message = queue.getMessageAt(0);
+ assertNotNull(message);
+ assertThat(message, instanceOf(ObjectMessage.class));
+ final ObjectMessage objectMessage = (ObjectMessage) message;
+ final Object o = objectMessage.getObject();
+ assertThat(o, instanceOf(LogEvent.class));
+ final LogEvent actual = (LogEvent) o;
+ assertEquals(expected.getMessage().getFormattedMessage(), actual.getMessage().getFormattedMessage());
+ }
+
+ @Test
+ public void testJmsTopicAppenderCompatibility() throws Exception {
+ assertEquals(0, topic.size());
+ final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsTopicAppender");
+ final LogEvent expected = createLogEvent();
+ appender.append(expected);
+ assertEquals(1, topic.size());
+ final Message message = topic.getMessageAt(0);
+ assertNotNull(message);
+ assertThat(message, instanceOf(ObjectMessage.class));
+ final ObjectMessage objectMessage = (ObjectMessage) message;
+ final Object o = objectMessage.getObject();
+ assertThat(o, instanceOf(LogEvent.class));
+ final LogEvent actual = (LogEvent) o;
+ assertEquals(expected.getMessage().getFormattedMessage(), actual.getMessage().getFormattedMessage());
+ }
+
+ private static Log4jLogEvent createLogEvent() {
+ return Log4jLogEvent.newBuilder()
+ .setLoggerName(JmsAppenderTest.class.getName())
+ .setLoggerFqcn(JmsAppenderTest.class.getName())
+ .setLevel(Level.INFO)
+ .setMessage(new SimpleMessage(LOG_MESSAGE))
+ .build();
+ }
+
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java
deleted file mode 100644
index 9ed98fa..0000000
--- a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderIT.java
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.appender;
-
-import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Properties;
-import java.util.concurrent.CountDownLatch;
-import java.util.concurrent.TimeUnit;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageListener;
-import javax.jms.ObjectMessage;
-
-import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
-import org.apache.logging.log4j.core.layout.SerializedLayout;
-import org.apache.logging.log4j.message.SimpleMessage;
-import org.apache.logging.log4j.mom.jms.manager.JmsManager;
-import org.apache.logging.log4j.mom.jms.manager.JndiManager;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
-
-import static org.junit.Assert.*;
-
-/**
- * Integration test for JmsAppender using an embedded ActiveMQ broker.
- */
-public class JmsAppenderIT {
-
- private static JmsManager jmsManager;
-
- private JmsAppender appender;
-
- @BeforeClass
- public static void setUpClass() {
- final Properties additional = new Properties();
- additional.setProperty("queue.TestQueue", "TestQueue");
- JndiManager jndiManager = JndiManager.getJndiManager(ActiveMQInitialContextFactory.class.getName(),
- "vm://localhost?broker.persistent=false", null, null, null, additional);
- jmsManager = JmsManager.getJmsManager("JmsManager", jndiManager, "ConnectionFactory", "TestQueue", null, null);
- }
-
- @AfterClass
- public static void tearDownClass() {
- jmsManager.release();
- }
-
- @Before
- public void setUp() throws Exception {
- appender = new JmsAppender("JmsAppender", null, SerializedLayout.createLayout(), true, jmsManager);
- appender.start();
- }
-
- @Test
- public void testLogToQueue() throws Exception {
- final int messageCount = 100;
- final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
- final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount);
- messageConsumer.setMessageListener(consumer);
- final String messageText = "Hello, World!";
- final String loggerName = this.getClass().getName();
- for (int i = 0; i < messageCount; i++) {
- final LogEvent event = Log4jLogEvent.createEvent(loggerName, null, loggerName, Level.ERROR,
- new SimpleMessage(messageText), null, null, null, null, Thread.currentThread().getName(), null,
- System.currentTimeMillis());
- appender.append(event);
- }
- consumer.awaitAndAssertAllMessagesConsumed();
- }
-
- private static class JmsQueueConsumer implements MessageListener {
-
- private final int messageCount;
- private final CountDownLatch countDownLatch;
- private final Collection<LogEvent> events;
-
- private JmsQueueConsumer(final int messageCount) {
- this.messageCount = messageCount;
- this.countDownLatch = new CountDownLatch(messageCount);
- this.events = new ArrayList<LogEvent>(messageCount);
- }
-
- @Override
- public void onMessage(Message message) {
- try {
- consume((ObjectMessage) message);
- } catch (JMSException e) {
- e.printStackTrace();
- }
- }
-
- private void consume(ObjectMessage message) throws JMSException {
- try {
- final LogEvent event = (LogEvent) message.getObject();
- events.add(event);
- } finally {
- countDownLatch.countDown();
- }
- }
-
- public void awaitAndAssertAllMessagesConsumed() throws InterruptedException {
- countDownLatch.await(5, TimeUnit.SECONDS);
- assertEquals(messageCount, events.size());
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java
deleted file mode 100644
index 743a4eb..0000000
--- a/log4j-jms/src/test/java/org/apache/logging/log4j/mom/jms/appender/JmsAppenderTest.java
+++ /dev/null
@@ -1,122 +0,0 @@
-package org.apache.logging.log4j.mom.jms.appender;
-
-import javax.jms.Message;
-import javax.jms.ObjectMessage;
-import javax.jms.TextMessage;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.impl.Log4jLogEvent;
-import org.apache.logging.log4j.core.util.JndiCloser;
-import org.apache.logging.log4j.junit.InitialLoggerContext;
-import org.apache.logging.log4j.message.SimpleMessage;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
-import org.junit.Rule;
-import org.junit.Test;
-import org.mockejb.jms.MockQueue;
-import org.mockejb.jms.MockTopic;
-import org.mockejb.jms.QueueConnectionFactoryImpl;
-import org.mockejb.jms.TopicConnectionFactoryImpl;
-import org.mockejb.jndi.MockContextFactory;
-
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.junit.Assert.*;
-
-public class JmsAppenderTest {
-
- private static final String CONNECTION_FACTORY_NAME = "jms/queues";
- private static final String TOPIC_FACTORY_NAME = "jms/topics";
- private static final String DESTINATION_NAME = "jms/destination";
- private static final String QUEUE_NAME = "jms/queue";
- private static final String TOPIC_NAME = "jms/topic";
- private static final String LOG_MESSAGE = "Hello, world!";
-
- private static Context context;
-
- private static MockQueue destination;
- private static MockQueue queue;
- private static MockTopic topic;
-
- @BeforeClass
- public static void setUpClass() throws Exception {
- MockContextFactory.setAsInitial();
- context = new InitialContext();
- context.rebind(CONNECTION_FACTORY_NAME, new QueueConnectionFactoryImpl());
- context.rebind(TOPIC_FACTORY_NAME, new TopicConnectionFactoryImpl());
- destination = new MockQueue(DESTINATION_NAME);
- context.rebind(DESTINATION_NAME, destination);
- queue = new MockQueue(QUEUE_NAME);
- context.rebind(QUEUE_NAME, queue);
- topic = new MockTopic(TOPIC_NAME);
- context.rebind(TOPIC_NAME, topic);
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- JndiCloser.close(context);
- }
-
- @Rule
- public InitialLoggerContext ctx = new InitialLoggerContext("JmsAppenderTest.xml");
-
- @Test
- public void testAppendToQueue() throws Exception {
- assertEquals(0, destination.size());
- final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsAppender");
- final LogEvent event = createLogEvent();
- appender.append(event);
- assertEquals(1, destination.size());
- final Message message = destination.getMessageAt(0);
- assertNotNull(message);
- assertThat(message, instanceOf(TextMessage.class));
- final TextMessage textMessage = (TextMessage) message;
- assertEquals(LOG_MESSAGE, textMessage.getText());
- }
-
- @Test
- public void testJmsQueueAppenderCompatibility() throws Exception {
- assertEquals(0, queue.size());
- final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsQueueAppender");
- final LogEvent expected = createLogEvent();
- appender.append(expected);
- assertEquals(1, queue.size());
- final Message message = queue.getMessageAt(0);
- assertNotNull(message);
- assertThat(message, instanceOf(ObjectMessage.class));
- final ObjectMessage objectMessage = (ObjectMessage) message;
- final Object o = objectMessage.getObject();
- assertThat(o, instanceOf(LogEvent.class));
- final LogEvent actual = (LogEvent) o;
- assertEquals(expected.getMessage().getFormattedMessage(), actual.getMessage().getFormattedMessage());
- }
-
- @Test
- public void testJmsTopicAppenderCompatibility() throws Exception {
- assertEquals(0, topic.size());
- final JmsAppender appender = (JmsAppender) ctx.getRequiredAppender("JmsTopicAppender");
- final LogEvent expected = createLogEvent();
- appender.append(expected);
- assertEquals(1, topic.size());
- final Message message = topic.getMessageAt(0);
- assertNotNull(message);
- assertThat(message, instanceOf(ObjectMessage.class));
- final ObjectMessage objectMessage = (ObjectMessage) message;
- final Object o = objectMessage.getObject();
- assertThat(o, instanceOf(LogEvent.class));
- final LogEvent actual = (LogEvent) o;
- assertEquals(expected.getMessage().getFormattedMessage(), actual.getMessage().getFormattedMessage());
- }
-
- private static Log4jLogEvent createLogEvent() {
- return Log4jLogEvent.newBuilder()
- .setLoggerName(JmsAppenderTest.class.getName())
- .setLoggerFqcn(JmsAppenderTest.class.getName())
- .setLevel(Level.INFO)
- .setMessage(new SimpleMessage(LOG_MESSAGE))
- .build();
- }
-
-}
\ No newline at end of file
[3/3] git commit: Add copyright header.
Posted by ma...@apache.org.
Add copyright header.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/f4721c24
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/f4721c24
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/f4721c24
Branch: refs/heads/messaging-module
Commit: f4721c24e2703c27262349949a8daaa8bd5061fd
Parents: 7ddb07b
Author: Matt Sicker <ma...@apache.org>
Authored: Fri Sep 5 23:54:27 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Fri Sep 5 23:54:27 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/jms/appender/JmsAppenderTest.java | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/f4721c24/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
index 55268cc..1babed2 100644
--- a/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
+++ b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderTest.java
@@ -1,3 +1,19 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.appender;
import javax.jms.Message;
[2/3] git commit: Remove mom package from hierarchy.
Posted by ma...@apache.org.
Remove mom package from hierarchy.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/7ddb07bd
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/7ddb07bd
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/7ddb07bd
Branch: refs/heads/messaging-module
Commit: 7ddb07bd0494d2c045bb98ba922d1b2e2cf56f9f
Parents: 7aec0b5
Author: Matt Sicker <ma...@apache.org>
Authored: Fri Sep 5 23:53:53 2014 -0500
Committer: Matt Sicker <ma...@apache.org>
Committed: Fri Sep 5 23:53:53 2014 -0500
----------------------------------------------------------------------
.../logging/log4j/jms/appender/JmsAppender.java | 198 +++++++++++++++++++
.../log4j/jms/appender/package-info.java | 20 ++
.../logging/log4j/jms/manager/JmsManager.java | 169 ++++++++++++++++
.../logging/log4j/jms/manager/JndiManager.java | 125 ++++++++++++
.../log4j/jms/receiver/AbstractJmsReceiver.java | 82 ++++++++
.../log4j/jms/receiver/JmsQueueReceiver.java | 106 ++++++++++
.../log4j/jms/receiver/JmsTopicReceiver.java | 104 ++++++++++
.../log4j/jms/receiver/package-info.java | 25 +++
.../log4j/mom/jms/appender/JmsAppender.java | 198 -------------------
.../log4j/mom/jms/appender/package-info.java | 20 --
.../log4j/mom/jms/manager/JmsManager.java | 169 ----------------
.../log4j/mom/jms/manager/JndiManager.java | 125 ------------
.../mom/jms/receiver/AbstractJmsReceiver.java | 83 --------
.../mom/jms/receiver/JmsQueueReceiver.java | 107 ----------
.../mom/jms/receiver/JmsTopicReceiver.java | 105 ----------
.../log4j/mom/jms/receiver/package-info.java | 25 ---
.../log4j/jms/appender/JmsAppenderIT.java | 126 ++++++++++++
.../log4j/jms/appender/JmsAppenderTest.java | 122 ++++++++++++
.../log4j/mom/jms/appender/JmsAppenderIT.java | 126 ------------
.../log4j/mom/jms/appender/JmsAppenderTest.java | 122 ------------
20 files changed, 1077 insertions(+), 1080 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/JmsAppender.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/JmsAppender.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/JmsAppender.java
new file mode 100644
index 0000000..d90f203
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/JmsAppender.java
@@ -0,0 +1,198 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.appender;
+
+import java.io.Serializable;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageProducer;
+
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.Layout;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.appender.AbstractAppender;
+import org.apache.logging.log4j.core.appender.AppenderLoggingException;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginAliases;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+import org.apache.logging.log4j.core.config.plugins.PluginElement;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.jms.manager.JmsManager;
+import org.apache.logging.log4j.jms.manager.JndiManager;
+
+/**
+ * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However,
+ * configurations set up for the 2.0 version of the JMS appenders will still work.
+ */
+@Plugin(name = "JMS", category = "Core", elementType = "appender", printObject = true)
+@PluginAliases({"JMSQueue", "JMSTopic"})
+public class JmsAppender extends AbstractAppender {
+
+ private final JmsManager manager;
+ private final MessageProducer producer;
+
+ protected JmsAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
+ final boolean ignoreExceptions, final JmsManager manager)
+ throws JMSException {
+ super(name, filter, layout, ignoreExceptions);
+ this.manager = manager;
+ this.producer = this.manager.createMessageProducer();
+ }
+
+ @Override
+ public void append(final LogEvent event) {
+ try {
+ final Message message = this.manager.createMessage(getLayout().toSerializable(event));
+ message.setJMSTimestamp(event.getTimeMillis());
+ this.producer.send(message);
+ } catch (JMSException e) {
+ throw new AppenderLoggingException(e);
+ }
+ }
+
+ @PluginBuilderFactory
+ public static Builder newBuilder() {
+ return new Builder();
+ }
+
+ public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> {
+
+ @PluginBuilderAttribute
+ private String name;
+
+ @PluginBuilderAttribute
+ private String factoryName;
+
+ @PluginBuilderAttribute
+ private String providerUrl;
+
+ @PluginBuilderAttribute
+ private String urlPkgPrefixes;
+
+ @PluginBuilderAttribute
+ private String securityPrincipalName;
+
+ @PluginBuilderAttribute(sensitive = true)
+ private String securityCredentials;
+
+ @PluginBuilderAttribute
+ private String factoryBindingName;
+
+ @PluginBuilderAttribute
+ @PluginAliases({"queueBindingName", "topicBindingName"})
+ private String destinationBindingName;
+
+ @PluginBuilderAttribute
+ private String username;
+
+ @PluginBuilderAttribute(sensitive = true)
+ private String password;
+
+ @PluginElement("Layout")
+ private Layout<? extends Serializable> layout = SerializedLayout.createLayout();
+
+ @PluginElement("Filter")
+ private Filter filter;
+
+ @PluginBuilderAttribute
+ private boolean ignoreExceptions = true;
+
+ private Builder() {
+ }
+
+ public Builder setName(final String name) {
+ this.name = name;
+ return this;
+ }
+
+ public Builder setFactoryName(final String factoryName) {
+ this.factoryName = factoryName;
+ return this;
+ }
+
+ public Builder setProviderUrl(final String providerUrl) {
+ this.providerUrl = providerUrl;
+ return this;
+ }
+
+ public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) {
+ this.urlPkgPrefixes = urlPkgPrefixes;
+ return this;
+ }
+
+ public Builder setSecurityPrincipalName(final String securityPrincipalName) {
+ this.securityPrincipalName = securityPrincipalName;
+ return this;
+ }
+
+ public Builder setSecurityCredentials(final String securityCredentials) {
+ this.securityCredentials = securityCredentials;
+ return this;
+ }
+
+ public Builder setFactoryBindingName(final String factoryBindingName) {
+ this.factoryBindingName = factoryBindingName;
+ return this;
+ }
+
+ public Builder setDestinationBindingName(final String destinationBindingName) {
+ this.destinationBindingName = destinationBindingName;
+ return this;
+ }
+
+ public Builder setUsername(final String username) {
+ this.username = username;
+ return this;
+ }
+
+ public Builder setPassword(final String password) {
+ this.password = password;
+ return this;
+ }
+
+ public Builder setLayout(final Layout<? extends Serializable> layout) {
+ this.layout = layout;
+ return this;
+ }
+
+ public Builder setFilter(final Filter filter) {
+ this.filter = filter;
+ return this;
+ }
+
+ public Builder setIgnoreExceptions(final boolean ignoreExceptions) {
+ this.ignoreExceptions = ignoreExceptions;
+ return this;
+ }
+
+ @Override
+ public JmsAppender build() {
+ final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes,
+ securityPrincipalName, securityCredentials, null);
+ final JmsManager jmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName,
+ destinationBindingName, username, password);
+ try {
+ return new JmsAppender(name, filter, layout, ignoreExceptions, jmsManager);
+ } catch (final JMSException e) {
+ LOGGER.error("Error creating JmsAppender [{}].", name, e);
+ return null;
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/package-info.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/package-info.java
new file mode 100644
index 0000000..26aec37
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/appender/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+/**
+ * Appender classes for using JMS. These are directly configured through your log4j2 configuration file.
+ */
+package org.apache.logging.log4j.jms.appender;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JmsManager.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JmsManager.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JmsManager.java
new file mode 100644
index 0000000..f6921d2
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JmsManager.java
@@ -0,0 +1,169 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.manager;
+
+import java.io.Serializable;
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.naming.NamingException;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects
+ * involving a configured ConnectionFactory and Destination.
+ */
+public class JmsManager extends AbstractManager {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+
+ private static final JmsManagerFactory FACTORY = new JmsManagerFactory();
+
+ private final JndiManager jndiManager;
+ private final Connection connection;
+ private final Session session;
+ private final Destination destination;
+
+ private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName,
+ final String destinationName, final String username, final String password)
+ throws NamingException, JMSException {
+ super(name);
+ this.jndiManager = jndiManager;
+ final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName);
+ if (username != null && password != null) {
+ this.connection = connectionFactory.createConnection(username, password);
+ } else {
+ this.connection = connectionFactory.createConnection();
+ }
+ this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
+ this.destination = this.jndiManager.lookup(destinationName);
+ this.connection.start();
+ }
+
+ /**
+ * Gets a JmsManager using the specified configuration parameters.
+ *
+ * @param name The name to use for this JmsManager.
+ * @param jndiManager The JndiManager to look up JMS information through.
+ * @param connectionFactoryName The binding name for the {@link javax.jms.ConnectionFactory}.
+ * @param destinationName The binding name for the {@link javax.jms.Destination}.
+ * @param username The username to connect with or {@code null} for no authentication.
+ * @param password The password to use with the given username or {@code null} for no authentication.
+ * @return The JmsManager as configured.
+ */
+ public static JmsManager getJmsManager(final String name, final JndiManager jndiManager,
+ final String connectionFactoryName, final String destinationName,
+ final String username, final String password) {
+ final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName,
+ username, password);
+ return FACTORY.createManager(name, configuration);
+ }
+
+ /**
+ * Creates a MessageConsumer on this Destination using the current Session.
+ *
+ * @return A MessageConsumer on this Destination.
+ * @throws JMSException
+ */
+ public MessageConsumer createMessageConsumer() throws JMSException {
+ return this.session.createConsumer(this.destination);
+ }
+
+ /**
+ * Creates a MessageProducer on this Destination using the current Session.
+ *
+ * @return A MessageProducer on this Destination.
+ * @throws JMSException
+ */
+ public MessageProducer createMessageProducer() throws JMSException {
+ return this.session.createProducer(this.destination);
+ }
+
+ /**
+ * Creates a TextMessage or ObjectMessage from a Serializable object. For instance, when using a text-based
+ * {@link org.apache.logging.log4j.core.Layout} such as {@link org.apache.logging.log4j.core.layout.PatternLayout},
+ * the {@link org.apache.logging.log4j.core.LogEvent} message will be serialized to a String. When using a
+ * layout such as {@link org.apache.logging.log4j.core.layout.SerializedLayout}, the LogEvent message will be
+ * serialized as a Java object.
+ *
+ * @param object The LogEvent or String message to wrap.
+ * @return A new JMS message containing the provided object.
+ * @throws JMSException
+ */
+ public Message createMessage(final Serializable object) throws JMSException {
+ if (object instanceof String) {
+ return this.session.createTextMessage((String) object);
+ } else {
+ return this.session.createObjectMessage(object);
+ }
+ }
+
+ @Override
+ protected void releaseSub() {
+ try {
+ this.session.close();
+ } catch (final JMSException ignored) {
+ }
+ try {
+ this.connection.close();
+ } catch (final JMSException ignored) {
+ }
+ this.jndiManager.release();
+ }
+
+ private static class JmsConfiguration {
+ private final JndiManager jndiManager;
+ private final String connectionFactoryName;
+ private final String destinationName;
+ private final String username;
+ private final String password;
+
+ private JmsConfiguration(JndiManager jndiManager, String connectionFactoryName, String destinationName,
+ String username, String password) {
+ this.jndiManager = jndiManager;
+ this.connectionFactoryName = connectionFactoryName;
+ this.destinationName = destinationName;
+ this.username = username;
+ this.password = password;
+ }
+ }
+
+ private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> {
+
+ @Override
+ public JmsManager createManager(String name, JmsConfiguration data) {
+ try {
+ return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName,
+ data.username, data.password);
+ } catch (final Exception e) {
+ LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].",
+ data.connectionFactoryName, data.destinationName, e);
+ return null;
+ }
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JndiManager.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JndiManager.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JndiManager.java
new file mode 100644
index 0000000..c680948
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/manager/JndiManager.java
@@ -0,0 +1,125 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.manager;
+
+import java.util.Properties;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.appender.AbstractManager;
+import org.apache.logging.log4j.core.appender.ManagerFactory;
+import org.apache.logging.log4j.core.util.JndiCloser;
+import org.apache.logging.log4j.status.StatusLogger;
+
+/**
+ * JNDI context manager. Mainly useful for finding JMS objects and configuring the InitialContext.
+ */
+public class JndiManager extends AbstractManager {
+
+ private static final Logger LOGGER = StatusLogger.getLogger();
+
+ private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
+
+ private final Context context;
+
+ private JndiManager(String name, Context context) {
+ super(name);
+ this.context = context;
+ }
+
+ /**
+ * Gets a JndiManager with the provided configuration information.
+ *
+ * @param initialContextFactoryName Fully qualified class name of an implementation of
+ * {@link javax.naming.spi.InitialContextFactory}.
+ * @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
+ * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
+ * class that will create a URL context factory
+ * @param securityPrincipal The name of the identity of the Principal.
+ * @param securityCredentials The security credentials of the Principal.
+ * @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
+ * @return the JndiManager for the provided parameters.
+ */
+ public static JndiManager getJndiManager(final String initialContextFactoryName,
+ final String providerURL,
+ final String urlPkgPrefixes,
+ final String securityPrincipal,
+ final String securityCredentials,
+ final Properties additionalProperties) {
+ final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
+ if (initialContextFactoryName == null) {
+ return getManager(name, FACTORY, null);
+ }
+ final Properties properties = new Properties();
+ properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
+ if (providerURL != null) {
+ properties.setProperty(Context.PROVIDER_URL, providerURL);
+ } else {
+ LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " +
+ "provider URL. This is likely to cause problems.", initialContextFactoryName);
+ }
+ if (urlPkgPrefixes != null) {
+ properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
+ }
+ if (securityPrincipal != null) {
+ properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
+ if (securityCredentials != null) {
+ properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
+ } else {
+ LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
+ securityPrincipal);
+ }
+ }
+ if (additionalProperties != null) {
+ properties.putAll(additionalProperties);
+ }
+ return getManager(name, FACTORY, properties);
+ }
+
+ @Override
+ protected void releaseSub() {
+ JndiCloser.closeSilently(this.context);
+ }
+
+ /**
+ * Looks up a named object through this JNDI context.
+ *
+ * @param name name of the object to look up.
+ * @param <T> the type of the object.
+ * @return the named object if it could be located.
+ * @throws NamingException
+ */
+ @SuppressWarnings("unchecked")
+ public <T> T lookup(final String name) throws NamingException {
+ return (T) this.context.lookup(name);
+ }
+
+ private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
+
+ @Override
+ public JndiManager createManager(String name, Properties data) {
+ try {
+ return new JndiManager(name, new InitialContext(data));
+ } catch (final NamingException e) {
+ LOGGER.error("Error creating JNDI InitialContext.", e);
+ return null;
+ }
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/AbstractJmsReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/AbstractJmsReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/AbstractJmsReceiver.java
new file mode 100644
index 0000000..4d21d83
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/AbstractJmsReceiver.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.receiver;
+
+import java.io.Serializable;
+import javax.jms.JMSException;
+import javax.jms.ObjectMessage;
+import javax.naming.Context;
+import javax.naming.NameNotFoundException;
+import javax.naming.NamingException;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.LogEventListener;
+
+/**
+ * Abstract base class for receiving LogEvents over JMS. This class expects all messages to be serialized log events.
+ */
+public abstract class AbstractJmsReceiver extends LogEventListener implements javax.jms.MessageListener {
+
+ /**
+ * Logger to capture diagnostics.
+ */
+ protected Logger logger = LogManager.getLogger(this.getClass().getName());
+
+ /**
+ * Listener that receives the event.
+ * @param message The received message.
+ */
+ @Override
+ public void onMessage(final javax.jms.Message message) {
+ try {
+ if (message instanceof ObjectMessage) {
+ final ObjectMessage objectMessage = (ObjectMessage) message;
+ final Serializable object = objectMessage.getObject();
+ if (object instanceof LogEvent) {
+ log((LogEvent) object);
+ } else {
+ logger.warn("Received message is of type " + object.getClass().getName() + ", was expecting LogEvent.");
+ }
+ } else {
+ logger.warn("Received message is of type " + message.getJMSType()
+ + ", was expecting ObjectMessage.");
+ }
+ } catch (final JMSException jmse) {
+ logger.error("Exception thrown while processing incoming message.",
+ jmse);
+ }
+ }
+
+ /**
+ * Looks up an object from the Context.
+ * @param ctx The Context.
+ * @param name The name of the object to locate.
+ * @return The object.
+ * @throws NamingException if an error occurs.
+ */
+ protected Object lookup(final Context ctx, final String name) throws NamingException {
+ try {
+ return ctx.lookup(name);
+ } catch (final NameNotFoundException e) {
+ logger.error("Could not find name [" + name + "].");
+ throw e;
+ }
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsQueueReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsQueueReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsQueueReceiver.java
new file mode 100644
index 0000000..0cd2efb
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsQueueReceiver.java
@@ -0,0 +1,106 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.receiver;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import javax.jms.JMSException;
+import javax.jms.Queue;
+import javax.jms.QueueConnection;
+import javax.jms.QueueConnectionFactory;
+import javax.jms.QueueReceiver;
+import javax.jms.QueueSession;
+import javax.jms.Session;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+/**
+ * Receives Log Events over a JMS Queue. This implementation expects that all messages will
+ * contain a serialized LogEvent.
+ */
+public class JmsQueueReceiver extends AbstractJmsReceiver {
+
+ /**
+ * Constructor.
+ * @param qcfBindingName The QueueConnectionFactory binding name.
+ * @param queueBindingName The Queue binding name.
+ * @param username The userid to connect to the queue.
+ * @param password The password to connect to the queue.
+ */
+ public JmsQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
+ final String password) {
+
+ try {
+ final Context ctx = new InitialContext();
+ QueueConnectionFactory queueConnectionFactory;
+ queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
+ final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
+ queueConnection.start();
+ final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
+ final Queue queue = (Queue) ctx.lookup(queueBindingName);
+ final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
+ queueReceiver.setMessageListener(this);
+ } catch (final JMSException e) {
+ logger.error("Could not read JMS message.", e);
+ } catch (final NamingException e) {
+ logger.error("Could not read JMS message.", e);
+ } catch (final RuntimeException e) {
+ logger.error("Could not read JMS message.", e);
+ }
+ }
+
+ /**
+ * Main startup for the receiver.
+ * @param args The command line arguments.
+ * @throws Exception if an error occurs.
+ */
+ public static void main(final String[] args) throws Exception {
+ if (args.length != 4) {
+ usage("Wrong number of arguments.");
+ }
+
+ final String qcfBindingName = args[0];
+ final String queueBindingName = args[1];
+ final String username = args[2];
+ final String password = args[3];
+
+ new JmsQueueReceiver(qcfBindingName, queueBindingName, username, password);
+
+ final Charset enc = Charset.defaultCharset();
+ final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
+ // Loop until the word "exit" is typed
+ System.out.println("Type \"exit\" to quit JmsQueueReceiver.");
+ while (true) {
+ final String line = stdin.readLine();
+ if (line == null || line.equalsIgnoreCase("exit")) {
+ System.out.println("Exiting. Kill the application if it does not exit "
+ + "due to daemon threads.");
+ return;
+ }
+ }
+ }
+
+
+ private static void usage(final String msg) {
+ System.err.println(msg);
+ System.err.println("Usage: java " + JmsQueueReceiver.class.getName()
+ + " QueueConnectionFactoryBindingName QueueBindingName username password");
+ System.exit(1);
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsTopicReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsTopicReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsTopicReceiver.java
new file mode 100644
index 0000000..31c0354
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/JmsTopicReceiver.java
@@ -0,0 +1,104 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.receiver;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.nio.charset.Charset;
+import javax.jms.JMSException;
+import javax.jms.Session;
+import javax.jms.Topic;
+import javax.jms.TopicConnection;
+import javax.jms.TopicConnectionFactory;
+import javax.jms.TopicSession;
+import javax.jms.TopicSubscriber;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+
+/**
+ * Receives Topic messages that contain LogEvents. This implementation expects that all messages
+ * are serialized log events.
+ */
+public class JmsTopicReceiver extends AbstractJmsReceiver {
+
+ /**
+ * Constructor.
+ * @param tcfBindingName The TopicConnectionFactory binding name.
+ * @param topicBindingName The Topic binding name.
+ * @param username The userid to connect to the topic.
+ * @param password The password to connect to the topic.
+ */
+ public JmsTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
+ final String password) {
+ try {
+ final Context ctx = new InitialContext();
+ TopicConnectionFactory topicConnectionFactory;
+ topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
+ final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
+ topicConnection.start();
+ final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
+ final Topic topic = (Topic) ctx.lookup(topicBindingName);
+ final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
+ topicSubscriber.setMessageListener(this);
+ } catch (final JMSException e) {
+ logger.error("Could not read JMS message.", e);
+ } catch (final NamingException e) {
+ logger.error("Could not read JMS message.", e);
+ } catch (final RuntimeException e) {
+ logger.error("Could not read JMS message.", e);
+ }
+ }
+
+ /**
+ * Main startup for the receiver.
+ * @param args The command line arguments.
+ * @throws Exception if an error occurs.
+ */
+ public static void main(final String[] args) throws Exception {
+ if (args.length != 4) {
+ usage("Wrong number of arguments.");
+ }
+
+ final String tcfBindingName = args[0];
+ final String topicBindingName = args[1];
+ final String username = args[2];
+ final String password = args[3];
+
+ new JmsTopicReceiver(tcfBindingName, topicBindingName, username, password);
+
+ final Charset enc = Charset.defaultCharset();
+ final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
+ // Loop until the word "exit" is typed
+ System.out.println("Type \"exit\" to quit JmsTopicReceiver.");
+ while (true) {
+ final String line = stdin.readLine();
+ if (line == null || line.equalsIgnoreCase("exit")) {
+ System.out.println("Exiting. Kill the application if it does not exit "
+ + "due to daemon threads.");
+ return;
+ }
+ }
+ }
+
+ private static void usage(final String msg) {
+ System.err.println(msg);
+ System.err.println("Usage: java " + JmsTopicReceiver.class.getName()
+ + " TopicConnectionFactoryBindingName TopicBindingName username password");
+ System.exit(1);
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/package-info.java b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/package-info.java
new file mode 100644
index 0000000..b53fd95
--- /dev/null
+++ b/log4j-jms/src/main/java/org/apache/logging/log4j/jms/receiver/package-info.java
@@ -0,0 +1,25 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+/**
+ * Supporting network code for JMS appenders.
+ * <p/>
+ * Note that you can use JmsQueueReceiver or JmsTopicReceiver as executable main classes to receive log events over
+ * JMS (sent via the appropriate JMS appender) that can be subsequently logged according to the configuration given to
+ * the running process. Of course, use of these classes as standalone executables are entirely optional and can
+ * be used directly in your application (e.g., through your Spring {@code beans.xml} configuration).
+ */
+package org.apache.logging.log4j.jms.receiver;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java
deleted file mode 100644
index f99e890..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/JmsAppender.java
+++ /dev/null
@@ -1,198 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.appender;
-
-import java.io.Serializable;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageProducer;
-
-import org.apache.logging.log4j.core.Filter;
-import org.apache.logging.log4j.core.Layout;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.appender.AbstractAppender;
-import org.apache.logging.log4j.core.appender.AppenderLoggingException;
-import org.apache.logging.log4j.core.config.plugins.Plugin;
-import org.apache.logging.log4j.core.config.plugins.PluginAliases;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
-import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
-import org.apache.logging.log4j.core.config.plugins.PluginElement;
-import org.apache.logging.log4j.core.layout.SerializedLayout;
-import org.apache.logging.log4j.mom.jms.manager.JmsManager;
-import org.apache.logging.log4j.mom.jms.manager.JndiManager;
-
-/**
- * Generic JMS Appender plugin for both queues and topics. This Appender replaces the previous split ones. However,
- * configurations set up for the 2.0 version of the JMS appenders will still work.
- */
-@Plugin(name = "JMS", category = "Core", elementType = "appender", printObject = true)
-@PluginAliases({"JMSQueue", "JMSTopic"})
-public class JmsAppender extends AbstractAppender {
-
- private final JmsManager manager;
- private final MessageProducer producer;
-
- protected JmsAppender(final String name, final Filter filter, final Layout<? extends Serializable> layout,
- final boolean ignoreExceptions, final JmsManager manager)
- throws JMSException {
- super(name, filter, layout, ignoreExceptions);
- this.manager = manager;
- this.producer = this.manager.createMessageProducer();
- }
-
- @Override
- public void append(final LogEvent event) {
- try {
- final Message message = this.manager.createMessage(getLayout().toSerializable(event));
- message.setJMSTimestamp(event.getTimeMillis());
- this.producer.send(message);
- } catch (JMSException e) {
- throw new AppenderLoggingException(e);
- }
- }
-
- @PluginBuilderFactory
- public static Builder newBuilder() {
- return new Builder();
- }
-
- public static class Builder implements org.apache.logging.log4j.core.util.Builder<JmsAppender> {
-
- @PluginBuilderAttribute
- private String name;
-
- @PluginBuilderAttribute
- private String factoryName;
-
- @PluginBuilderAttribute
- private String providerUrl;
-
- @PluginBuilderAttribute
- private String urlPkgPrefixes;
-
- @PluginBuilderAttribute
- private String securityPrincipalName;
-
- @PluginBuilderAttribute(sensitive = true)
- private String securityCredentials;
-
- @PluginBuilderAttribute
- private String factoryBindingName;
-
- @PluginBuilderAttribute
- @PluginAliases({"queueBindingName", "topicBindingName"})
- private String destinationBindingName;
-
- @PluginBuilderAttribute
- private String username;
-
- @PluginBuilderAttribute(sensitive = true)
- private String password;
-
- @PluginElement("Layout")
- private Layout<? extends Serializable> layout = SerializedLayout.createLayout();
-
- @PluginElement("Filter")
- private Filter filter;
-
- @PluginBuilderAttribute
- private boolean ignoreExceptions = true;
-
- private Builder() {
- }
-
- public Builder setName(final String name) {
- this.name = name;
- return this;
- }
-
- public Builder setFactoryName(final String factoryName) {
- this.factoryName = factoryName;
- return this;
- }
-
- public Builder setProviderUrl(final String providerUrl) {
- this.providerUrl = providerUrl;
- return this;
- }
-
- public Builder setUrlPkgPrefixes(final String urlPkgPrefixes) {
- this.urlPkgPrefixes = urlPkgPrefixes;
- return this;
- }
-
- public Builder setSecurityPrincipalName(final String securityPrincipalName) {
- this.securityPrincipalName = securityPrincipalName;
- return this;
- }
-
- public Builder setSecurityCredentials(final String securityCredentials) {
- this.securityCredentials = securityCredentials;
- return this;
- }
-
- public Builder setFactoryBindingName(final String factoryBindingName) {
- this.factoryBindingName = factoryBindingName;
- return this;
- }
-
- public Builder setDestinationBindingName(final String destinationBindingName) {
- this.destinationBindingName = destinationBindingName;
- return this;
- }
-
- public Builder setUsername(final String username) {
- this.username = username;
- return this;
- }
-
- public Builder setPassword(final String password) {
- this.password = password;
- return this;
- }
-
- public Builder setLayout(final Layout<? extends Serializable> layout) {
- this.layout = layout;
- return this;
- }
-
- public Builder setFilter(final Filter filter) {
- this.filter = filter;
- return this;
- }
-
- public Builder setIgnoreExceptions(final boolean ignoreExceptions) {
- this.ignoreExceptions = ignoreExceptions;
- return this;
- }
-
- @Override
- public JmsAppender build() {
- final JndiManager jndiManager = JndiManager.getJndiManager(factoryName, providerUrl, urlPkgPrefixes,
- securityPrincipalName, securityCredentials, null);
- final JmsManager jmsManager = JmsManager.getJmsManager(name, jndiManager, factoryBindingName,
- destinationBindingName, username, password);
- try {
- return new JmsAppender(name, filter, layout, ignoreExceptions, jmsManager);
- } catch (final JMSException e) {
- LOGGER.error("Error creating JmsAppender [{}].", name, e);
- return null;
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java
deleted file mode 100644
index 0c7a4ff..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/appender/package-info.java
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-/**
- * Appender classes for using JMS. These are directly configured through your log4j2 configuration file.
- */
-package org.apache.logging.log4j.mom.jms.appender;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java
deleted file mode 100644
index d3ce550..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JmsManager.java
+++ /dev/null
@@ -1,169 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.manager;
-
-import java.io.Serializable;
-import javax.jms.Connection;
-import javax.jms.ConnectionFactory;
-import javax.jms.Destination;
-import javax.jms.JMSException;
-import javax.jms.Message;
-import javax.jms.MessageConsumer;
-import javax.jms.MessageProducer;
-import javax.jms.Session;
-import javax.naming.NamingException;
-
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.status.StatusLogger;
-
-/**
- * JMS connection and session manager. Can be used to access MessageProducer, MessageConsumer, and Message objects
- * involving a configured ConnectionFactory and Destination.
- */
-public class JmsManager extends AbstractManager {
-
- private static final Logger LOGGER = StatusLogger.getLogger();
-
- private static final JmsManagerFactory FACTORY = new JmsManagerFactory();
-
- private final JndiManager jndiManager;
- private final Connection connection;
- private final Session session;
- private final Destination destination;
-
- private JmsManager(final String name, final JndiManager jndiManager, final String connectionFactoryName,
- final String destinationName, final String username, final String password)
- throws NamingException, JMSException {
- super(name);
- this.jndiManager = jndiManager;
- final ConnectionFactory connectionFactory = this.jndiManager.lookup(connectionFactoryName);
- if (username != null && password != null) {
- this.connection = connectionFactory.createConnection(username, password);
- } else {
- this.connection = connectionFactory.createConnection();
- }
- this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
- this.destination = this.jndiManager.lookup(destinationName);
- this.connection.start();
- }
-
- /**
- * Gets a JmsManager using the specified configuration parameters.
- *
- * @param name The name to use for this JmsManager.
- * @param jndiManager The JndiManager to look up JMS information through.
- * @param connectionFactoryName The binding name for the {@link javax.jms.ConnectionFactory}.
- * @param destinationName The binding name for the {@link javax.jms.Destination}.
- * @param username The username to connect with or {@code null} for no authentication.
- * @param password The password to use with the given username or {@code null} for no authentication.
- * @return The JmsManager as configured.
- */
- public static JmsManager getJmsManager(final String name, final JndiManager jndiManager,
- final String connectionFactoryName, final String destinationName,
- final String username, final String password) {
- final JmsConfiguration configuration = new JmsConfiguration(jndiManager, connectionFactoryName, destinationName,
- username, password);
- return FACTORY.createManager(name, configuration);
- }
-
- /**
- * Creates a MessageConsumer on this Destination using the current Session.
- *
- * @return A MessageConsumer on this Destination.
- * @throws JMSException
- */
- public MessageConsumer createMessageConsumer() throws JMSException {
- return this.session.createConsumer(this.destination);
- }
-
- /**
- * Creates a MessageProducer on this Destination using the current Session.
- *
- * @return A MessageProducer on this Destination.
- * @throws JMSException
- */
- public MessageProducer createMessageProducer() throws JMSException {
- return this.session.createProducer(this.destination);
- }
-
- /**
- * Creates a TextMessage or ObjectMessage from a Serializable object. For instance, when using a text-based
- * {@link org.apache.logging.log4j.core.Layout} such as {@link org.apache.logging.log4j.core.layout.PatternLayout},
- * the {@link org.apache.logging.log4j.core.LogEvent} message will be serialized to a String. When using a
- * layout such as {@link org.apache.logging.log4j.core.layout.SerializedLayout}, the LogEvent message will be
- * serialized as a Java object.
- *
- * @param object The LogEvent or String message to wrap.
- * @return A new JMS message containing the provided object.
- * @throws JMSException
- */
- public Message createMessage(final Serializable object) throws JMSException {
- if (object instanceof String) {
- return this.session.createTextMessage((String) object);
- } else {
- return this.session.createObjectMessage(object);
- }
- }
-
- @Override
- protected void releaseSub() {
- try {
- this.session.close();
- } catch (final JMSException ignored) {
- }
- try {
- this.connection.close();
- } catch (final JMSException ignored) {
- }
- this.jndiManager.release();
- }
-
- private static class JmsConfiguration {
- private final JndiManager jndiManager;
- private final String connectionFactoryName;
- private final String destinationName;
- private final String username;
- private final String password;
-
- private JmsConfiguration(JndiManager jndiManager, String connectionFactoryName, String destinationName,
- String username, String password) {
- this.jndiManager = jndiManager;
- this.connectionFactoryName = connectionFactoryName;
- this.destinationName = destinationName;
- this.username = username;
- this.password = password;
- }
- }
-
- private static class JmsManagerFactory implements ManagerFactory<JmsManager, JmsConfiguration> {
-
- @Override
- public JmsManager createManager(String name, JmsConfiguration data) {
- try {
- return new JmsManager(name, data.jndiManager, data.connectionFactoryName, data.destinationName,
- data.username, data.password);
- } catch (final Exception e) {
- LOGGER.error("Error creating JmsManager using ConnectionFactory [{}] and Destination [{}].",
- data.connectionFactoryName, data.destinationName, e);
- return null;
- }
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java
deleted file mode 100644
index 873b7f4..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/manager/JndiManager.java
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.manager;
-
-import java.util.Properties;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.appender.AbstractManager;
-import org.apache.logging.log4j.core.appender.ManagerFactory;
-import org.apache.logging.log4j.core.util.JndiCloser;
-import org.apache.logging.log4j.status.StatusLogger;
-
-/**
- * JNDI context manager. Mainly useful for finding JMS objects and configuring the InitialContext.
- */
-public class JndiManager extends AbstractManager {
-
- private static final Logger LOGGER = StatusLogger.getLogger();
-
- private static final JndiManagerFactory FACTORY = new JndiManagerFactory();
-
- private final Context context;
-
- private JndiManager(String name, Context context) {
- super(name);
- this.context = context;
- }
-
- /**
- * Gets a JndiManager with the provided configuration information.
- *
- * @param initialContextFactoryName Fully qualified class name of an implementation of
- * {@link javax.naming.spi.InitialContextFactory}.
- * @param providerURL The provider URL to use for the JNDI connection (specific to the above factory).
- * @param urlPkgPrefixes A colon-separated list of package prefixes for the class name of the factory
- * class that will create a URL context factory
- * @param securityPrincipal The name of the identity of the Principal.
- * @param securityCredentials The security credentials of the Principal.
- * @param additionalProperties Any additional JNDI environment properties to set or {@code null} for none.
- * @return the JndiManager for the provided parameters.
- */
- public static JndiManager getJndiManager(final String initialContextFactoryName,
- final String providerURL,
- final String urlPkgPrefixes,
- final String securityPrincipal,
- final String securityCredentials,
- final Properties additionalProperties) {
- final String name = JndiManager.class.getName() + '@' + JndiManager.class.hashCode();
- if (initialContextFactoryName == null) {
- return getManager(name, FACTORY, null);
- }
- final Properties properties = new Properties();
- properties.setProperty(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
- if (providerURL != null) {
- properties.setProperty(Context.PROVIDER_URL, providerURL);
- } else {
- LOGGER.warn("The JNDI InitialContextFactory class name [{}] was provided, but there was no associated " +
- "provider URL. This is likely to cause problems.", initialContextFactoryName);
- }
- if (urlPkgPrefixes != null) {
- properties.setProperty(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
- }
- if (securityPrincipal != null) {
- properties.setProperty(Context.SECURITY_PRINCIPAL, securityPrincipal);
- if (securityCredentials != null) {
- properties.setProperty(Context.SECURITY_CREDENTIALS, securityCredentials);
- } else {
- LOGGER.warn("A security principal [{}] was provided, but with no corresponding security credentials.",
- securityPrincipal);
- }
- }
- if (additionalProperties != null) {
- properties.putAll(additionalProperties);
- }
- return getManager(name, FACTORY, properties);
- }
-
- @Override
- protected void releaseSub() {
- JndiCloser.closeSilently(this.context);
- }
-
- /**
- * Looks up a named object through this JNDI context.
- *
- * @param name name of the object to look up.
- * @param <T> the type of the object.
- * @return the named object if it could be located.
- * @throws NamingException
- */
- @SuppressWarnings("unchecked")
- public <T> T lookup(final String name) throws NamingException {
- return (T) this.context.lookup(name);
- }
-
- private static class JndiManagerFactory implements ManagerFactory<JndiManager, Properties> {
-
- @Override
- public JndiManager createManager(String name, Properties data) {
- try {
- return new JndiManager(name, new InitialContext(data));
- } catch (final NamingException e) {
- LOGGER.error("Error creating JNDI InitialContext.", e);
- return null;
- }
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/AbstractJmsReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/AbstractJmsReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/AbstractJmsReceiver.java
deleted file mode 100644
index bf86c65..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/AbstractJmsReceiver.java
+++ /dev/null
@@ -1,83 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.receiver;
-
-import java.io.Serializable;
-
-import javax.jms.JMSException;
-import javax.jms.ObjectMessage;
-import javax.naming.Context;
-import javax.naming.NameNotFoundException;
-import javax.naming.NamingException;
-
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LogEventListener;
-
-/**
- * Abstract base class for receiving LogEvents over JMS. This class expects all messages to be serialized log events.
- */
-public abstract class AbstractJmsReceiver extends LogEventListener implements javax.jms.MessageListener {
-
- /**
- * Logger to capture diagnostics.
- */
- protected Logger logger = LogManager.getLogger(this.getClass().getName());
-
- /**
- * Listener that receives the event.
- * @param message The received message.
- */
- @Override
- public void onMessage(final javax.jms.Message message) {
- try {
- if (message instanceof ObjectMessage) {
- final ObjectMessage objectMessage = (ObjectMessage) message;
- final Serializable object = objectMessage.getObject();
- if (object instanceof LogEvent) {
- log((LogEvent) object);
- } else {
- logger.warn("Received message is of type " + object.getClass().getName() + ", was expecting LogEvent.");
- }
- } else {
- logger.warn("Received message is of type " + message.getJMSType()
- + ", was expecting ObjectMessage.");
- }
- } catch (final JMSException jmse) {
- logger.error("Exception thrown while processing incoming message.",
- jmse);
- }
- }
-
- /**
- * Looks up an object from the Context.
- * @param ctx The Context.
- * @param name The name of the object to locate.
- * @return The object.
- * @throws NamingException if an error occurs.
- */
- protected Object lookup(final Context ctx, final String name) throws NamingException {
- try {
- return ctx.lookup(name);
- } catch (final NameNotFoundException e) {
- logger.error("Could not find name [" + name + "].");
- throw e;
- }
- }
-
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsQueueReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsQueueReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsQueueReceiver.java
deleted file mode 100644
index b231489..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsQueueReceiver.java
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.receiver;
-
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.nio.charset.Charset;
-
-import javax.jms.JMSException;
-import javax.jms.Queue;
-import javax.jms.QueueConnection;
-import javax.jms.QueueConnectionFactory;
-import javax.jms.QueueReceiver;
-import javax.jms.QueueSession;
-import javax.jms.Session;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-/**
- * Receives Log Events over a JMS Queue. This implementation expects that all messages will
- * contain a serialized LogEvent.
- */
-public class JmsQueueReceiver extends AbstractJmsReceiver {
-
- /**
- * Constructor.
- * @param qcfBindingName The QueueConnectionFactory binding name.
- * @param queueBindingName The Queue binding name.
- * @param username The userid to connect to the queue.
- * @param password The password to connect to the queue.
- */
- public JmsQueueReceiver(final String qcfBindingName, final String queueBindingName, final String username,
- final String password) {
-
- try {
- final Context ctx = new InitialContext();
- QueueConnectionFactory queueConnectionFactory;
- queueConnectionFactory = (QueueConnectionFactory) lookup(ctx, qcfBindingName);
- final QueueConnection queueConnection = queueConnectionFactory.createQueueConnection(username, password);
- queueConnection.start();
- final QueueSession queueSession = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
- final Queue queue = (Queue) ctx.lookup(queueBindingName);
- final QueueReceiver queueReceiver = queueSession.createReceiver(queue);
- queueReceiver.setMessageListener(this);
- } catch (final JMSException e) {
- logger.error("Could not read JMS message.", e);
- } catch (final NamingException e) {
- logger.error("Could not read JMS message.", e);
- } catch (final RuntimeException e) {
- logger.error("Could not read JMS message.", e);
- }
- }
-
- /**
- * Main startup for the receiver.
- * @param args The command line arguments.
- * @throws Exception if an error occurs.
- */
- public static void main(final String[] args) throws Exception {
- if (args.length != 4) {
- usage("Wrong number of arguments.");
- }
-
- final String qcfBindingName = args[0];
- final String queueBindingName = args[1];
- final String username = args[2];
- final String password = args[3];
-
- new JmsQueueReceiver(qcfBindingName, queueBindingName, username, password);
-
- final Charset enc = Charset.defaultCharset();
- final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
- // Loop until the word "exit" is typed
- System.out.println("Type \"exit\" to quit JmsQueueReceiver.");
- while (true) {
- final String line = stdin.readLine();
- if (line == null || line.equalsIgnoreCase("exit")) {
- System.out.println("Exiting. Kill the application if it does not exit "
- + "due to daemon threads.");
- return;
- }
- }
- }
-
-
- private static void usage(final String msg) {
- System.err.println(msg);
- System.err.println("Usage: java " + JmsQueueReceiver.class.getName()
- + " QueueConnectionFactoryBindingName QueueBindingName username password");
- System.exit(1);
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsTopicReceiver.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsTopicReceiver.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsTopicReceiver.java
deleted file mode 100644
index 0149ee5..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/JmsTopicReceiver.java
+++ /dev/null
@@ -1,105 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.logging.log4j.mom.jms.receiver;
-
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.nio.charset.Charset;
-
-import javax.jms.JMSException;
-import javax.jms.Session;
-import javax.jms.Topic;
-import javax.jms.TopicConnection;
-import javax.jms.TopicConnectionFactory;
-import javax.jms.TopicSession;
-import javax.jms.TopicSubscriber;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-
-/**
- * Receives Topic messages that contain LogEvents. This implementation expects that all messages
- * are serialized log events.
- */
-public class JmsTopicReceiver extends AbstractJmsReceiver {
-
- /**
- * Constructor.
- * @param tcfBindingName The TopicConnectionFactory binding name.
- * @param topicBindingName The Topic binding name.
- * @param username The userid to connect to the topic.
- * @param password The password to connect to the topic.
- */
- public JmsTopicReceiver(final String tcfBindingName, final String topicBindingName, final String username,
- final String password) {
- try {
- final Context ctx = new InitialContext();
- TopicConnectionFactory topicConnectionFactory;
- topicConnectionFactory = (TopicConnectionFactory) lookup(ctx, tcfBindingName);
- final TopicConnection topicConnection = topicConnectionFactory.createTopicConnection(username, password);
- topicConnection.start();
- final TopicSession topicSession = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
- final Topic topic = (Topic) ctx.lookup(topicBindingName);
- final TopicSubscriber topicSubscriber = topicSession.createSubscriber(topic);
- topicSubscriber.setMessageListener(this);
- } catch (final JMSException e) {
- logger.error("Could not read JMS message.", e);
- } catch (final NamingException e) {
- logger.error("Could not read JMS message.", e);
- } catch (final RuntimeException e) {
- logger.error("Could not read JMS message.", e);
- }
- }
-
- /**
- * Main startup for the receiver.
- * @param args The command line arguments.
- * @throws Exception if an error occurs.
- */
- public static void main(final String[] args) throws Exception {
- if (args.length != 4) {
- usage("Wrong number of arguments.");
- }
-
- final String tcfBindingName = args[0];
- final String topicBindingName = args[1];
- final String username = args[2];
- final String password = args[3];
-
- new JmsTopicReceiver(tcfBindingName, topicBindingName, username, password);
-
- final Charset enc = Charset.defaultCharset();
- final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, enc));
- // Loop until the word "exit" is typed
- System.out.println("Type \"exit\" to quit JmsTopicReceiver.");
- while (true) {
- final String line = stdin.readLine();
- if (line == null || line.equalsIgnoreCase("exit")) {
- System.out.println("Exiting. Kill the application if it does not exit "
- + "due to daemon threads.");
- return;
- }
- }
- }
-
- private static void usage(final String msg) {
- System.err.println(msg);
- System.err.println("Usage: java " + JmsTopicReceiver.class.getName()
- + " TopicConnectionFactoryBindingName TopicBindingName username password");
- System.exit(1);
- }
-}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/package-info.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/package-info.java b/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/package-info.java
deleted file mode 100644
index 1f233e8..0000000
--- a/log4j-jms/src/main/java/org/apache/logging/log4j/mom/jms/receiver/package-info.java
+++ /dev/null
@@ -1,25 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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.
- */
-/**
- * Supporting network code for JMS appenders.
- * <p/>
- * Note that you can use JmsQueueReceiver or JmsTopicReceiver as executable main classes to receive log events over
- * JMS (sent via the appropriate JMS appender) that can be subsequently logged according to the configuration given to
- * the running process. Of course, use of these classes as standalone executables are entirely optional and can
- * be used directly in your application (e.g., through your Spring {@code beans.xml} configuration).
- */
-package org.apache.logging.log4j.mom.jms.receiver;
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/7ddb07bd/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderIT.java
----------------------------------------------------------------------
diff --git a/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderIT.java b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderIT.java
new file mode 100644
index 0000000..fad68e5
--- /dev/null
+++ b/log4j-jms/src/test/java/org/apache/logging/log4j/jms/appender/JmsAppenderIT.java
@@ -0,0 +1,126 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.logging.log4j.jms.appender;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Properties;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageConsumer;
+import javax.jms.MessageListener;
+import javax.jms.ObjectMessage;
+
+import org.apache.activemq.jndi.ActiveMQInitialContextFactory;
+import org.apache.logging.log4j.Level;
+import org.apache.logging.log4j.core.LogEvent;
+import org.apache.logging.log4j.core.impl.Log4jLogEvent;
+import org.apache.logging.log4j.core.layout.SerializedLayout;
+import org.apache.logging.log4j.jms.manager.JmsManager;
+import org.apache.logging.log4j.jms.manager.JndiManager;
+import org.apache.logging.log4j.message.SimpleMessage;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * Integration test for JmsAppender using an embedded ActiveMQ broker.
+ */
+public class JmsAppenderIT {
+
+ private static JmsManager jmsManager;
+
+ private JmsAppender appender;
+
+ @BeforeClass
+ public static void setUpClass() {
+ final Properties additional = new Properties();
+ additional.setProperty("queue.TestQueue", "TestQueue");
+ JndiManager jndiManager = JndiManager.getJndiManager(ActiveMQInitialContextFactory.class.getName(),
+ "vm://localhost?broker.persistent=false", null, null, null, additional);
+ jmsManager = JmsManager.getJmsManager("JmsManager", jndiManager, "ConnectionFactory", "TestQueue", null, null);
+ }
+
+ @AfterClass
+ public static void tearDownClass() {
+ jmsManager.release();
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ appender = new JmsAppender("JmsAppender", null, SerializedLayout.createLayout(), true, jmsManager);
+ appender.start();
+ }
+
+ @Test
+ public void testLogToQueue() throws Exception {
+ final int messageCount = 100;
+ final MessageConsumer messageConsumer = jmsManager.createMessageConsumer();
+ final JmsQueueConsumer consumer = new JmsQueueConsumer(messageCount);
+ messageConsumer.setMessageListener(consumer);
+ final String messageText = "Hello, World!";
+ final String loggerName = this.getClass().getName();
+ for (int i = 0; i < messageCount; i++) {
+ final LogEvent event = Log4jLogEvent.createEvent(loggerName, null, loggerName, Level.ERROR,
+ new SimpleMessage(messageText), null, null, null, null, Thread.currentThread().getName(), null,
+ System.currentTimeMillis());
+ appender.append(event);
+ }
+ consumer.awaitAndAssertAllMessagesConsumed();
+ }
+
+ private static class JmsQueueConsumer implements MessageListener {
+
+ private final int messageCount;
+ private final CountDownLatch countDownLatch;
+ private final Collection<LogEvent> events;
+
+ private JmsQueueConsumer(final int messageCount) {
+ this.messageCount = messageCount;
+ this.countDownLatch = new CountDownLatch(messageCount);
+ this.events = new ArrayList<LogEvent>(messageCount);
+ }
+
+ @Override
+ public void onMessage(Message message) {
+ try {
+ consume((ObjectMessage) message);
+ } catch (JMSException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void consume(ObjectMessage message) throws JMSException {
+ try {
+ final LogEvent event = (LogEvent) message.getObject();
+ events.add(event);
+ } finally {
+ countDownLatch.countDown();
+ }
+ }
+
+ public void awaitAndAssertAllMessagesConsumed() throws InterruptedException {
+ countDownLatch.await(5, TimeUnit.SECONDS);
+ assertEquals(messageCount, events.size());
+ }
+ }
+}