[jboss-cvs] JBossAS SVN: r72288 - in projects/jboss-aspects/trunk/current-invocation/src: test and 3 other directories.
jboss-cvs-commits at lists.jboss.org
jboss-cvs-commits at lists.jboss.org
Wed Apr 16 11:18:33 EDT 2008
Author: ALRubinger
Date: 2008-04-16 11:18:33 -0400 (Wed, 16 Apr 2008)
New Revision: 72288
Added:
projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocation.java
projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocationInterceptor.java
projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/PojoBean.java
projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/TestException.java
projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/unit/
projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/unit/CurrentInvocationUnitTestCase.java
projects/jboss-aspects/trunk/current-invocation/src/test/resources/
projects/jboss-aspects/trunk/current-invocation/src/test/resources/jboss-aop.xml
Log:
[JBASPECT-1] Added Implementation and Test Cases
Added: projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocation.java
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocation.java (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocation.java 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,115 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.aspects.currentinvocation;
+
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.logging.Logger;
+
+/**
+ * CurrentInvocation
+ *
+ * Holds the Invocation associated with the Current Thread for
+ * access from any class within the associated Thread's context
+ * (scope). Internally contains a central registry backed by
+ * a ThreadLocal to contain the Invocation.
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CurrentInvocation
+{
+
+ // -------------------------------------------------------------------------------------------------||
+ // Class Members -----------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ /*
+ * JBoss Logger
+ */
+ private static final Logger logger = Logger.getLogger(CurrentInvocation.class);
+
+ // -------------------------------------------------------------------------------------------------||
+ // Instance Members --------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ /*
+ * Registry for the current Invocation
+ */
+ private static final ThreadLocal<Invocation> registry = new ThreadLocal<Invocation>()
+ {
+ @Override
+ protected Invocation initialValue()
+ {
+ return null;
+ }
+ };
+
+ // -------------------------------------------------------------------------------------------------||
+ // Functional Methods ------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ /**
+ * Retrieves the Invocation associated with the current
+ * Thread / request if one is associated. Returns null
+ * otherwise
+ *
+ * @return
+ */
+ public static Invocation getCurrentInvocation()
+ {
+ return CurrentInvocation.getRegistry().get();
+ }
+
+ /**
+ * Clears the current Thread / request association
+ * with an Invocation, if one exists
+ */
+ static void clear()
+ {
+ // Log
+ logger.trace("Clearing Thread " + Thread.currentThread().toString() + " of association with Invocation");
+ CurrentInvocation.getRegistry().remove();
+ }
+
+ /**
+ * Associates the specified invocation with the current
+ * Thread / request
+ *
+ * @param invocation
+ */
+ static void setCurrentInvocation(Invocation invocation)
+ {
+ // Log
+ logger.trace("Placing invocation " + invocation + " into scope for Thread " + Thread.currentThread().toString());
+ CurrentInvocation.getRegistry().set(invocation);
+ }
+
+ // -------------------------------------------------------------------------------------------------||
+ // Accessors ---------------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private static ThreadLocal<Invocation> getRegistry()
+ {
+ return CurrentInvocation.registry;
+ }
+
+}
Added: projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocationInterceptor.java
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocationInterceptor.java (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/main/java/org/jboss/aspects/currentinvocation/CurrentInvocationInterceptor.java 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,75 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.aspects.currentinvocation;
+
+import java.io.Serializable;
+
+import org.jboss.aop.advice.Interceptor;
+import org.jboss.aop.joinpoint.Invocation;
+
+/**
+ * CurrentInvocationInterceptor
+ *
+ * Associates the current invocation with the running
+ * Thread for retrieval by any component within
+ * the same request context. Clears the association
+ * at the end of the Interceptor chain for safe return to
+ * a potential Thread pool.
+ *
+ * @see CurrentInvocation
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CurrentInvocationInterceptor implements Interceptor, Serializable
+{
+ // -------------------------------------------------------------------------------------------------||
+ // Class Members -----------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private static final long serialVersionUID = 1L;
+
+ // -------------------------------------------------------------------------------------------------||
+ // Required Implementations ------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ public String getName()
+ {
+ return CurrentInvocationInterceptor.class.getSimpleName();
+ }
+
+ public Object invoke(Invocation invocation) throws Throwable
+ {
+ try
+ {
+ // Place the Invocation in Request scope
+ CurrentInvocation.setCurrentInvocation(invocation);
+
+ // Carry on
+ return invocation.invokeNext();
+ }
+ finally
+ {
+ // Clean up for return to Thread Pool
+ CurrentInvocation.clear();
+ }
+ }
+}
Added: projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/PojoBean.java
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/PojoBean.java (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/PojoBean.java 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,95 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.aspects.test.currentinvocation;
+
+import org.jboss.aop.joinpoint.Invocation;
+import org.jboss.aspects.currentinvocation.CurrentInvocation;
+
+/**
+ * PojoBean
+ *
+ * A Simple POJO to act as target for Current Invocation
+ * Interception for use in testing
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class PojoBean
+{
+ /**
+ * Returns whether or not the current invocation can be
+ * obtained from the registry, validating that the Interceptor
+ * worked as expected
+ * @return
+ */
+ public boolean canGetCurrentInvocation()
+ {
+ Invocation invocation = CurrentInvocation.getCurrentInvocation();
+ return invocation != null;
+ }
+
+ /**
+ * Throw an exception
+ */
+ public void throwException() throws TestException
+ {
+ // Ensure current invocation is available
+ this.ensureCurrentInvocationIsAvailable();
+
+ // Throw the expected TestException
+ throw new TestException("ALR : Carlo :: Einstein : Paris Hilton");
+ }
+
+ /**
+ * Define a bind for interception only, and
+ * wait until notified by another Thread
+ *
+ * @param millis
+ */
+ public void suspend()
+ {
+ // Ensure current invocation is available
+ this.ensureCurrentInvocationIsAvailable();
+
+ try
+ {
+ wait();
+ }
+ catch (InterruptedException e)
+ {
+ throw new RuntimeException(e);
+ }
+
+ }
+
+ /**
+ * Throw a RuntimeException if no Current Invocation
+ * is in scope
+ */
+ private void ensureCurrentInvocationIsAvailable()
+ {
+ if (CurrentInvocation.getCurrentInvocation() == null)
+ {
+ throw new RuntimeException("Current Invocation not found");
+ }
+ }
+}
Added: projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/TestException.java
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/TestException.java (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/TestException.java 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,41 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.aspects.test.currentinvocation;
+
+/**
+ * TestException
+ *
+ * An Exception used for Testing
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class TestException extends Exception
+{
+ private static final long serialVersionUID = 1L;
+
+ public TestException(String message)
+ {
+ super(message);
+ }
+
+}
Added: projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/unit/CurrentInvocationUnitTestCase.java
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/unit/CurrentInvocationUnitTestCase.java (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/test/java/org/jboss/aspects/test/currentinvocation/unit/CurrentInvocationUnitTestCase.java 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,191 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.aspects.test.currentinvocation.unit;
+
+import junit.framework.TestCase;
+
+import org.jboss.aspects.common.AOPDeployer;
+import org.jboss.aspects.currentinvocation.CurrentInvocation;
+import org.jboss.aspects.test.currentinvocation.PojoBean;
+import org.jboss.aspects.test.currentinvocation.TestException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * CurrentInvocationUnitTestCase
+ *
+ * Test Cases to ensure the CurrentInvocation mechanism
+ * is working correctly
+ *
+ * @author <a href="mailto:andrew.rubinger at jboss.org">ALR</a>
+ * @version $Revision: $
+ */
+public class CurrentInvocationUnitTestCase
+{
+
+ // -------------------------------------------------------------------------------------------------||
+ // Class Members -----------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private static final String DD_AOP_INCONTEXT = "jboss-aop.xml";
+
+ // -------------------------------------------------------------------------------------------------||
+ // Instance Members --------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private AOPDeployer deployer = new AOPDeployer(CurrentInvocationUnitTestCase.DD_AOP_INCONTEXT);
+
+ // -------------------------------------------------------------------------------------------------||
+ // Tests -------------------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ /**
+ * Ensures that the current invocation
+ * can be obtained from within proper context
+ */
+ @Test
+ public void testCurrentInvocationInContext()
+ {
+ // Ensure current invocation is accessible from within context
+ TestCase.assertEquals("Current Invocation for this Thread was null", true, new PojoBean()
+ .canGetCurrentInvocation());
+ // Ensure context is cleansed after completion
+ TestCase.assertEquals("Current Invocation was not cleansed after interception completed", null, CurrentInvocation
+ .getCurrentInvocation());
+ }
+
+ /**
+ * Ensures that the Current Invocation is cleaned up
+ * even in the case an Exception is thrown
+ */
+ @Test
+ public void testCleanupAfterException()
+ {
+ try
+ {
+ new PojoBean().throwException();
+ }
+ catch (TestException e)
+ {
+ TestCase.assertEquals("Current Invocation was not cleansed after exception was thrown", null,
+ CurrentInvocation.getCurrentInvocation());
+ }
+ }
+
+ /**
+ * Ensures that a Thread running concurrently
+ * alongside an invocation cannot access
+ * the current invocation outside its own scope
+ */
+ @Test
+ public void testConcurrentAccessFromSeparateThreadFails()
+ {
+ // Initialize
+ long waitForLaunch = 750L;
+
+ // Make a new PojoBean
+ PojoBean bean = new PojoBean();
+
+ // Start a new job for Pojo to invoke and hold
+ Thread otherThread = new RunPojoBeanSuspend(bean);
+ otherThread.start();
+
+ // Let the process start
+ try
+ {
+ Thread.sleep(waitForLaunch);
+ }
+ catch (InterruptedException e)
+ {
+ throw new RuntimeException(e);
+ }
+
+ // Ensure we can't get the current invocation that's in
+ // the other Thread's scope
+ TestCase.assertEquals("Scoping of CurrentInvocation is not limited per Thread", null, CurrentInvocation
+ .getCurrentInvocation());
+
+ // Let the other Thread complete
+ synchronized(bean){
+ bean.notify();
+ }
+
+ // Wait for other thread to die
+ try
+ {
+ otherThread.join();
+ }
+ catch (InterruptedException e)
+ {
+ throw new RuntimeException(e);
+ }
+ }
+
+ // -------------------------------------------------------------------------------------------------||
+ // Helper Classes ---------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private static class RunPojoBeanSuspend extends Thread
+ {
+
+ PojoBean bean;
+
+ public RunPojoBeanSuspend(PojoBean bean)
+ {
+ this.bean = bean;
+ }
+
+ @Override
+ public void run()
+ {
+ super.run();
+ this.bean.suspend();
+ }
+ }
+
+ // -------------------------------------------------------------------------------------------------||
+ // Lifecycle ---------------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ @Before
+ public void setup() throws Exception
+ {
+ this.getDeployer().deploy();
+ }
+
+ @After
+ public void tearDown() throws Exception
+ {
+ this.getDeployer().undeploy();
+ }
+
+ // -------------------------------------------------------------------------------------------------||
+ // Accessors ---------------------------------------------------------------------------------------||
+ // -------------------------------------------------------------------------------------------------||
+
+ private AOPDeployer getDeployer()
+ {
+ return deployer;
+ }
+
+}
Added: projects/jboss-aspects/trunk/current-invocation/src/test/resources/jboss-aop.xml
===================================================================
--- projects/jboss-aspects/trunk/current-invocation/src/test/resources/jboss-aop.xml (rev 0)
+++ projects/jboss-aspects/trunk/current-invocation/src/test/resources/jboss-aop.xml 2008-04-16 15:18:33 UTC (rev 72288)
@@ -0,0 +1,15 @@
+<aop>
+
+ <bind pointcut="execution(public boolean org.jboss.aspects.test.currentinvocation.PojoBean->canGetCurrentInvocation())">
+ <interceptor class="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor"/>
+ </bind>
+
+ <bind pointcut="execution(public void org.jboss.aspects.test.currentinvocation.PojoBean->throwException())">
+ <interceptor class="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor"/>
+ </bind>
+
+ <bind pointcut="execution(public void org.jboss.aspects.test.currentinvocation.PojoBean->suspend())">
+ <interceptor class="org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor"/>
+ </bind>
+
+</aop>
\ No newline at end of file
More information about the jboss-cvs-commits
mailing list