Skip to content

Commit 88fa35a

Browse files
committed
added top-level directory interceptor and its first module around-construct to demonstrate using @AroundConstruct
1 parent d7b03c5 commit 88fa35a

File tree

12 files changed

+261
-0
lines changed

12 files changed

+261
-0
lines changed
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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.interceptor</groupId>
6+
<artifactId>interceptor-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>around-construct</artifactId>
12+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
/**
4+
* Class description here.
5+
*
6+
* @author <a href="mailto:radim.hanus@apksoft.eu">Radim Hanus</a>
7+
*/
8+
public interface Greeting {
9+
boolean isConstructed();
10+
boolean isInitialized();
11+
12+
Param getParam();
13+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
import javax.annotation.PostConstruct;
4+
import javax.inject.Inject;
5+
6+
/**
7+
* @author Radim Hanus
8+
*/
9+
@MyInterceptorBinding
10+
public class GreetingBean implements Greeting {
11+
private boolean constructed = false;
12+
private boolean initialized = false;
13+
14+
private Param param;
15+
16+
@Inject
17+
public GreetingBean(Param param) {
18+
this.param = param;
19+
constructed = true;
20+
}
21+
22+
@PostConstruct
23+
void onPostConstruct() {
24+
initialized = true;
25+
}
26+
27+
@Override
28+
public boolean isConstructed() {
29+
return constructed;
30+
}
31+
32+
@Override
33+
public boolean isInitialized() {
34+
return initialized;
35+
}
36+
37+
@Override
38+
public Param getParam() {
39+
return param;
40+
}
41+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
/**
4+
* @author Radim Hanus
5+
*/
6+
public class GreetingParam implements Param {
7+
private String value;
8+
9+
public GreetingParam() {
10+
value = "Greeting";
11+
}
12+
13+
@Override
14+
public String getValue() {
15+
return value;
16+
}
17+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
import javax.interceptor.AroundConstruct;
4+
import javax.interceptor.Interceptor;
5+
import javax.interceptor.InvocationContext;
6+
import java.lang.reflect.Constructor;
7+
import java.lang.reflect.Method;
8+
9+
/**
10+
* @author Radim Hanus
11+
*/
12+
@Interceptor
13+
@MyInterceptorBinding
14+
public class MyInterceptor {
15+
@AroundConstruct
16+
public Object onConstruct(InvocationContext context) throws Exception {
17+
// null before the InvocationContext.proceed() returns
18+
Object target = context.getTarget();
19+
isNull(target);
20+
// null in case of AroundConstruct
21+
Method method = context.getMethod();
22+
isNull(method);
23+
// NOT null in case of AroundConstruct
24+
Constructor ctor = context.getConstructor();
25+
isNotNull(ctor);
26+
27+
// perform the constructor injection
28+
Object result = context.proceed();
29+
isNull(result);
30+
31+
// NOT null after the InvocationContext.proceed() completes
32+
target = context.getTarget();
33+
isNotNull(target);
34+
// a constructor should have been called
35+
GreetingBean bean = (GreetingBean) target;
36+
isBoolean(bean.isConstructed(), true);
37+
isBoolean(bean.isInitialized(), false);
38+
// constructor injection should have been done
39+
isNotNull(bean.getParam());
40+
41+
return null;
42+
}
43+
44+
private static void isNull(Object o) throws Exception {
45+
if (o != null) {
46+
throw new IllegalStateException("null required");
47+
}
48+
}
49+
50+
private static void isNotNull(Object o) throws Exception {
51+
if (o == null) {
52+
throw new IllegalStateException("not null required");
53+
}
54+
}
55+
56+
private static void isBoolean(Object o, Boolean value) {
57+
if (!o.equals(value)) {
58+
throw new IllegalStateException(value + " required");
59+
}
60+
}
61+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
import javax.interceptor.InterceptorBinding;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.Target;
6+
7+
import static java.lang.annotation.ElementType.METHOD;
8+
import static java.lang.annotation.ElementType.TYPE;
9+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
10+
11+
/**
12+
* @author Radim Hanus
13+
*/
14+
@InterceptorBinding
15+
@Retention(RUNTIME)
16+
@Target({METHOD, TYPE})
17+
public @interface MyInterceptorBinding {
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
/**
4+
* @author Radim Hanus
5+
*/
6+
public interface Param {
7+
String getValue();
8+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.javaee7.interceptor.aroundconstruct;
2+
3+
import org.jboss.arquillian.container.test.api.Deployment;
4+
import org.jboss.arquillian.junit.Arquillian;
5+
import org.jboss.shrinkwrap.api.Archive;
6+
import org.jboss.shrinkwrap.api.ShrinkWrap;
7+
import org.jboss.shrinkwrap.api.spec.JavaArchive;
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
11+
import javax.inject.Inject;
12+
13+
import static org.hamcrest.CoreMatchers.instanceOf;
14+
import static org.hamcrest.CoreMatchers.is;
15+
import static org.hamcrest.CoreMatchers.notNullValue;
16+
import static org.junit.Assert.assertThat;
17+
import static org.junit.Assert.assertTrue;
18+
19+
/**
20+
* @author Radim Hanus
21+
*/
22+
@RunWith(Arquillian.class)
23+
public class GreetingBeanTest {
24+
@Inject
25+
private Greeting bean;
26+
27+
@Deployment
28+
public static Archive<?> deploy() {
29+
return ShrinkWrap.create(JavaArchive.class)
30+
.addClasses(Greeting.class, GreetingBean.class, GreetingParam.class, MyInterceptor.class, MyInterceptorBinding.class, Param.class)
31+
.addAsManifestResource("beans.xml");
32+
}
33+
34+
@Test
35+
public void should_be_ready() throws Exception {
36+
assertThat(bean, is(notNullValue()));
37+
assertThat(bean, instanceOf(GreetingBean.class));
38+
assertTrue(bean.isConstructed());
39+
assertTrue(bean.isInitialized());
40+
assertThat(bean.getParam(), instanceOf(GreetingParam.class));
41+
}
42+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<arquillian xmlns="http://jboss.org/schema/arquillian"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://jboss.org/schema/arquillian http://jboss.org/schema/arquillian/arquillian_1_0.xsd">
6+
7+
<defaultProtocol type="Servlet 3.0"/>
8+
9+
<container qualifier="test" default="true">
10+
<configuration>
11+
<property name="jbossHome">${serverRoot:target/wildfly-8.0.0.CR1}</property>
12+
<property name="serverConfig">${serverProfile:standalone-full.xml}</property>
13+
</configuration>
14+
</container>
15+
16+
</arquillian>
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+
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_1_1.xsd"
6+
bean-discovery-mode="all">
7+
8+
<interceptors>
9+
<class>org.javaee7.interceptor.aroundconstruct.MyInterceptor</class>
10+
</interceptors>
11+
12+
</beans>

0 commit comments

Comments
 (0)