You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ac...@apache.org on 2006/09/26 07:56:45 UTC
svn commit: r449919 - in /incubator/activemq/trunk/activemq-optional/src:
main/java/org/apache/activemq/util/ test/java/org/apache/activemq/util/
test/resources/org/apache/ test/resources/org/apache/activemq/
test/resources/org/apache/activemq/util/
Author: aco
Date: Mon Sep 25 22:56:44 2006
New Revision: 449919
URL: http://svn.apache.org/viewvc?view=rev&rev=449919
Log:
Applied patch from Bruce Snyder to port AMQ 3.x LogAppender utility to AMQ 4.x.
Refer to: https://issues.apache.org/activemq/browse/AMQ-935
Applied to rev 449914
Added:
incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/
incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java (with props)
incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java (with props)
incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java (with props)
incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/
incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java (with props)
incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/
incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/
incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/
incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties (with props)
Added: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java?view=auto&rev=449919
==============================================================================
--- incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java (added)
+++ incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java Mon Sep 25 22:56:44 2006
@@ -0,0 +1,87 @@
+/**
+ *
+ * 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.activemq.util;
+
+import java.net.URISyntaxException;
+
+import org.apache.activemq.ActiveMQConnection;
+
+import javax.jms.Connection;
+import javax.jms.JMSException;
+
+/**
+ * A JMS 1.1 log4j appender which uses ActiveMQ by default and does not require any JNDI
+ * configurations
+ *
+ * @version $Revision$
+ */
+public class JmsLogAppender extends JmsLogAppenderSupport {
+ private String uri = "tcp://localhost:61616";
+ private String userName;
+ private String password;
+
+ public JmsLogAppender() {
+ }
+
+ public String getUri() {
+ return uri;
+ }
+
+ public void setUri(String uri) {
+ this.uri = uri;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ protected Connection createConnection() throws JMSException {
+ if (userName != null) {
+ try {
+ return ActiveMQConnection.makeConnection(userName, password, uri);
+ } catch (URISyntaxException e) {
+ throw new JMSException("Unable to connect to a broker using " +
+ "userName: \'" + userName +
+ "\' password \'" + password +
+ "\' uri \'" + uri + "\' :: error - " +
+ e.getMessage());
+ }
+ }
+ else {
+ try {
+ return ActiveMQConnection.makeConnection(uri);
+ } catch (URISyntaxException e) {
+ throw new JMSException("Unable to connect to a broker using " +
+ "uri \'" + uri + "\' :: error - " +
+ e.getMessage());
+ }
+ }
+ }
+}
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppender.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java?view=auto&rev=449919
==============================================================================
--- incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java (added)
+++ incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java Mon Sep 25 22:56:44 2006
@@ -0,0 +1,175 @@
+/**
+ *
+ * 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.activemq.util;
+
+import org.apache.log4j.AppenderSkeleton;
+import org.apache.log4j.spi.LoggingEvent;
+
+import javax.jms.Connection;
+import javax.jms.Destination;
+import javax.jms.JMSException;
+import javax.jms.Message;
+import javax.jms.MessageProducer;
+import javax.jms.Session;
+import javax.naming.NamingException;
+import java.io.Serializable;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+/**
+ * An abstract base class for implementation inheritence for a log4j JMS appender
+ *
+ * @version $Revision$
+ */
+public abstract class JmsLogAppenderSupport extends AppenderSkeleton {
+
+ public static final int JMS_PUBLISH_ERROR_CODE = 61616;
+
+ private Connection connection;
+ private Session session;
+ private MessageProducer producer;
+ private boolean allowTextMessages = true;
+ private String subjectPrefix = "log4j.";
+
+ public JmsLogAppenderSupport() {
+ }
+
+ public Connection getConnection() throws JMSException, NamingException {
+ if (connection == null) {
+ connection = createConnection();
+ }
+ return connection;
+ }
+
+ public void setConnection(Connection connection) {
+ this.connection = connection;
+ }
+
+ public Session getSession() throws JMSException, NamingException {
+ if (session == null) {
+ session = createSession();
+ }
+ return session;
+ }
+
+ public void setSession(Session session) {
+ this.session = session;
+ }
+
+ public MessageProducer getProducer() throws JMSException, NamingException {
+ if (producer == null) {
+ producer = createProducer();
+ }
+ return producer;
+ }
+
+ public void setProducer(MessageProducer producer) {
+ this.producer = producer;
+ }
+
+ public void close() {
+ List errors = new ArrayList();
+ if (producer != null) {
+ try {
+ producer.close();
+ }
+ catch (JMSException e) {
+ errors.add(e);
+ }
+ }
+ if (session != null) {
+ try {
+ session.close();
+ }
+ catch (JMSException e) {
+ errors.add(e);
+ }
+ }
+ if (connection != null) {
+ try {
+ connection.close();
+ }
+ catch (JMSException e) {
+ errors.add(e);
+ }
+ }
+ for (Iterator iter = errors.iterator(); iter.hasNext();) {
+ JMSException e = (JMSException) iter.next();
+ getErrorHandler().error("Error closing JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE);
+ }
+ }
+
+ public boolean requiresLayout() {
+ return false;
+ }
+
+ public void activateOptions() {
+ try {
+ // lets ensure we're all created
+ getProducer();
+ }
+ catch (Exception e) {
+ getErrorHandler().error("Could not create JMS resources: " + e, e, JMS_PUBLISH_ERROR_CODE);
+ }
+ }
+
+
+ // Implementation methods
+ //-------------------------------------------------------------------------
+ protected abstract Connection createConnection() throws JMSException, NamingException;
+
+ protected Session createSession() throws JMSException, NamingException {
+ return getConnection().createSession(false, Session.AUTO_ACKNOWLEDGE);
+ }
+
+ protected MessageProducer createProducer() throws JMSException, NamingException {
+ return getSession().createProducer(null);
+ }
+
+ protected void append(LoggingEvent event) {
+ try {
+ Message message = createMessage(event);
+ Destination destination = getDestination(event);
+ getProducer().send(destination, message);
+ }
+ catch (Exception e) {
+ getErrorHandler().error("Could not send message due to: " + e, e, JMS_PUBLISH_ERROR_CODE, event);
+ }
+ }
+
+ protected Message createMessage(LoggingEvent event) throws JMSException, NamingException {
+ Message answer = null;
+ Object value = event.getMessage();
+ if (allowTextMessages && value instanceof String) {
+ answer = getSession().createTextMessage((String) value);
+ }
+ else {
+ answer = getSession().createObjectMessage((Serializable) value);
+ }
+ answer.setStringProperty("level", event.getLevel().toString());
+ answer.setIntProperty("levelInt", event.getLevel().toInt());
+ answer.setStringProperty("threadName", event.getThreadName());
+ return answer;
+ }
+
+ protected Destination getDestination(LoggingEvent event) throws JMSException, NamingException {
+ String name = subjectPrefix + event.getLoggerName();
+ return getSession().createTopic(name);
+ }
+}
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JmsLogAppenderSupport.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java?view=auto&rev=449919
==============================================================================
--- incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java (added)
+++ incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java Mon Sep 25 22:56:44 2006
@@ -0,0 +1,167 @@
+/**
+ *
+ * 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.activemq.util;
+
+import org.apache.log4j.helpers.LogLog;
+
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.JMSException;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.Hashtable;
+
+/**
+ * A JMS 1.1 log4j appender which uses JNDI to locate a JMS ConnectionFactory
+ * to use for logging events.
+ *
+ * @version $Revision$
+ */
+public class JndiJmsLogAppender extends JmsLogAppenderSupport {
+
+ private String jndiName;
+ private String userName;
+ private String password;
+
+ private String initialContextFactoryName;
+ private String providerURL;
+ private String urlPkgPrefixes;
+ private String securityPrincipalName;
+ private String securityCredentials;
+
+ public JndiJmsLogAppender() {
+ }
+
+ public String getJndiName() {
+ return jndiName;
+ }
+
+ public void setJndiName(String jndiName) {
+ this.jndiName = jndiName;
+ }
+
+ public String getUserName() {
+ return userName;
+ }
+
+ public void setUserName(String userName) {
+ this.userName = userName;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+
+ // to customize the JNDI context
+ //-------------------------------------------------------------------------
+ public String getInitialContextFactoryName() {
+ return initialContextFactoryName;
+ }
+
+ public void setInitialContextFactoryName(String initialContextFactoryName) {
+ this.initialContextFactoryName = initialContextFactoryName;
+ }
+
+ public String getProviderURL() {
+ return providerURL;
+ }
+
+ public void setProviderURL(String providerURL) {
+ this.providerURL = providerURL;
+ }
+
+ public String getUrlPkgPrefixes() {
+ return urlPkgPrefixes;
+ }
+
+ public void setUrlPkgPrefixes(String urlPkgPrefixes) {
+ this.urlPkgPrefixes = urlPkgPrefixes;
+ }
+
+ public String getSecurityPrincipalName() {
+ return securityPrincipalName;
+ }
+
+ public void setSecurityPrincipalName(String securityPrincipalName) {
+ this.securityPrincipalName = securityPrincipalName;
+ }
+
+ public String getSecurityCredentials() {
+ return securityCredentials;
+ }
+
+ public void setSecurityCredentials(String securityCredentials) {
+ this.securityCredentials = securityCredentials;
+ }
+
+ // Implementation methods
+ //-------------------------------------------------------------------------
+ protected Connection createConnection() throws JMSException, NamingException {
+ InitialContext context = createInitialContext();
+ LogLog.debug("Looking up ConnectionFactory with jndiName: " + jndiName);
+ ConnectionFactory factory = (ConnectionFactory) context.lookup(jndiName);
+ if (factory == null) {
+ throw new JMSException("No such ConnectionFactory for name: " + jndiName);
+ }
+ if (userName != null) {
+ return factory.createConnection(userName, password);
+ }
+ else {
+ return factory.createConnection();
+ }
+ }
+
+ protected InitialContext createInitialContext() throws NamingException {
+ if (initialContextFactoryName == null) {
+ return new InitialContext();
+ }
+ else {
+ Hashtable env = new Hashtable();
+ env.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactoryName);
+ if (providerURL != null) {
+ env.put(Context.PROVIDER_URL, providerURL);
+ }
+ else {
+ LogLog.warn("You have set InitialContextFactoryName option but not the "
+ + "ProviderURL. This is likely to cause problems.");
+ }
+ if (urlPkgPrefixes != null) {
+ env.put(Context.URL_PKG_PREFIXES, urlPkgPrefixes);
+ }
+
+ if (securityPrincipalName != null) {
+ env.put(Context.SECURITY_PRINCIPAL, securityPrincipalName);
+ if (securityCredentials != null) {
+ env.put(Context.SECURITY_CREDENTIALS, securityCredentials);
+ }
+ else {
+ LogLog.warn("You have set SecurityPrincipalName option but not the "
+ + "SecurityCredentials. This is likely to cause problems.");
+ }
+ }
+ LogLog.debug("Looking up JNDI context with environment: " + env);
+ return new InitialContext(env);
+ }
+ }
+}
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: incubator/activemq/trunk/activemq-optional/src/main/java/org/apache/activemq/util/JndiJmsLogAppender.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java?view=auto&rev=449919
==============================================================================
--- incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java (added)
+++ incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java Mon Sep 25 22:56:44 2006
@@ -0,0 +1,46 @@
+/**
+ *
+ * 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.activemq.util;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+import java.util.Properties;
+
+import org.apache.log4j.Logger;
+import org.apache.log4j.PropertyConfigurator;
+
+/**
+ * @version $Revision$
+ */
+public class JmsLogAppenderTest extends TestCase {
+
+
+ public void testLoggingWithJMS() throws IOException {
+
+ // lets try configure log4j
+ Properties properties = new Properties();
+ properties.load(getClass().getResourceAsStream("test-log4j.properties"));
+ PropertyConfigurator.configure(properties);
+
+ Logger.getLogger("FOO.BAR").info("Hello");
+ Logger.getLogger("FOO.BAR.WHATNOT").debug("A debug message");
+ Logger.getLogger("FOO.BAR.WHATNOT.ANOTHER").warn("Some warnings");
+
+ }
+}
Propchange: incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: incubator/activemq/trunk/activemq-optional/src/test/java/org/apache/activemq/util/JmsLogAppenderTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties
URL: http://svn.apache.org/viewvc/incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties?view=auto&rev=449919
==============================================================================
--- incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties (added)
+++ incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties Mon Sep 25 22:56:44 2006
@@ -0,0 +1,3 @@
+log4j.rootLogger=info, jms
+
+log4j.appender.jms=org.apache.activemq.util.JmsLogAppender
Propchange: incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: incubator/activemq/trunk/activemq-optional/src/test/resources/org/apache/activemq/util/test-log4j.properties
------------------------------------------------------------------------------
svn:mime-type = text/plain