Skip to content

Commit

Permalink
Create a separate InterceptorContext class to avoid various encapsula…
Browse files Browse the repository at this point in the history
…tion issues with InvocationContext
  • Loading branch information
dmlloyd committed Feb 4, 2011
1 parent e5e1a87 commit c5a91e9
Show file tree
Hide file tree
Showing 11 changed files with 369 additions and 152 deletions.
34 changes: 13 additions & 21 deletions src/main/java/org/jboss/invocation/ChainedInterceptor.java
Expand Up @@ -23,8 +23,9 @@
package org.jboss.invocation;

import java.io.Serializable;

import javax.interceptor.InvocationContext;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

import static org.jboss.invocation.InvocationMessages.msg;

Expand All @@ -38,7 +39,7 @@ class ChainedInterceptor implements Interceptor, Serializable {

private static final long serialVersionUID = 7951017996430287249L;

private final Interceptor[] interceptors;
private final List<Interceptor> interceptors;

/**
* Construct a new instance.
Expand All @@ -49,26 +50,17 @@ class ChainedInterceptor implements Interceptor, Serializable {
if (interceptors == null) {
throw msg.nullParameter("interceptors");
}
this.interceptors = interceptors;
this.interceptors = Arrays.asList(interceptors);
}

/** {@inheritDoc} */
public Object processInvocation(final InvocationContext context) throws Exception {
final InvocationContext childContext = new DelegatingInvocationContext(context) {
private int index = 0;

public Object proceed() throws Exception {
if (index < interceptors.length) {
try {
return interceptors[index++].processInvocation(this);
} finally {
index--;
}
} else {
return super.proceed();
}
}
};
return childContext.proceed();
public Object processInvocation(final InterceptorContext context) throws Exception {
final ListIterator<Interceptor> old = context.getInterceptorIterator();
context.setInterceptorIterator(new ConcatenatedIterator<Interceptor>(interceptors.listIterator(), old));
try {
return context.proceed();
} finally {
context.setInterceptorIterator(old);
}
}
}
77 changes: 77 additions & 0 deletions src/main/java/org/jboss/invocation/ConcatenatedIterator.java
@@ -0,0 +1,77 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2011, Red Hat, Inc., 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.invocation;

import java.util.ListIterator;

/**
* A concatenated iterator.
*
* @param <T> the element type
* @author <a href="mailto:david.lloyd@redhat.com">David M. Lloyd</a>
*/
class ConcatenatedIterator<T> implements ListIterator<T> {
private final ListIterator<T> first;
private final ListIterator<T> second;

ConcatenatedIterator(final ListIterator<T> first, final ListIterator<T> second) {
this.first = first;
this.second = second;
}

public boolean hasNext() {
return first.hasNext() || second.hasNext();
}

public T next() {
return first.hasNext() ? first.next() : second.next();
}

public boolean hasPrevious() {
return second.hasPrevious() || first.hasPrevious();
}

public T previous() {
return second.hasPrevious() ? second.previous() : first.previous();
}

public int nextIndex() {
throw new UnsupportedOperationException();
}

public int previousIndex() {
throw new UnsupportedOperationException();
}

public void remove() {
throw new UnsupportedOperationException();
}

public void set(final T t) {
throw new UnsupportedOperationException();
}

public void add(final T t) {
throw new UnsupportedOperationException();
}
}
Expand Up @@ -28,8 +28,6 @@
import java.security.AccessController;
import java.security.PrivilegedAction;

import javax.interceptor.InvocationContext;

/**
* An interceptor which sets the thread context class loader for the duration of an invocation.
* <p>
Expand Down Expand Up @@ -58,7 +56,7 @@ public ContextClassLoaderInterceptor(final ClassLoader classLoader) {
}

/** {@inheritDoc} */
public Object processInvocation(final InvocationContext context) throws Exception {
public Object processInvocation(final InterceptorContext context) throws Exception {
final Thread thread = Thread.currentThread();
final ClassLoader old;
final SecurityManager sm = System.getSecurityManager();
Expand Down
115 changes: 18 additions & 97 deletions src/main/java/org/jboss/invocation/InVMRemoteInterceptor.java
Expand Up @@ -22,7 +22,6 @@

package org.jboss.invocation;

import java.lang.reflect.Method;
import java.security.AccessController;
import java.security.PrivilegedAction;
import org.jboss.marshalling.cloner.ClassLoaderClassCloner;
Expand All @@ -31,8 +30,6 @@
import org.jboss.marshalling.cloner.ObjectClonerFactory;
import org.jboss.marshalling.cloner.ObjectCloners;

import javax.interceptor.InvocationContext;

/**
* An invocation processor which passes the invocation (possibly by value) to a target class loader. Invocations will be
* cloned to the target class loader; replies will be cloned to the caller's thread context class loader. The target
Expand All @@ -49,99 +46,41 @@ public ClassLoader run() {
}
};

private final Interceptor targetInterceptor;
private final Method targetMethod;
private final ClassLoaderClassCloner classCloner;
private final PassMode passMode;
private final Object targetInstance;
private final ClonerConfiguration configuration;

/**
* Construct a new instance.
*
* @param targetInterceptor the target interceptor
* @param targetMethod the target method
* @param targetClassLoader the target class loader
* @param passMode the parameter pass mode
* @param targetInstance the target invocation instance
*
*/
public InVMRemoteInterceptor(final Interceptor targetInterceptor, final Method targetMethod, final ClassLoader targetClassLoader, final PassMode passMode, final Object targetInstance) {
this.targetInterceptor = targetInterceptor;
this.targetMethod = targetMethod;
this.passMode = passMode;
this.targetInstance = targetInstance;
public InVMRemoteInterceptor(final ClassLoader targetClassLoader) {
configuration = new ClonerConfiguration();
configuration.setClassCloner(classCloner = new ClassLoaderClassCloner(targetClassLoader));
configuration.setClassCloner(new ClassLoaderClassCloner(targetClassLoader));
}

/** {@inheritDoc} */
public Object processInvocation(final InvocationContext context) throws Exception {
public Object processInvocation(final InterceptorContext context) throws Exception {
final Object[] parameters = context.getParameters();
final ObjectClonerFactory clonerFactory = ObjectCloners.getSerializingObjectClonerFactory();
final ObjectCloner cloner = clonerFactory.createCloner(configuration);
final Object[] newParameters;
switch (passMode) {
case REFERENCE_ONLY: {
newParameters = parameters;
break;
}
case SAME_CLASS_LOADER: {
newParameters = parameters.clone();
final int len = newParameters.length;
for (int i = 0; i < len; i++) {
final Object param = parameters[i];
if (param != null) {
final Class<?> origClass = param.getClass();
final Class<?> newClass = classCloner.clone(origClass);
if (newClass != origClass) {
newParameters[i] = cloner.clone(param);
}
}
}
break;
}
case VALUE_ONLY: {
final int len = parameters.length;
newParameters = new Object[len];
for (int i = 0; i < len; i++) {
newParameters[i] = cloner.clone(parameters[i]);
}
break;
}
default: {
// not reachable
throw new IllegalStateException();
}
final int len = parameters.length;
newParameters = new Object[len];
for (int i = 0; i < len; i++) {
newParameters[i] = cloner.clone(parameters[i]);
}
final InvocationContext newContext = new SimpleInvocationContext(targetInstance, targetMethod, newParameters, context.getContextData(), null);
final Object result = targetInterceptor.processInvocation(newContext);
switch (passMode) {
case REFERENCE_ONLY: {
return result;
}
case SAME_CLASS_LOADER: {
if (result == null) {
return null;
}
final ClassLoaderClassCloner classCloner = new ClassLoaderClassCloner(getContextClassLoader());
final Class<?> classClone = classCloner.clone(result.getClass());
if (classClone == result.getClass()) {
return result;
}
// fall through
}
case VALUE_ONLY: {
if (result == null) {
return null;
}
final ClonerConfiguration copyBackConfiguration = new ClonerConfiguration();
copyBackConfiguration.setClassCloner(new ClassLoaderClassCloner(getContextClassLoader()));
return clonerFactory.createCloner(copyBackConfiguration).clone(result);
}
default: {
// not reachable
throw new IllegalStateException();
context.setParameters(newParameters);
try {
final Object result = context.proceed();
if (result == null) {
return null;
}
final ClonerConfiguration copyBackConfiguration = new ClonerConfiguration();
copyBackConfiguration.setClassCloner(new ClassLoaderClassCloner(getContextClassLoader()));
return clonerFactory.createCloner(copyBackConfiguration).clone(result);
} finally {
context.setParameters(parameters);
}
}

Expand All @@ -153,22 +92,4 @@ private ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
}

/**
* The mode to use for parameter and result passing to the target.
*/
public enum PassMode {
/**
* Pass all items by reference always.
*/
REFERENCE_ONLY,
/**
* Pass items by reference which are of the same type and class loader.
*/
SAME_CLASS_LOADER,
/**
* Pass all items by value always.
*/
VALUE_ONLY,
}
}
8 changes: 3 additions & 5 deletions src/main/java/org/jboss/invocation/Interceptor.java
Expand Up @@ -22,8 +22,6 @@

package org.jboss.invocation;

import javax.interceptor.InvocationContext;

/**
* A processor for invocations. May perform some action, including but not limited to handling the invocation, before
* or in lieu of passing it on to the dispatcher or another processor.
Expand All @@ -35,11 +33,11 @@ public interface Interceptor {

/**
* Process an invocation. The invocation can be handled directly, or passed on to the next processor in the
* chain via {@code context}.
* chain.
*
* @param context the invocation context
* @param context the interceptor context
* @return the result of the invocation
* @throws Exception If the underlying invocation resulted in some exception
*/
Object processInvocation(InvocationContext context) throws Exception;
Object processInvocation(InterceptorContext context) throws Exception;
}

0 comments on commit c5a91e9

Please sign in to comment.