[jboss-svn-commits] JBL Code SVN: r37900 - in labs/jbossesb/trunk/product/rosetta: tests/src/org/jboss/soa/esb/actions and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Feb 28 16:13:57 EST 2012


Author: tcunning
Date: 2012-02-28 16:13:55 -0500 (Tue, 28 Feb 2012)
New Revision: 37900

Added:
   labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/ServiceLoggerAction.java
   labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/ServiceLoggerActionUnitTest.java
Log:
JBESB-3728
Add a new logging action that can log text and messages based on log4j
settings set on a per service-category/service-name basis.


Added: labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/ServiceLoggerAction.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/ServiceLoggerAction.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/src/org/jboss/soa/esb/actions/ServiceLoggerAction.java	2012-02-28 21:13:55 UTC (rev 37900)
@@ -0,0 +1,143 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2012, JBoss Inc., and others contributors as indicated 
+ * by the @authors tag. All rights reserved. 
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors. 
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A 
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * MA  02110-1301, USA.
+ * 
+ * (C) 2005-2012
+ */
+
+package org.jboss.soa.esb.actions;
+
+import org.apache.commons.lang.builder.ReflectionToStringBuilder;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.jboss.soa.esb.actions.AbstractActionLifecycle;
+import org.jboss.soa.esb.actions.annotation.Process;
+import org.jboss.soa.esb.helpers.ConfigTree;
+import org.jboss.soa.esb.listeners.ListenerTagNames;
+import org.jboss.soa.esb.listeners.message.MessageDeliverException;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.MessagePayloadProxy;
+
+/**
+ * Simple action that logs custom text and possibly the message to a logger using the 
+ * "<service-category>.<service-name>" appender.    This action differs from LogAction
+ * in that it allows you to set LogLevelAction on a per service basis, rather
+ * than on a per-action basis, and that it allows you to log custom text.
+ * Debug level setting will result in the ESB message being output.
+ * Trace level setting will result in the output of the message payload
+ * 
+ * Here is an example of how to use this within your jboss-esb.xml :
+ * <p/>
+ * <code>
+ * &lt;action name="ServiceLoggerAction"
+ * 		class="org.jboss.soa.esb.actions.ServiceLoggerAction"&gt;<br/>
+ * &nbsp;&nbsp;&nbsp;&lt;property name="text" value="Reached here"/&gt;<br/>
+ * &nbsp;&nbsp;&nbsp;&lt;property name="get-payload-location" value="foo"/&gt;<br/>
+ * &lt;/action&gt;<br/>
+ * </code>
+ * <p/>
+ * 
+ * Property description:
+ * <ul>
+ * <li><i>text</i>  Custom text to be output, defaults to action name
+ * <li><i>get-payload-location</i> True or False value which specifies
+ * whether the payload location should be logged in Trace 
+ * </ul>         
+ * 
+ * 
+ * @author <a href="mailto:noconnor at redhat.com">noconnor at redhat.com</a>
+ * @author <a href="mailto:tcunning at redhat.com">tcunning at redhat.com</a>
+ */
+public class ServiceLoggerAction extends AbstractActionLifecycle {
+	public static final String TEXT_TAG = "text";
+	public static final String LOG_PAYLOAD_LOCATION_TAG = "get-payload-location";
+	
+	protected ConfigTree _config;
+	private Logger myLogger;
+	private MessagePayloadProxy payloadProxy;
+
+	public ServiceLoggerAction() {	
+		myLogger = Logger.getLogger(this.getClass());
+	}
+	
+	public boolean isLogPayloadLocation() {
+		return logPayloadLocation;
+	}
+
+	public void setLogPayloadLocation(boolean logPayloadLocation) {
+		this.logPayloadLocation = logPayloadLocation;
+	}
+
+	public String getText() {
+		return text;
+	}
+
+	public void setText(String text) {
+		this.text = text;
+	}
+
+	private boolean logPayloadLocation;
+	
+	private String currCategoryName;
+	private String currActionName;
+	private String text;
+
+	public Logger getLogger() {
+		return myLogger;
+	}
+	
+	public ServiceLoggerAction(ConfigTree configTree) {
+		myLogger = Logger.getLogger(configTree.getParent().getAttribute(
+				ListenerTagNames.SERVICE_CATEGORY_NAME_TAG) + "."
+				+ configTree.getParent().getAttribute(ListenerTagNames.SERVICE_NAME_TAG));
+		
+		currCategoryName = configTree
+				.getAttribute(ListenerTagNames.SERVICE_CATEGORY_NAME_TAG);
+		currActionName = configTree
+				.getAttribute(ListenerTagNames.ACTION_ELEMENT_TAG);
+
+		logPayloadLocation = "true".equals(configTree.getAttribute(LOG_PAYLOAD_LOCATION_TAG));
+		text = configTree.getAttribute(TEXT_TAG, currCategoryName + "." + currActionName);
+
+		payloadProxy = new MessagePayloadProxy(configTree);
+	}
+
+
+	@Process
+	public Message process(Message message) {
+		myLogger.info(text);
+		if (message != null) {
+			if (myLogger.isDebugEnabled()) {
+				myLogger.debug(message.toString());
+			}
+			if (myLogger.isTraceEnabled()) {
+				try {
+					if (logPayloadLocation) {
+						String payloadLocation = payloadProxy.getGetPayloadLocation();
+						myLogger.trace(payloadLocation);						
+					} 
+					
+					Object msgObj = payloadProxy.getPayload(message);
+					myLogger.trace(ReflectionToStringBuilder.toString(msgObj));
+					
+				} catch (MessageDeliverException e) {
+					myLogger.trace("Exception thrown during trace prepare"+e.getMessage());
+				}
+			}
+		}
+		return message;
+	}
+}

