[jboss-svn-commits] JBL Code SVN: r21004 - in labs/jbossesb/trunk/product/services/spring/src: test/java/org/jboss/soa/esb/actions/spring and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Jul 11 03:29:51 EDT 2008


Author: beve
Date: 2008-07-11 03:29:51 -0400 (Fri, 11 Jul 2008)
New Revision: 21004

Added:
   labs/jbossesb/trunk/product/services/spring/src/test/resources/log4j.xml
Modified:
   labs/jbossesb/trunk/product/services/spring/src/main/java/org/jboss/soa/esb/actions/AbstractSpringAction.java
   labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/AbstractSpringActionUnitTest.java
   labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/MockSpringAction.java
Log:
Work for JBESB-1660 "Improved Spring integration"


Modified: labs/jbossesb/trunk/product/services/spring/src/main/java/org/jboss/soa/esb/actions/AbstractSpringAction.java
===================================================================
--- labs/jbossesb/trunk/product/services/spring/src/main/java/org/jboss/soa/esb/actions/AbstractSpringAction.java	2008-07-11 07:11:10 UTC (rev 21003)
+++ labs/jbossesb/trunk/product/services/spring/src/main/java/org/jboss/soa/esb/actions/AbstractSpringAction.java	2008-07-11 07:29:51 UTC (rev 21004)
@@ -24,171 +24,200 @@
 import org.apache.log4j.Logger;
 import org.jboss.soa.esb.helpers.ConfigTree;
 import org.jboss.soa.esb.message.Message;
+import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.BeanFactory;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 
 /**
- * Abstract class for Actions that make calls to Spring beans. 
+ * Abstract class for Actions that makes calls to Spring beans.
+ * <p/>
+ * <p>
+ * <pre>{@code
+ * Usage :
+ * <action name="sayHello" class="xyx.com.SimpleSpringAction" process="process">
+ *     <property name="springContextXml" value="spring-context1.xml, spring-context2.xml"/>
+ * </action>
+ * }</pre><br>
  * 
- * <p>This class creates a Spring IoC from a jboss-esb.xml element named "springContextXml". 
- * A comma separated list of spring context files may be used.
+ * Description of configuration properties:
+ * <ul>
+ * <li><i>springContextXml</i>: A single Spring bean definition xml file or a comma separated list of xml files.
+ * </ul>
  * 
  * @author <a href="mailto:james.williams at redhat.com">James Williams</a>.
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
  * 
  */
