Skip to content

Commit c328c35

Browse files
committed
Added sample for interceptor factor - prog. adding interceptor
1 parent f2cc2cf commit c328c35

File tree

14 files changed

+173
-9
lines changed

14 files changed

+173
-9
lines changed

cdi/events-priority/pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
3-
<modelVersion>4.0.0</modelVersion>
2+
<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>
3+
44
<parent>
55
<groupId>org.javaee8.cdi</groupId>
66
<artifactId>cdi-samples</artifactId>
77
<version>1.0-SNAPSHOT</version>
8-
<relativePath>../pom.xml</relativePath>
98
</parent>
109

1110
<artifactId>events-priority</artifactId>
11+
<name>Java EE 8 Samples: CDI - Events Priority</name>
1212
</project>

cdi/interception-factory/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<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>
3+
4+
<parent>
5+
<groupId>org.javaee8.cdi</groupId>
6+
<artifactId>cdi-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
</parent>
9+
10+
<artifactId>interception-factory</artifactId>
11+
<name>Java EE 8 Samples: CDI - Interception factory</name>
12+
</project>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
import static java.lang.annotation.ElementType.METHOD;
4+
import static java.lang.annotation.ElementType.TYPE;
5+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
6+
7+
import java.lang.annotation.Inherited;
8+
import java.lang.annotation.Retention;
9+
import java.lang.annotation.Target;
10+
11+
import javax.enterprise.util.AnnotationLiteral;
12+
import javax.interceptor.InterceptorBinding;
13+
14+
@Inherited
15+
@InterceptorBinding
16+
@Retention(RUNTIME)
17+
@Target({ METHOD, TYPE })
18+
public @interface HelloAdder {
19+
20+
@SuppressWarnings("all")
21+
public static final class Literal extends AnnotationLiteral<HelloAdder> implements HelloAdder {
22+
public static final Literal INSTANCE = new Literal();
23+
private static final long serialVersionUID = 1L;
24+
}
25+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
import javax.annotation.Priority;
4+
import javax.interceptor.AroundInvoke;
5+
import javax.interceptor.Interceptor;
6+
import javax.interceptor.InvocationContext;
7+
8+
@HelloAdder
9+
@Interceptor
10+
@Priority(500)
11+
public class HelloAdderInterceptor {
12+
13+
@AroundInvoke
14+
public Object modifyGreet(InvocationContext context) throws Exception {
15+
16+
if (context.getMethod().getName().equals("setGreet")) {
17+
context.setParameters(new Object[] { "Hello " + context.getParameters()[0] });
18+
}
19+
20+
return context.proceed();
21+
}
22+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
public interface MyGreeting {
4+
String getGreet();
5+
void setGreet(String greet);
6+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
public class MyGreetingImpl implements MyGreeting {
4+
5+
private String greet;
6+
7+
public String getGreet() {
8+
return greet;
9+
}
10+
11+
public void setGreet(String greet) {
12+
this.greet = greet;
13+
}
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
import java.util.logging.Logger;
4+
5+
import javax.annotation.Priority;
6+
import javax.enterprise.context.ApplicationScoped;
7+
import javax.enterprise.inject.Alternative;
8+
import javax.enterprise.inject.Produces;
9+
import javax.enterprise.inject.spi.InterceptionFactory;
10+
11+
@Alternative
12+
@Priority(500)
13+
@ApplicationScoped
14+
public class MyGreetingProducer {
15+
16+
private static final Logger logger = Logger.getLogger(MyGreetingProducer.class.getName());
17+
18+
/**
19+
* This producer produces a MyGreeting alternative for MyGreetingImpl.
20+
* <p>
21+
* Note that the alternative is set by making the class, not the method, an
22+
* alternative.The alternative is activated via the <code>@Priority</code> annotation.
23+
*
24+
* @param interceptionFactory InterceptionFactory injected by CDI
25+
* @return MyGreeting instance, programmatically proxied
26+
*/
27+
@Produces
28+
public MyGreeting produce(InterceptionFactory<MyGreetingImpl> interceptionFactory) {
29+
30+
logger.info("Producing a MyGreeting");
31+
32+
// We're telling the InterceptionFactory here to dynamically add the @HelloAdder
33+
// annotation.
34+
interceptionFactory.configure().add(HelloAdder.Literal.INSTANCE);
35+
36+
// This will create a proxy as configured above around the bare
37+
// instance of MyGreetingImpl that we provide.
38+
return interceptionFactory.createInterceptedInstance(new MyGreetingImpl());
39+
}
40+
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.javaee8.cdi.interception.factory;
2+
3+
import static org.jboss.shrinkwrap.api.ShrinkWrap.create;
4+
import static org.junit.Assert.assertEquals;
5+
6+
import javax.inject.Inject;
7+
8+
import org.jboss.arquillian.container.test.api.Deployment;
9+
import org.jboss.arquillian.junit.Arquillian;
10+
import org.jboss.shrinkwrap.api.Archive;
11+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
12+
import org.junit.Test;
13+
import org.junit.runner.RunWith;
14+
15+
@RunWith(Arquillian.class)
16+
public class InterceptorFactoryTest {
17+
18+
@Deployment
19+
public static Archive<?> deploy() {
20+
return create(JavaArchive.class)
21+
.addClasses(
22+
MyGreeting.class, MyGreetingImpl.class, MyGreetingProducer.class,
23+
HelloAdder.class, HelloAdderInterceptor.class)
24+
.addAsManifestResource("beans.xml");
25+
}
26+
27+
@Inject
28+
private MyGreeting myGreeting;
29+
30+
@Test
31+
public void test() {
32+
myGreeting.setGreet("Reza");
33+
34+
assertEquals("Hello Reza", myGreeting.getGreet());
35+
}
36+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd"
6+
bean-discovery-mode="all">
7+
8+
</beans>

cdi/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
<groupId>org.javaee8.cdi</groupId>
1111
<artifactId>cdi-samples</artifactId>
1212
<packaging>pom</packaging>
13-
<name>Java EE 8 CDI Samples</name>
13+
<name>Java EE 8 Samples: CDI</name>
1414

1515
<modules>
16+
<module>interception-factory</module>
1617
<module>events-priority</module>
1718
</modules>
1819

0 commit comments

Comments
 (0)