Skip to content

Commit

Permalink
ChainedInterceptor was actually weaving interceptors
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfc authored and dmlloyd committed Feb 16, 2011
1 parent 5fa899d commit 5781edf
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/org/jboss/invocation/ChainedInterceptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class ChainedInterceptor implements Interceptor, Serializable {
/** {@inheritDoc} */
public Object processInvocation(final InterceptorContext context) throws Exception {
final ListIterator<Interceptor> old = context.getInterceptorIterator();
context.setInterceptorIterator(new ConcatenatedIterator<Interceptor>(interceptors.listIterator(), old));
context.setInterceptorIterator(interceptors.listIterator());
try {
return context.proceed();
} finally {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/jboss/invocation/Interceptors.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ public static InterceptorFactory getChainedInterceptorFactory(Collection<Interce
return new ChainedInterceptorFactory(instances.toArray(new InterceptorFactory[instances.size()]));
}

public static Interceptor getWeavedInterceptor(final Interceptor... interceptors) {
return new WeavedInterceptor(interceptors);
}

/**
* Convenience method to get a {@link Throwable} as an {@link Exception}.
*
Expand Down
53 changes: 53 additions & 0 deletions src/main/java/org/jboss/invocation/WeavedInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 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.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;

/**
* Weaves a series of interceptors into an existing interceptor chain.
*
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
class WeavedInterceptor implements Interceptor, Serializable {
private final List<Interceptor> interceptors;

WeavedInterceptor(final Interceptor... interceptors) {
this.interceptors = Arrays.asList(interceptors);
}

@Override
public Object processInvocation(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);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 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.test;

import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorContext;
import org.jboss.invocation.Interceptors;
import org.junit.Test;

import java.lang.reflect.Method;

import static org.jboss.invocation.test.MyInterceptor.createMyInterceptor;
import static org.junit.Assert.assertEquals;

/**
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
public class ChainedInterceptorTestCase {
public String echo(final String msg) {
return "Echo " + msg;
}

@Test
public void test1() throws Exception {
Method method = ChainedInterceptorTestCase.class.getMethod("echo", String.class);
InterceptorContext context = new InterceptorContext();
context.setMethod(method);
context.setTarget(this);
context.setParameters(new Object[] { "test1" });

Interceptor interceptor1 = Interceptors.getChainedInterceptor(createMyInterceptor("1"), createMyInterceptor("2"), Interceptors.getInvokingInterceptor());
Interceptor interceptor2 = Interceptors.getChainedInterceptor(createMyInterceptor("3"), createMyInterceptor("4"), interceptor1);

String result = (String) interceptor2.processInvocation(context);
String expected = "3#4#1#2#Echo test1";
assertEquals(expected, result);
}
}
47 changes: 47 additions & 0 deletions src/test/java/org/jboss/invocation/test/MyInterceptor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 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.test;

import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorContext;

/**
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
public abstract class MyInterceptor implements Interceptor {
protected abstract String getName();

public static Interceptor createMyInterceptor(final String name) {
return new MyInterceptor() {
@Override
protected String getName() {
return name;
}
};
}

@Override
public Object processInvocation(final InterceptorContext context) throws Exception {
return getName() + "#" + context.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright (c) 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.test;

import org.jboss.invocation.Interceptor;
import org.jboss.invocation.InterceptorContext;
import org.jboss.invocation.Interceptors;
import org.junit.Test;

import java.lang.reflect.Method;

import static org.jboss.invocation.test.MyInterceptor.createMyInterceptor;
import static org.junit.Assert.assertEquals;

/**
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
public class WeavedInterceptorTestCase {
public String echo(final String msg) {
return "Echo " + msg;
}

@Test
public void test1() throws Exception {
Method method = WeavedInterceptorTestCase.class.getMethod("echo", String.class);
InterceptorContext context = new InterceptorContext();
context.setMethod(method);
context.setTarget(this);
context.setParameters(new Object[] { "test1" });

Interceptor interceptor2 = Interceptors.getWeavedInterceptor(createMyInterceptor("3"), createMyInterceptor("4"));
Interceptor interceptor1 = Interceptors.getChainedInterceptor(interceptor2, createMyInterceptor("1"), createMyInterceptor("2"), Interceptors.getInvokingInterceptor());

String result = (String) interceptor1.processInvocation(context);
String expected = "3#4#1#2#Echo test1";
assertEquals(expected, result);
}
}

0 comments on commit 5781edf

Please sign in to comment.