-public abstract class AbstractSpringAction extends AbstractActionLifecycle
-      implements ActionLifecycle
+public abstract class AbstractSpringAction extends AbstractActionLifecycle implements ActionLifecycle
 {
-   private static Logger logger = Logger.getLogger(AbstractSpringAction.class);
+	private Logger logger = Logger.getLogger( AbstractSpringAction.class );
+	
+	/** property name used in config file */
+	private static final String SPRING_CONTEXT_XML_ATTR = "springContextXml";
 
-   private static final String SPRING_CONTEXT_XML_ATTR = "springContextXml";
+	/** Spring bean definition xml fil */
+	private String springContextXml;
 
-   private String springContextXml;
+	/** configuration object */
+	protected ConfigTree configTree;
 
-   protected ConfigTree configTree;
+	/** Spring bean factory */
+	private BeanFactory factory;
 
-   private BeanFactory factory;
+	/**
+	 * Constructs and instance with storing the passed in ConfigTree instance.
+	 * <p/>
+	 * 
+	 * @param configTree the ConfigTree instance that will be stored
+	 */
+	public AbstractSpringAction(final ConfigTree configTree)
+	{
+		this.configTree = configTree;
+	}
 
-   /*
-    * (non-Javadoc)
-    * 
-    * @see org.jboss.soa.esb.actions.AbstractActionLifecycle#initialise()
-    */
-   public void initialise() throws ActionLifecycleException
-   {
-      springContextXml = configTree.getAttribute(SPRING_CONTEXT_XML_ATTR);
+	/**
+	 * Please do not call this no-args constructor and use
+	 * {@link #AbstractSpringAction(ConfigTree)} instead. This is because
+	 * the field configTree will be using in the {@link #initialise()} method
+	 * and will be null this config tree is not saved.
+	 * 
+	 * @deprecated Use {@link #AbstractSpringAction(ConfigTree)} instead
+	 */
+	public AbstractSpringAction()
+	{
+	}
 
-      if (springContextXml == null)
-      {
-         logger.error("No Spring context specified on action config "
-               + configTree.getAttribute("name")
-               + ". Expecting Spring context name.");
-         throw new ActionLifecycleException(
-               "No Spring context specified on action config: "
-                     + SPRING_CONTEXT_XML_ATTR + ".");
-      } else
-      {
-         initializeSpring();
-      }
-   }
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.jboss.soa.esb.actions.AbstractActionLifecycle#initialise()
+	 */
+	public void initialise() throws ActionLifecycleException
+	{
+		if (configTree == null)
+		{
+			throw new ActionLifecycleException( "Please make sure that the AbstractSpringAction(ConfigTree configTree) constructor was called and not the no-args constructor." );
+		}
 
-   /**
-    * Initialize Spring IoC
-    * 
-    * @throws ActionLifecycleException
-    */
-   protected void initializeSpring() throws ActionLifecycleException
-   {
-      if (isBeanFactoryNull())
-      {
-         loadSpringIoc();
-      }
-   }
+		springContextXml = configTree.getAttribute( SPRING_CONTEXT_XML_ATTR );
+		if (springContextXml == null)
+		{
+			throw new ActionLifecycleException( "No Spring context specified on action config: " + SPRING_CONTEXT_XML_ATTR + "." );
+		} 
+		
+		initializeSpring();
+	}
 
-   /**
-    * Request a BeanFactory instance from the action. If no Spring IoC container
-    * exists, create one.
-    * 
-    * @return Spring Bean Factory
-    * @throws ActionLifecycleException
-    */
-   protected BeanFactory getBeanFactory() throws ActionLifecycleException
-   {
-      return this.factory;
-   }
+	/**
+	 * Check to see if Spring Bean factory is null. Mostly used for unit tests,
+	 * but could provide use in other situations.
+	 * 
+	 * @return true if the bean factory is null
+	 */
+	public boolean isBeanFactoryNull()
+	{
+		return factory == null;
+	}
+	
+	/**
+	 * Generic Exception handler for Spring Actions. Displays the root cause
+	 * message and full stack trace.
+	 * 
+	 * @param message
+	 * @param exception
+	 */
+	public void exceptionHandler( Message message, Throwable exception )
+	{
+		Throwable rootCause = exception.getCause();
+		StackTraceElement[] traceElms = rootCause.getStackTrace();
 
-   /**
-    * Check to see if Spring Bean factory is null. Mostly used for unit tests,
-    * but could provide use in other situations.
-    * 
-    * @return
-    */
-   public boolean isBeanFactoryNull()
-   {
-      if (factory == null)
-      {
-         return true;
-      } else
-      {
-         return false;
-      }
-   }
+		StringBuffer stackTrace = new StringBuffer( "Exception Root Cause is: \n" );
+		stackTrace.append( rootCause.getMessage() );
+		stackTrace.append( "\n Full Stack Trace is: \n" );
+		for (StackTraceElement elm : traceElms)
+		{
+			stackTrace.append( elm );
+			stackTrace.append( "\n" );
+		}
 
-   /**
-    * Create a spring IoC container.
-    * 
-    * @throws ActionLifecycleException
-    */
-   private void loadSpringIoc() throws ActionLifecycleException
-   {
-      String springContextXml = configTree
-            .getAttribute(SPRING_CONTEXT_XML_ATTR);
-      try
-      {
-         ApplicationContext springContext;
+		logger.error( stackTrace.toString() );
+	}
+	
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see org.jboss.soa.esb.actions.AbstractActionLifecycle#destroy()
+	 */
+	public void destroy() throws ActionLifecycleException
+	{
+	}
 
-         if (springContextXml.contains(","))
-         {
-            String[] springContextXmls = springContextXml.split(",");
-            springContext = new ClassPathXmlApplicationContext(
-                  springContextXmls);
-         } else
-         {
-            springContext = new ClassPathXmlApplicationContext(springContextXml);
-         }
+	/**
+	 * Initialize Spring IoC
+	 * 
+	 * @throws ActionLifecycleException
+	 */
+	protected void initializeSpring() throws ActionLifecycleException
+	{
+		if (isBeanFactoryNull())
+		{
+			loadSpringIoc();
+		}
+	}
 
-         factory = (BeanFactory) springContext;
-      } catch (Exception e)
-      {
-         throw new ActionLifecycleException(e);
-      }
-   }
+	/**
+	 * Request a BeanFactory instance from the action. If no Spring IoC
+	 * container exists, create one.
+	 * 
+	 * @return Spring Bean Factory
+	 * @throws ActionLifecycleException
+	 */
+	protected BeanFactory getBeanFactory() throws ActionLifecycleException
+	{
+		return factory;
+	}
+	
+	/**
+	 * Conviencence method for printing a console log header
+	 * @deprecated
+	 */
+	protected void logHeader() { }
 
-   /**
-    * Generic Exception handler for Spring Actions. Displays the root cause
-    * message and full stack trace.
-    * 
-    * @param message
-    * @param exception
-    */
-   public void exceptionHandler(Message message, Throwable exception)
-   {
-      Throwable rootCause = exception.getCause();
-      StackTraceElement[] traceElms = rootCause.getStackTrace();
+	/**
+	 * Conviencence method for printing a console log footer
+	 * @deprecated
+	 */
+	protected void logFooter() { }
 
-      StringBuffer stackTrace = new StringBuffer("Exception Root Cause is: \n");
-      stackTrace.append(rootCause.getMessage());
-      stackTrace.append("\n Full Stack Trace is: \n");
-      for (StackTraceElement elm : traceElms)
-      {
-         stackTrace.append(elm);
-         stackTrace.append("\n");
-      }
 
-      logger.error(stackTrace.toString());
-   }
+	/**
+	 * Create a spring IoC container.
+	 * 
+	 * @throws ActionLifecycleException
+	 */
+	private void loadSpringIoc() throws ActionLifecycleException
+	{
+		try
+		{
+			ApplicationContext springContext;
 
-   /**
-    * Conviencence method for printing a console log header
-    */
-   protected void logHeader()
-   {
-      System.out.println("\n&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
-   }
+			if (springContextXml.contains( "," ))
+			{
+				String[] springContextXmls = springContextXml.split( "," );
+				springContext = new ClassPathXmlApplicationContext( springContextXmls );
+			} 
+			else
+			{
+				springContext = new ClassPathXmlApplicationContext( springContextXml );
+			}
 
-   /**
-    * Conviencence method for printing a console log footer
-    */
-   protected void logFooter()
-   {
-      System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n");
-   }
-
-   /* (non-Javadoc)
-    * @see org.jboss.soa.esb.actions.AbstractActionLifecycle#destroy()
-    */
-   public void destroy() throws ActionLifecycleException
-   {
-   }
+			factory = (BeanFactory) springContext;
+		} 
+		catch (final BeansException e)
+		{
+			throw new ActionLifecycleException( "BeansException caught in loadSpringToc : " , e );
+		}
+	}
+	
 }

Modified: labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/AbstractSpringActionUnitTest.java
===================================================================
--- labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/AbstractSpringActionUnitTest.java	2008-07-11 07:11:10 UTC (rev 21003)
+++ labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/AbstractSpringActionUnitTest.java	2008-07-11 07:29:51 UTC (rev 21004)
@@ -19,55 +19,75 @@
  */
 package org.jboss.soa.esb.actions.spring;
 
-import junit.framework.TestCase;
+import static org.junit.Assert.assertEquals;
+import junit.framework.JUnit4TestAdapter;
 
+import org.jboss.soa.esb.actions.ActionLifecycleException;
 import org.jboss.soa.esb.helpers.ConfigTree;
+import org.junit.Test;
 
 /**
  * Unit tests for org.jboss.soa.esb.actions.AbstractSpringAction
- * 
+ * <p/> 
  * @author <a href="mailto:james.williams at redhat.com">james.williams at redhat.com</a>
+ * @author <a href="mailto:dbevenius at redhat.com">Daniel Bevenius</a>
  */
-public class AbstractSpringActionUnitTest extends TestCase
+public class AbstractSpringActionUnitTest 
 {
+	private final String springContext1 = "/spring-context-1.xml";
+	private final String springContext2 = "/spring-context-2.xml";
+	
+	@Test ( expected = ActionLifecycleException.class )
+    public void nullConfigTree() throws Exception
+    {
+		new MockSpringAction( createConfig() );
+	}
 
-   public void test_bean_factory_load_single() throws Exception
-   {
-      ConfigTree config = new ConfigTree("<config/>");
+	@Test
+    public void test_bean_factory_load_single() throws Exception
+    {
+        MockSpringAction mockAction = createMockAction( new String[] { springContext1 } );
+        assertEquals("hello from Spring", mockAction.sayHelloSpring());
+        assertEquals(false, mockAction.isBeanFactoryNull());
+    }
 
-      config.setAttribute("springContextXml", "/spring-context-1.xml");
+    @Test
+    public void test_bean_factory_load_multiple() throws Exception
+    {
+        MockSpringAction mockAction = createMockAction( new String[] { springContext1, springContext2 } );
+        assertEquals("hello from Spring", mockAction.sayHelloSpring());
+        assertEquals("goodbye from Spring", mockAction.sayGoodbyeSpring());
+        assertEquals(false, mockAction.isBeanFactoryNull());
+    }
 
-      MockSpringAction mockAction = new MockSpringAction(config);
-      assertEquals("hello from Spring", mockAction.sayHelloSpring());
-      assertEquals(false, mockAction.isBeanFactoryNull());
-   }
-
-   public void test_bean_factory_load_multiple() throws Exception
-   {
-      ConfigTree config = new ConfigTree("<config/>");
-
+    @Test
+    public void test_spring_aop() throws Exception
+    {
+        MockSpringAction mockAction = createMockAction( new String[] { springContext1 } );
+        assertEquals("Greeting Changed", mockAction.sayHelloAopStyle());
+        assertEquals(false, mockAction.isBeanFactoryNull());
+    }
+       
+    public static junit.framework.Test suite()
+    {
+        return new JUnit4TestAdapter( AbstractSpringActionUnitTest.class );
+    }
     
-      String configPath1 = "/spring-context-1.xml";
-      String configPath2 = "/spring-context-2.xml";
-
-      config.setAttribute("springContextXml", configPath1 + "," + configPath2);
-
-      MockSpringAction mockAction = new MockSpringAction(config);
-      assertEquals("hello from Spring", mockAction.sayHelloSpring());
-      assertEquals("goodbye from Spring", mockAction.sayGoodbyeSpring());
-      assertEquals(false, mockAction.isBeanFactoryNull());
-   }
-
-   public void test_spring_aop() throws Exception
-   {
-      ConfigTree config = new ConfigTree("<config/>");
-
-      String pathToConfigFile = "/spring-context-1.xml";
-
-      config.setAttribute("springContextXml", pathToConfigFile);
-
-      MockSpringAction mockAction = new MockSpringAction(config);
-      assertEquals("Greeting Changed", mockAction.sayHelloAopStyle());
-      assertEquals(false, mockAction.isBeanFactoryNull());
-   }
+	private ConfigTree createConfig()
+	{
+        return new ConfigTree("<config/>");
+	}
+	
+	private MockSpringAction createMockAction( String[] files) throws Exception
+	{
+        final ConfigTree config = createConfig();
+        final StringBuilder sb = new StringBuilder();
+        for (String configFile : files)
+		{
+        	sb.append( configFile ).append( ',' );
+		}
+        config.setAttribute( "springContextXml" , sb.toString() );
+        
+        return new MockSpringAction( config );
+	}
 }

Modified: labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/MockSpringAction.java
===================================================================
--- labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/MockSpringAction.java	2008-07-11 07:11:10 UTC (rev 21003)
+++ labs/jbossesb/trunk/product/services/spring/src/test/java/org/jboss/soa/esb/actions/spring/MockSpringAction.java	2008-07-11 07:29:51 UTC (rev 21004)
@@ -31,10 +31,10 @@
  */
 public class MockSpringAction extends AbstractSpringAction
 {
-
+	
    public MockSpringAction(ConfigTree config) throws Exception
    {
-      configTree = config;
+	  super(config);
       initialise();
    }
 

Added: labs/jbossesb/trunk/product/services/spring/src/test/resources/log4j.xml
===================================================================
--- labs/jbossesb/trunk/product/services/spring/src/test/resources/log4j.xml	                        (rev 0)
+++ labs/jbossesb/trunk/product/services/spring/src/test/resources/log4j.xml	2008-07-11 07:29:51 UTC (rev 21004)
@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
+
+   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+      <param name="Target" value="System.out"/>
+
+      <layout class="org.apache.log4j.PatternLayout">
+         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%t][%c{1}] %m%n"/>
+      </layout>
+   </appender>
+
+   <!-- ================ -->
+   <!-- Limit categories -->
+   <!-- ================ -->
+
+   <category name="org.jbpm">
+      <priority value="INFO"/>
+   </category>
+   <category name="org.hibernate">
+      <priority value="ERROR"/>
+   </category>
+   <category name="org.jboss">
+      <priority value="WARN"/>
+   </category>
+   
+   <category name="org.springframework">
+      <priority value="ERROR"/>
+   </category>
+   
+   <category name="org.jboss.soa.esb.actions.AbstractSpringAction">
+      <priority value="warn"/>
+   </category>
+   
+   <category name="org.jboss.internal.soa.esb">
+      <priority value="ERROR"/>
+   </category>
+   
+   <category name="org.jboss..soa.esb">
+      <priority value="ERROR"/>
+   </category>
+   
+   <!-- ======================= -->
+   <!-- Setup the Root category -->
+   <!-- ======================= -->
+
+   <root>
+      <appender-ref ref="CONSOLE"/>
+   </root>
+
+</log4j:configuration>




More information about the jboss-svn-commits mailing list