This is an automated email from the ASF dual-hosted git repository. rgoers pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j-server.git
commit b707e7beb7db22211b266c1e5b5e8b1b5af8e00c Author: Gary Gregory <[email protected]> AuthorDate: Sun Jul 30 13:52:12 2017 -0700 Update JMS Server to match updated Appender. Move some classes ActiveMQ JUnit test rule in this module from the Core module. This is to avoid a dependency cycle. --- log4j-server/pom.xml | 5 ++ .../org/apache/logging/log4j/server/JmsServer.java | 22 ++--- .../logging/log4j/server/UdpSocketServer.java | 2 +- .../log4j/server/mom/jms/AbstractJmsReceiver.java | 33 ++++++-- .../log4j/server/mom/jms/JmsQueueReceiver.java | 8 +- .../log4j/server/mom/jms/JmsTopicReceiver.java | 6 +- .../mom/activemq/ActiveMqBrokerServiceHelper.java | 51 ++++++++++++ .../mom/activemq/ActiveMqBrokerServiceRule.java | 94 ++++++++++++++++++++++ .../log4j/server/mom/jms/JmsQueueReceiverTest.java | 42 ++++++++++ pom.xml | 15 +++- 10 files changed, 252 insertions(+), 26 deletions(-) diff --git a/log4j-server/pom.xml b/log4j-server/pom.xml index 29de565..36098e0 100644 --- a/log4j-server/pom.xml +++ b/log4j-server/pom.xml @@ -93,6 +93,11 @@ <artifactId>mockito-core</artifactId> <scope>test</scope> </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-broker</artifactId> + <scope>test</scope> + </dependency> </dependencies> <build> <plugins> diff --git a/log4j-server/src/main/java/org/apache/logging/log4j/server/JmsServer.java b/log4j-server/src/main/java/org/apache/logging/log4j/server/JmsServer.java index 70477db..a6cf9cc 100644 --- a/log4j-server/src/main/java/org/apache/logging/log4j/server/JmsServer.java +++ b/log4j-server/src/main/java/org/apache/logging/log4j/server/JmsServer.java @@ -24,6 +24,7 @@ import java.nio.charset.Charset; import java.util.Properties; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicReference; + import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageConsumer; @@ -35,6 +36,7 @@ import org.apache.logging.log4j.core.AbstractLifeCycle; import org.apache.logging.log4j.core.LifeCycle2; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.LogEventListener; +import org.apache.logging.log4j.core.appender.mom.JmsAppender; import org.apache.logging.log4j.core.appender.mom.JmsManager; import org.apache.logging.log4j.core.net.JndiManager; @@ -49,14 +51,14 @@ public class JmsServer extends LogEventListener implements MessageListener, Life private final JmsManager jmsManager; private MessageConsumer messageConsumer; - public JmsServer(final String connectionFactoryBindingName, - final String destinationBindingName, - final String username, - final String password) { + public JmsServer(final String connectionFactoryBindingName, final String connectionFactoryName, + final String providerURL, final String destinationBindingName, final String username, final char[] password, + final Properties jndiProperties) { final String managerName = JmsServer.class.getName() + '@' + JmsServer.class.hashCode(); - // TODO init JmsManager properly - jmsManager = JmsManager.getJmsManager(managerName, jndiManager, connectionFactoryBindingName, - destinationBindingName, username, password.toCharArray(), false, 0L); + final Properties jndiManager = JndiManager.createProperties(connectionFactoryBindingName, providerURL, null, + null, null, jndiProperties); + jmsManager = JmsManager.getJmsManager(managerName, jndiManager, connectionFactoryName, destinationBindingName, + username, password, false, JmsAppender.Builder.DEFAULT_RECONNECT_INTERVAL_MILLIS); } @Override @@ -76,7 +78,7 @@ public class JmsServer extends LogEventListener implements MessageListener, Life } } else { LOGGER.warn("Received message of type {} and JMSType {} which cannot be handled.", message.getClass(), - message.getJMSType()); + message.getJMSType()); } } catch (final JMSException e) { LOGGER.catching(e); @@ -130,10 +132,8 @@ public class JmsServer extends LogEventListener implements MessageListener, Life * Starts and runs this server until the user types "exit" into standard input. * * @throws IOException - * @since 2.6 */ - public void run() throws IOException { - this.start(); + public void commandLineLoop() throws IOException { System.out.println("Type \"exit\" to quit."); final BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in, Charset.defaultCharset())); while (true) { diff --git a/log4j-server/src/main/java/org/apache/logging/log4j/server/UdpSocketServer.java b/log4j-server/src/main/java/org/apache/logging/log4j/server/UdpSocketServer.java index a19f217..db66653 100644 --- a/log4j-server/src/main/java/org/apache/logging/log4j/server/UdpSocketServer.java +++ b/log4j-server/src/main/java/org/apache/logging/log4j/server/UdpSocketServer.java @@ -182,7 +182,7 @@ public class UdpSocketServer<T extends InputStream> extends AbstractSocketServer @Override public void shutdown() { this.setActive(false); - Thread.currentThread().interrupt(); + //Thread.currentThread().interrupt(); datagramSocket.close(); } } diff --git a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/AbstractJmsReceiver.java b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/AbstractJmsReceiver.java index 373d31c..9103d69 100644 --- a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/AbstractJmsReceiver.java +++ b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/AbstractJmsReceiver.java @@ -17,6 +17,8 @@ package org.apache.logging.log4j.server.mom.jms; +import java.util.Properties; + import org.apache.logging.log4j.server.JmsServer; /** @@ -26,6 +28,10 @@ import org.apache.logging.log4j.server.JmsServer; */ public abstract class AbstractJmsReceiver { + class CommandLineArgs { + + } + /** * Prints out usage information to {@linkplain System#err standard error}. */ @@ -33,16 +39,31 @@ public abstract class AbstractJmsReceiver { /** * Executes a JmsServer with the given command line arguments. + * + * @param interactive + * Whether or not this is an interactive application by providing a command line and exit on error. + * @param args + * command line arguments * - * @param args command line arguments * @throws Exception */ - protected void doMain(final String... args) throws Exception { - if (args.length != 4) { + protected void doMain(boolean interactive, final String... args) throws Exception { + // TODO Too many args, Use JCommander + if (args.length < 5) { usage(); - System.exit(1); + if (interactive) { + System.exit(1); + } + } + final Properties properties = new Properties(); + for (int index = 5; index < args.length; index += 2) { + properties.put(args[index], args[index + 1]); + } + final JmsServer server = new JmsServer(args[0], "ConnectionFactory", args[1], args[2], args[3], + args[4].toCharArray(), properties); + server.start(); + if (interactive) { + server.commandLineLoop(); } - final JmsServer server = new JmsServer(args[0], args[1], args[2], args[3]); - server.run(); } } diff --git a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiver.java b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiver.java index a7f75ec..f7cadc8 100644 --- a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiver.java +++ b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiver.java @@ -23,7 +23,8 @@ package org.apache.logging.log4j.server.mom.jms; */ public class JmsQueueReceiver extends AbstractJmsReceiver { - private JmsQueueReceiver() { + JmsQueueReceiver() { + // Usage is to call main() } /** @@ -33,14 +34,13 @@ public class JmsQueueReceiver extends AbstractJmsReceiver { * @throws Exception if an error occurs. */ public static void main(final String[] args) throws Exception { - final JmsQueueReceiver receiver = new JmsQueueReceiver(); - receiver.doMain(args); + new JmsQueueReceiver().doMain(true, args); } @Override protected void usage() { System.err.println("Wrong number of arguments."); System.err.println("Usage: java " + JmsQueueReceiver.class.getName() - + " QueueConnectionFactoryBindingName QueueBindingName username password"); + + " QueueConnectionFactoryBindingName QueueBindingName username password [jndiPropertyKey jndiPropertyValue]*"); } } diff --git a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsTopicReceiver.java b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsTopicReceiver.java index 21828d7..8e50cdf 100644 --- a/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsTopicReceiver.java +++ b/log4j-server/src/main/java/org/apache/logging/log4j/server/mom/jms/JmsTopicReceiver.java @@ -24,6 +24,7 @@ package org.apache.logging.log4j.server.mom.jms; public class JmsTopicReceiver extends AbstractJmsReceiver { private JmsTopicReceiver() { + // Usage is to call main() } /** @@ -33,14 +34,13 @@ public class JmsTopicReceiver extends AbstractJmsReceiver { * @throws Exception if an error occurs. */ public static void main(final String[] args) throws Exception { - final JmsTopicReceiver receiver = new JmsTopicReceiver(); - receiver.doMain(args); + new JmsTopicReceiver().doMain(true, args); } @Override protected void usage() { System.err.println("Wrong number of arguments."); System.err.println("Usage: java " + JmsTopicReceiver.class.getName() - + " TopicConnectionFactoryBindingName TopicBindingName username password"); + + " TopicConnectionFactoryBindingName TopicBindingName username password [jndiPropertyKey jndiPropertyValue]*"); } } diff --git a/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceHelper.java b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceHelper.java new file mode 100644 index 0000000..e1fb522 --- /dev/null +++ b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceHelper.java @@ -0,0 +1,51 @@ +/* + * 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.server.mom.activemq; + +import java.io.IOException; + +import org.apache.activemq.broker.BrokerService; + +/** + * Helps starts an embedded Apache ActiveMQ service broker. + */ +public class ActiveMqBrokerServiceHelper { + + public static BrokerService startBrokerService(final String brokerName, String brokerUrlString, final int port) + throws Exception { + // TODO Abstract out scheme + brokerUrlString = "tcp://localhost:" + port; + final BrokerService broker = new BrokerService(); + // configure the Broker + broker.setBrokerName(brokerName); + broker.addConnector(brokerUrlString); + broker.setPersistent(false); + broker.start(); + broker.waitUntilStarted(); + return broker; + } + + public static void stopBrokerService(final BrokerService brokerService) throws IOException, Exception { + if (brokerService != null) { + brokerService.deleteAllMessages(); + brokerService.stop(); + brokerService.waitUntilStopped(); + } + } + +} diff --git a/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceRule.java b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceRule.java new file mode 100644 index 0000000..a3116ac --- /dev/null +++ b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/activemq/ActiveMqBrokerServiceRule.java @@ -0,0 +1,94 @@ +/* + * 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.server.mom.activemq; + +import org.apache.activemq.broker.BrokerService; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.TestMarkers; +import org.junit.rules.TestRule; +import org.junit.runner.Description; +import org.junit.runners.model.Statement; + +/** + * JUnit {@link TestRule} to manage an in-JVM Apache ActiveMQ broker with socket + * communications between clients and broker. + */ +public class ActiveMqBrokerServiceRule implements TestRule { + + static final Logger logger = LogManager.getLogger(ActiveMqBrokerServiceRule.class); + + /** + * Apache Active MQ uses this property name to lookup which port to use to + * connect to a broker. + */ + public static final String PORT_PROPERTY_NAME = "org.apache.activemq.AMQ_PORT"; + + private final String brokerName; + + private String brokerUrlString; + + private final String portPropertyName; + + public ActiveMqBrokerServiceRule(final String brokerName, final String portPropertyName) { + this.brokerName = brokerName; + this.portPropertyName = portPropertyName; + } + + @Override + public Statement apply(final Statement base, final Description description) { + return new Statement() { + + @Override + public void evaluate() throws Throwable { + final BrokerService broker = ActiveMqBrokerServiceHelper.startBrokerService(brokerName, brokerUrlString, + Integer.parseInt(System.getProperty(portPropertyName))); + logger.debug(TestMarkers.TEST_RULE_LIFE_CYCLE, "{} started Apache Active MQ {}", + this.getClass().getSimpleName(), this); + try { + base.evaluate(); + } finally { + ActiveMqBrokerServiceHelper.stopBrokerService(broker); + logger.debug(TestMarkers.TEST_RULE_LIFE_CYCLE, "{} stopped Apache Active MQ {}", + this.getClass().getSimpleName(), this); + } + } + + }; + } + + public String getBrokerName() { + return brokerName; + } + + public String getBrokerUrlString() { + return brokerUrlString; + } + + @Override + public String toString() { + final StringBuilder builder = new StringBuilder(); + builder.append("ActiveMqBrokerServiceRule [brokerName="); + builder.append(brokerName); + builder.append(", bindAddress="); + builder.append(brokerUrlString); + builder.append("]"); + return builder.toString(); + } + +} diff --git a/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiverTest.java b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiverTest.java new file mode 100644 index 0000000..891a4e0 --- /dev/null +++ b/log4j-server/src/test/java/org/apache/logging/log4j/server/mom/jms/JmsQueueReceiverTest.java @@ -0,0 +1,42 @@ +/* + * 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.server.mom.jms; + +import org.apache.logging.log4j.server.mom.activemq.ActiveMqBrokerServiceRule; +import org.apache.logging.log4j.test.AvailablePortSystemPropertyRule; +import org.apache.logging.log4j.test.RuleChainFactory; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.rules.RuleChain; + +public class JmsQueueReceiverTest { + + private static final AvailablePortSystemPropertyRule portRule = AvailablePortSystemPropertyRule + .create(ActiveMqBrokerServiceRule.PORT_PROPERTY_NAME); + + private static final ActiveMqBrokerServiceRule activeMqBrokerServiceRule = new ActiveMqBrokerServiceRule( + JmsQueueReceiverTest.class.getName(), portRule.getName()); + + @ClassRule + public static RuleChain ruleChain = RuleChainFactory.create(portRule, activeMqBrokerServiceRule); + + @Test + public void testMain() throws Exception { + new JmsQueueReceiver().doMain(false, new String[] { "org.apache.activemq.jndi.ActiveMQInitialContextFactory", + "tcp://localhost:" + portRule.getPort(), "testq", "admin", "admin", "queue.testq", "testq" }); + } +} diff --git a/pom.xml b/pom.xml index 4672795..d28a3c0 100644 --- a/pom.xml +++ b/pom.xml @@ -200,7 +200,8 @@ <projectDir /> <commonsLoggingVersion>1.2</commonsLoggingVersion> <osgi.api.version>4.3.1</osgi.api.version> - <activemq.version>5.14.4</activemq.version> + <!-- Version 5.15.0 requires Java 8 --> + <activemq.version>5.14.5</activemq.version> <!-- Allow Clirr severity to be overriden by the command-line option -DminSeverity=level --> <minSeverity>info</minSeverity> <jctoolsVersion>1.2.1</jctoolsVersion> @@ -262,6 +263,18 @@ <version>1.0.1.Final</version> <scope>provided</scope> </dependency> + <dependency> + <groupId>org.apache.activemq</groupId> + <artifactId>activemq-broker</artifactId> + <version>${activemq.version}</version> + <scope>test</scope> + <exclusions> + <exclusion> + <groupId>org.apache.geronimo.specs</groupId> + <artifactId>geronimo-jms_1.1_spec</artifactId> + </exclusion> + </exclusions> + </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId>
