/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, 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.ejb3.async.impl.interceptor;
import java.io.Serializable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.ejb.EJBException;
import org.jboss.aop.Advisor;
import org.jboss.aop.advice.Interceptor;
import org.jboss.aop.joinpoint.Invocation;
import org.jboss.ejb3.EJBContainer;
import org.jboss.ejb3.async.impl.SerializableFuture;
import org.jboss.ejb3.async.spi.AsyncInvocationId;
import org.jboss.ejb3.async.spi.AsyncInvocationMap;
import org.jboss.ejb3.mdb.MessagingContainer;
import org.jboss.ejb3.service.ServiceContainer;
import org.jboss.ejb3.session.SessionSpecContainer;
import org.jboss.logging.Logger;
/**
* Ensures the {@link Future} implementation provided by a bean provider
* is capable of being serialized; if not, provides this mechanism.
* Also manages a backing reference to the currently queued/running
* asynchronous operations such that they may be cancelled.
*
* @author <a href="mailto:andrew.rubinger@jboss.org">ALR</a>
* @version $Revision: $
*/
public class AsynchronousServerInterceptor implements Interceptor, Serializable
{
// --------------------------------------------------------------------------------||
// Class Members ------------------------------------------------------------------||
// --------------------------------------------------------------------------------||
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
/**
* Logger
*/
private static final Logger log = Logger.getLogger(AsynchronousServerInterceptor.class);
// --------------------------------------------------------------------------------||
// Constructor --------------------------------------------------------------------||
// --------------------------------------------------------------------------------||
/**
* No-arg constructor required
*/
public AsynchronousServerInterceptor()
{
log.debug("Created: " + this);
}
// --------------------------------------------------------------------------------||
// Required Implementations -------------------------------------------------------||
// --------------------------------------------------------------------------------||
/**
* {@inheritDoc}
* @see org.jboss.aop.advice.Interceptor#getName()
*/
public String getName()
{
return this.getClass().getSimpleName();
}
/**
* {@inheritDocs}
* @see org.jboss.aop.advice.Interceptor#invoke(org.jboss.aop.joinpoint.Invocation)
*/
public Object invoke(final Invocation invocation) throws Throwable
{
// Get the current invocation ID
final AsyncInvocationId id = CurrentAsyncInvocation.getCurrentAsyncInvocationId(invocation);
// Get at the current invocations via the container
final Advisor advisor = invocation.getAdvisor();
final EJBContainer ejbContainer = EJBContainer.getEJBContainer(advisor);
if (ejbContainer instanceof ServiceContainer || ejbContainer instanceof MessagingContainer)
{
return invocation.invokeNext();
}
final SessionSpecContainer sessionContainer = (SessionSpecContainer) EJBContainer.getEJBContainer(advisor);
final AsyncInvocationMap currentInvocations = sessionContainer.getCurrentAsyncInvocations();
// Put the current ID into the Map
if (id != null)
{
currentInvocations.put(id, false);
}
// Mark the Thread
CurrentAsyncInvocation.markCurrentInvocationOnThread(id);
// Now invoke
Object returnValue;
try
{
returnValue = invocation.invokeNext();
// If this is a Future
if (returnValue instanceof Future)
{
// Cast
final Future<?> future = (Future<?>) returnValue;
// If not Serializable
if (!(returnValue instanceof Serializable))
{
// Make it so
returnValue = new SerializableFuture<Object>(future.get());
}
}
return returnValue;
}
finally
{
// Remove the invocation from the Map of those currently-executed
if (id != null)
{
currentInvocations.remove(id);
}
// Unmark the Thread
CurrentAsyncInvocation.unmarkCurrentInvocationFromThread();
}
}
private static class ExceptionFuture<V> implements Future<V>, Serializable
{
/**
* serialVersionUID
*/
private static final long serialVersionUID = 1L;
private final EJBException exception;
public ExceptionFuture(final Exception exception)
{
assert exception != null : "Exception must be specified";
this.exception = new EJBException(exception);
}
@Override
public boolean cancel(boolean mayInterruptIfRunning)
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isCancelled()
{
// TODO Auto-generated method stub
return false;
}
@Override
public boolean isDone()
{
// TODO Auto-generated method stub
return true;
}
@Override
public V get() throws InterruptedException, ExecutionException
{
throw new ExecutionException(exception);
}
@Override
public V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
{
throw new ExecutionException(exception);
}
}
}