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 interceptor/around-construct/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.interceptor</groupId>
<artifactId>interceptor-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<artifactId>around-construct</artifactId>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.javaee7.interceptor.aroundconstruct;

/**
* @author Radim Hanus
*/
public interface Greeting {
boolean isConstructed();
boolean isInitialized();

Param getParam();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package org.javaee7.interceptor.aroundconstruct;

import javax.annotation.PostConstruct;
import javax.inject.Inject;

/**
* @author Radim Hanus
*/
@MyInterceptorBinding
public class GreetingBean implements Greeting {
private boolean constructed = false;
private boolean initialized = false;

private Param param;

@Inject
public GreetingBean(Param param) {
this.param = param;
constructed = true;
}

@PostConstruct
void onPostConstruct() {
initialized = true;
}

@Override
public boolean isConstructed() {
return constructed;
}

@Override
public boolean isInitialized() {
return initialized;
}

@Override
public Param getParam() {
return param;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.javaee7.interceptor.aroundconstruct;

/**
* @author Radim Hanus
*/
public class GreetingParam implements Param {
private String value;

public GreetingParam() {
value = "Greeting";
}

@Override
public String getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package org.javaee7.interceptor.aroundconstruct;

import javax.interceptor.AroundConstruct;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;

/**
* @author Radim Hanus
*/
@Interceptor
@MyInterceptorBinding
public class MyInterceptor {
@AroundConstruct
public Object onConstruct(InvocationContext context) throws Exception {
// null before the InvocationContext.proceed() returns
Object target = context.getTarget();
isNull(target);
// null in case of AroundConstruct
Method method = context.getMethod();
isNull(method);
// NOT null in case of AroundConstruct
Constructor ctor = context.getConstructor();
isNotNull(ctor);

// perform the constructor injection
Object result = context.proceed();
isNull(result);

// NOT null after the InvocationContext.proceed() completes
target = context.getTarget();
isNotNull(target);
// a constructor should have been called
GreetingBean bean = (GreetingBean) target;
isBoolean(bean.isConstructed(), true);
isBoolean(bean.isInitialized(), false);
// constructor injection should have been done
isNotNull(bean.getParam());

return null;
}

private static void isNull(Object o) throws Exception {
if (o != null) {
throw new IllegalStateException("null required");
}
}

private static void isNotNull(Object o) throws Exception {
if (o == null) {
throw new IllegalStateException("not null required");
}
}

private static void isBoolean(Object o, Boolean value) {
if (!o.equals(value)) {
throw new IllegalStateException(value + " required");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.javaee7.interceptor.aroundconstruct;

import javax.interceptor.InterceptorBinding;
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 Radim Hanus
*/
@InterceptorBinding
@Retention(RUNTIME)
@Target({METHOD, TYPE})
public @interface MyInterceptorBinding {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.javaee7.interceptor.aroundconstruct;

/**
* @author Radim Hanus
*/
public interface Param {
String getValue();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.javaee7.interceptor.aroundconstruct;

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.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
* @author Radim Hanus
*/
@RunWith(Arquillian.class)
public class GreetingBeanTest {
@Inject
private Greeting bean;

@Deployment
public static Archive<?> deploy() {
return ShrinkWrap.create(JavaArchive.class)
.addClasses(Greeting.class, GreetingBean.class, GreetingParam.class, MyInterceptor.class, MyInterceptorBinding.class, Param.class)
.addAsManifestResource("beans.xml");
}

@Test
public void should_be_ready() throws Exception {
assertThat(bean, is(notNullValue()));
assertThat(bean, instanceOf(GreetingBean.class));
assertTrue(bean.isConstructed());
assertTrue(bean.isInitialized());
assertThat(bean.getParam(), instanceOf(GreetingParam.class));
}
}
16 changes: 16 additions & 0 deletions interceptor/around-construct/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="http://jboss.org/schema/arquillian"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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>
12 changes: 12 additions & 0 deletions interceptor/around-construct/src/test/resources/beans.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?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">

<interceptors>
<class>org.javaee7.interceptor.aroundconstruct.MyInterceptor</class>
</interceptors>

</beans>
20 changes: 20 additions & 0 deletions interceptor/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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</groupId>
<artifactId>javaee7-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

<groupId>org.javaee7.interceptor</groupId>
<artifactId>interceptor-samples</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>JSR 318 Interceptors 1.2 Samples</name>

<modules>
<module>around-construct</module>
</modules>
</project>
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@
<module>concurrency</module>
<module>ejb</module>
<module>el</module>
<module>interceptor</module>
<module>javamail</module>
<module>jaspic</module>
<module>jaxrs</module>
Expand Down