Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Rewire Seam's Contexts static methods for Seam Autowire.
Browse files Browse the repository at this point in the history
Add javadoc to new async packages.
  • Loading branch information
carlosmunoz committed Aug 22, 2013
1 parent 8dc9549 commit b60ceb7
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 119 deletions.
10 changes: 10 additions & 0 deletions zanata-war/src/main/java/org/zanata/async/AsyncTask.java
Expand Up @@ -23,11 +23,21 @@
import java.util.concurrent.Callable;

/**
* Public common interface for all asynchronous tasks in the system.
*
* @param <V> The type of value returned by this task once finished.
* @param <H> The type of task handler provided by the task to keep callers
* informed of progress and/or other task related information.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
public interface AsyncTask<V, H extends AsyncTaskHandle<V>> extends Callable<V>
{

/**
* @return The handle used to keep task information. Tasks must always return the
* same instance of a handle.
*/
H getHandle();

}
Expand Up @@ -28,7 +28,7 @@
import lombok.Setter;

/**
* Asynchronous handle to provide communication between an asynchronous process
* Asynchronous handle to provide communication between an asynchronous task
* and interested clients.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
Expand Down
84 changes: 20 additions & 64 deletions zanata-war/src/main/java/org/zanata/async/AsyncUtils.java
Expand Up @@ -39,6 +39,13 @@ public class AsyncUtils
{
private static final String ASYNC_HANDLE_NAME = "__ASYNC_HANDLE__";

/**
* Outjects an asynchronous task handle. Use {@link AsyncUtils#getAsyncHandle(org.jboss.seam.ScopeType, Class)} or
* {@link AsyncUtils#getEventAsyncHandle(Class)} to retrieve the outjected handle.
*
* @param handle The handle to outject.
* @param scopeType The scope to outject the handle to.
*/
public static final void outject( AsyncTaskHandle<?> handle, ScopeType scopeType)
{
if(scopeType.isContextActive())
Expand All @@ -51,11 +58,24 @@ public static final void outject( AsyncTaskHandle<?> handle, ScopeType scopeType
}
}

/**
* Fetches an asynchronous task handle fro mthe event context.
*
* @param type The expected handle type.
* @return null if no handle is found.
*/
public static final <H extends AsyncTaskHandle> Optional<H> getEventAsyncHandle( Class<H> type )
{
return getAsyncHandle(ScopeType.EVENT, type);
}

/**
* Fetches an asynchronous task handle from a Seam context.
*
* @param scopeType The seam scope to look for the handle.
* @param type The expected handle type.
* @return null if no handle is found.
*/
public static final <H extends AsyncTaskHandle> Optional<H> getAsyncHandle(ScopeType scopeType, Class<H> type)
{
if(scopeType.isContextActive())
Expand All @@ -65,68 +85,4 @@ public static final <H extends AsyncTaskHandle> Optional<H> getAsyncHandle(Scope
return Optional.absent();
}

public static final Set<Context> getCurrentContexts()
{
return Sets.newHashSet(
Contexts.getApplicationContext(),
Contexts.getBusinessProcessContext(),
Contexts.getConversationContext(),
Contexts.getEventContext(),
Contexts.getMethodContext(),
Contexts.getPageContext(),
Contexts.getPageContext(),
Contexts.getSessionContext()
);
}

public static final void restoreAsyncContexts(Collection<Context> toRestore)
{
for( Context ctx : toRestore )
{
if( ctx == null )
{
continue; // Nothing to restore
}

Context destination = null;
switch (ctx.getType())
{
case APPLICATION:
destination = Contexts.getApplicationContext();
break;

case BUSINESS_PROCESS:
destination = Contexts.getBusinessProcessContext();
break;

case CONVERSATION:
destination = Contexts.getConversationContext();
break;

case EVENT:
destination = Contexts.getEventContext();
break;

case METHOD:
destination = Contexts.getMethodContext();
break;

case SESSION:
destination = Contexts.getSessionContext();
break;

default:
break; // Page, Stateless, and Unspecified do not get restored by this utility
}

if( destination != null )
{
for(String name : ctx.getNames())
{
destination.set(name, ctx.get(name));
}
}
}
}

}
Expand Up @@ -36,7 +36,7 @@

