Skip to content

Commit b6ec989

Browse files
committed
added cdi/interceptors-prioriry
1 parent 4881b60 commit b6ec989

File tree

10 files changed

+297
-0
lines changed

10 files changed

+297
-0
lines changed

cdi/interceptors-prioriry/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">
3+
<modelVersion>4.0.0</modelVersion>
4+
<parent>
5+
<groupId>org.javaee7.cdi</groupId>
6+
<artifactId>cdi-samples</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<relativePath>../pom.xml</relativePath>
9+
</parent>
10+
11+
<artifactId>interceptors-prioriry</artifactId>
12+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.cdi.interceptors;
41+
42+
/**
43+
* @author Radim Hanus
44+
*/
45+
public interface Greeting {
46+
public String getGreet();
47+
public void setGreet(String name);
48+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package org.javaee7.cdi.interceptors;
2+
3+
import javax.annotation.Priority;
4+
import javax.interceptor.AroundInvoke;
5+
import javax.interceptor.Interceptor;
6+
import javax.interceptor.InvocationContext;
7+
8+
/**
9+
* Interceptors with smaller priority values are called first.
10+
*
11+
* @author Radim Hanus
12+
*/
13+
@Interceptor
14+
@MyInterceptorBinding
15+
@Priority(Interceptor.Priority.APPLICATION + 100)
16+
public class HighPriorityInterceptor {
17+
@AroundInvoke
18+
public Object log(InvocationContext context) throws Exception {
19+
Object[] parameters = context.getParameters();
20+
if (parameters.length > 0 && parameters[0] instanceof String) {
21+
String param = (String) parameters[0];
22+
parameters[0] = "Hi " + param + " !";
23+
context.setParameters(parameters);
24+
}
25+
return context.proceed();
26+
}
27+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javaee7.cdi.interceptors;
2+
3+
import javax.annotation.Priority;
4+
import javax.interceptor.AroundInvoke;
5+
import javax.interceptor.Interceptor;
6+
import javax.interceptor.InvocationContext;
7+
import java.lang.reflect.Field;
8+
9+
/**
10+
* Interceptors with smaller priority values are called first.
11+
*
12+
* @author Radim Hanus
13+
*/
14+
@Interceptor
15+
@MyInterceptorBinding
16+
@Priority(Interceptor.Priority.APPLICATION + 200)
17+
public class LowPriorityInterceptor {
18+
@AroundInvoke
19+
public Object log(InvocationContext context) throws Exception {
20+
Object[] parameters = context.getParameters();
21+
if (parameters.length > 0 && parameters[0] instanceof String) {
22+
String param = (String) parameters[0];
23+
parameters[0] = param + " Nice to meet you.";
24+
context.setParameters(parameters);
25+
}
26+
return context.proceed();
27+
}
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.cdi.interceptors;
41+
42+
import static java.lang.annotation.ElementType.TYPE;
43+
import static java.lang.annotation.ElementType.METHOD;
44+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
45+
import java.lang.annotation.Inherited;
46+
import java.lang.annotation.Retention;
47+
import java.lang.annotation.Target;
48+
import javax.interceptor.InterceptorBinding;
49+
50+
/**
51+
* @author Arun Gupta
52+
*/
53+
@Inherited
54+
@InterceptorBinding
55+
@Retention(RUNTIME)
56+
@Target({METHOD, TYPE})
57+
public @interface MyInterceptorBinding {
58+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.cdi.interceptors;
41+
42+
/**
43+
* @author Radim Hanus
44+
*/
45+
@MyInterceptorBinding
46+
public class SimpleGreeting implements Greeting {
47+
private String greet;
48+
49+
public String getGreet() {
50+
return greet;
51+
}
52+
53+
public void setGreet(String greet) {
54+
this.greet = greet;
55+
}
56+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package org.javaee7.cdi.interceptors;
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.*;
14+
import static org.junit.Assert.assertEquals;
15+
import static org.junit.Assert.assertThat;
16+
17+
/**
18+
* Note that beans.xml doesn't define any interceptor. Interceptors declared using interceptor bindings
19+
* are enabled for the entire application and ordered using the Priority annotation.
20+
*
21+
* @author Radim Hanus
22+
*/
23+
@RunWith(Arquillian.class)
24+
public class GreetingTest {
25+
@Deployment
26+
public static Archive<?> deploy() {
27+
return ShrinkWrap.create(JavaArchive.class)
28+
.addClasses(Greeting.class, SimpleGreeting.class, MyInterceptorBinding.class, LowPriorityInterceptor.class, HighPriorityInterceptor.class)
29+
.addAsManifestResource("beans.xml");
30+
}
31+
32+
@Inject
33+
Greeting bean;
34+
35+
@Test
36+
public void test() throws Exception {
37+
assertThat(bean, is(notNullValue()));
38+
assertThat(bean, instanceOf(SimpleGreeting.class));
39+
40+
bean.setGreet("Arun");
41+
assertEquals(bean.getGreet(), "Hi Arun ! Nice to meet you.");
42+
}
43+
}
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:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns="http://jboss.org/schema/arquillian"
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: 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_1_1.xsd"
6+
bean-discovery-mode="all">
7+
8+
</beans>

cdi/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<module>exclude-filter</module>
2525
<module>built-in</module>
2626
<module>interceptors</module>
27+
<module>interceptors-prioriry</module>
2728
<module>nobeans-xml</module>
2829
<module>beansxml-noversion</module>
2930
<module>beanmanager</module>

0 commit comments

Comments
 (0)