Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cdi/interceptors-priority/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.javaee7.cdi</groupId>
<artifactId>cdi-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>interceptors-prioriry</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.javaee7.cdi.interceptors.priority;

/**
* @author Radim Hanus
*/
public interface Greeting {
public String getGreet();
public void setGreet(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.javaee7.cdi.interceptors.priority;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

/**
* Interceptors with smaller priority values are called first.
*
* @author Radim Hanus
*/
@Interceptor
@MyInterceptorBinding
@Priority(Interceptor.Priority.APPLICATION + 100)
public class HighPriorityInterceptor {
@AroundInvoke
public Object log(InvocationContext context) throws Exception {
Object[] parameters = context.getParameters();
if (parameters.length > 0 && parameters[0] instanceof String) {
String param = (String) parameters[0];
parameters[0] = "Hi " + param + " !";
context.setParameters(parameters);
}
return context.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.javaee7.cdi.interceptors.priority;

import javax.annotation.Priority;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;

/**
* Interceptors with smaller priority values are called first.
*
* @author Radim Hanus
*/
@Interceptor
@MyInterceptorBinding
@Priority(Interceptor.Priority.APPLICATION + 200)
public class LowPriorityInterceptor {
@AroundInvoke
public Object log(InvocationContext context) throws Exception {
Object[] parameters = context.getParameters();
if (parameters.length > 0 && parameters[0] instanceof String) {
String param = (String) parameters[0];
parameters[0] = param + " Nice to meet you.";
context.setParameters(parameters);
}
return context.proceed();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.javaee7.cdi.interceptors.priority;

import javax.interceptor.InterceptorBinding;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
* @author Arun Gupta
*/
@Inherited
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface MyInterceptorBinding {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.javaee7.cdi.interceptors.priority;

/**
* @author Radim Hanus
*/
@MyInterceptorBinding
public class SimpleGreeting implements Greeting {
private String greet;

public String getGreet() {
return greet;
}

public void setGreet(String greet) {
this.greet = greet;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.javaee7.cdi.interceptors.priority;

import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
* Note that beans.xml doesn't define any interceptor. Interceptors declared using interceptor bindings
* are enabled for the entire application and ordered using the Priority annotation.
*
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class GreetingTest {
@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, SimpleGreeting.class, MyInterceptorBinding.class, LowPriorityInterceptor.class, HighPriorityInterceptor.class)
.addAsManifestResource("beans.xml");
}

@Inject
Greeting bean;

@Test
public void test() throws Exception {
assertThat(bean, is(notNullValue()));
assertThat(bean, instanceOf(SimpleGreeting.class));

bean.setGreet("Arun");
assertEquals(bean.getGreet(), "Hi Arun ! Nice to meet you.");
}
}
16 changes: 16 additions & 0 deletions cdi/interceptors-priority/src/test/resources/arquillian.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>

<arquillian xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://jboss.org/schema/arquillian"
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">

<defaultProtocol type="Servlet 3.0"/>

<container qualifier="test" default="true">
<configuration>
<property name="jbossHome">${serverRoot:target/wildfly-8.0.0.CR1}</property>
<property name="serverConfig">${serverProfile:standalone-full.xml}</property>
</configuration>
</container>

</arquillian>
8 changes: 8 additions & 0 deletions cdi/interceptors-priority/src/test/resources/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">

</beans>
1 change: 1 addition & 0 deletions cdi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<module>exclude-filter</module>
<module>built-in</module>
<module>interceptors</module>
<module>interceptors-prioriry</module>
<module>nobeans-xml</module>
<module>beansxml-noversion</module>
<module>beanmanager</module>
Expand Down