/**
* This class executes a Runnable Process asynchronously. Do not use this class directly.
* Use {@link org.zanata.process.ProcessExecutor} instead.
* Use {@link org.zanata.async.TaskExecutor} instead.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
Expand Down
7 changes: 4 additions & 3 deletions zanata-war/src/main/java/org/zanata/async/TaskExecutor.java
Expand Up @@ -30,7 +30,7 @@
import lombok.extern.slf4j.Slf4j;

/**
* This component executes {@link org.zanata.process.RunnableProcess} objects.
* This component executes {@link org.zanata.async.AsyncTask} instances.
*
* @author Carlos Munoz <a href="mailto:camunoz@redhat.com">camunoz@redhat.com</a>
*/
Expand All @@ -44,9 +44,10 @@ public class TaskExecutor
private AsynchronousTaskExecutor asynchronousTaskExecutor;

/**
* Executes a process in the background.
* Executes an asynchronous in the background.
*
* @param handle The handle to be used for the running process.
* @param task The task to execute.
* @return The task handle to keep track of the executed task.
*/
public <V, H extends AsyncTaskHandle<V>> AsyncTaskHandle<V> startTask(AsyncTask<V, H> task)
{
Expand Down
Expand Up @@ -99,7 +99,7 @@ public void startCopyTrans()
CopyTransResource copyTransResource = getClientRequestFactory().createProxy(CopyTransResource.class);

copyTransResource.startCopyTrans("sample-project", "1.0", "my/path/document.txt");
verify(mockIdentity, atLeastOnce()).getSubject();
verify(mockIdentity, atLeastOnce()).checkPermission(eq("copy-trans"), anyVararg());
}

@Test
Expand All @@ -108,7 +108,6 @@ public void startCopyTransAndCheckStatus()
CopyTransResource copyTransResource = getClientRequestFactory().createProxy(CopyTransResource.class);

copyTransResource.startCopyTrans("sample-project", "1.0", "my/path/document.txt");
verify(mockIdentity, atLeastOnce()).getSubject();

CopyTransStatus status = copyTransResource.getCopyTransStatus("sample-project", "1.0", "my/path/document.txt");
assertThat(status, notNullValue());
Expand Down
38 changes: 0 additions & 38 deletions zanata-war/src/test/java/org/zanata/seam/AutowireIdentity.java

This file was deleted.

18 changes: 8 additions & 10 deletions zanata-war/src/test/java/org/zanata/seam/SeamAutowire.java
Expand Up @@ -76,7 +76,7 @@ public class SeamAutowire
{
rewireSeamComponentClass();
rewireSeamTransactionClass();
rewireSeamIdentityClass();
rewireSeamContextsClass();
}

protected SeamAutowire()
Expand Down Expand Up @@ -342,28 +342,26 @@ else if( accessor.getAnnotation(Logger.class) != null )
return component;
}

private static void rewireSeamIdentityClass()
private static void rewireSeamContextsClass()
{
try
{
ClassPool pool = ClassPool.getDefault();
CtClass instanceCls = pool.get("org.jboss.seam.security.Identity");
CtClass contextsCls = pool.get("org.jboss.seam.contexts.Contexts");

// Replace Component's method bodies with the ones in AutowireComponent
CtMethod methodToReplace = instanceCls.getDeclaredMethod("instance");
methodToReplace.setBody(
pool.get(AutowireIdentity.class.getName()).getDeclaredMethod("instance"),
null);
CtMethod methodToReplace = contextsCls.getDeclaredMethod("isSessionContextActive");
methodToReplace.setBody("return true;");

instanceCls.toClass();
contextsCls.toClass();
}
catch (NotFoundException e)
{
throw new RuntimeException("Problem rewiring Seam's Component class", e);
throw new RuntimeException("Problem rewiring Seam's Contexts class", e);
}
catch (CannotCompileException e)
{
throw new RuntimeException("Problem rewiring Seam's Component class", e);
throw new RuntimeException("Problem rewiring Seam's Contexts class", e);
}

}
Expand Down

0 comments on commit b60ceb7

Please sign in to comment.