Added: labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/ServiceLoggerActionUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/ServiceLoggerActionUnitTest.java	                        (rev 0)
+++ labs/jbossesb/trunk/product/rosetta/tests/src/org/jboss/soa/esb/actions/ServiceLoggerActionUnitTest.java	2012-02-28 21:13:55 UTC (rev 37900)
@@ -0,0 +1,63 @@
+/*
+ * JBoss, Home of Professional Open Source Copyright 2006, JBoss Inc., and
+ * individual contributors as indicated by the @authors tag. See the
+ * copyright.txt in the distribution for a full listing of individual
+ * contributors.
+ * 
+ * This is free software; you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation; either version 2.1 of the License, or (at your option)
+ * any later version.
+ * 
+ * This software is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ * 
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this software; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA, or see the FSF
+ * site: http://www.fsf.org.
+ */
+package org.jboss.soa.esb.actions;
+
+import static org.junit.Assert.*;
+import junit.framework.JUnit4TestAdapter;
+
+import org.apache.log4j.Level;
+import org.jboss.soa.esb.message.Message;
+import org.jboss.soa.esb.message.format.MessageFactory;
+import org.junit.Test;
+
+/**
+ * Unit test for {@link ServiceLoggerAction}
+ * 
+ * @author <a href="mailto:tcunning at redhat.com">Tom Cunningham</a>			
+ *
+ */
+public class ServiceLoggerActionUnitTest
+{
+	@Test
+	public void logLevel()
+	{
+		ServiceLoggerAction logAction = new ServiceLoggerAction();
+		assertEquals( Level.DEBUG, logAction.getLogger().getLevel() );
+	}
+			
+	@Test
+	public void process()
+	{
+		ServiceLoggerAction logAction = new ServiceLoggerAction();
+		logAction.setText("foo");
+		logAction.setLogPayloadLocation(true);
+		Message message = MessageFactory.getInstance().getMessage();
+		message.getBody().add( " some body..." );
+		logAction.process( message );
+	}
+	
+	public static junit.framework.Test suite()
+	{
+		return new JUnit4TestAdapter( ServiceLoggerActionUnitTest.class );
+	}
+
+}



More information about the jboss-svn-commits mailing list