Skip to content

Commit 41a5596

Browse files
committed
OWB-1448 Fix Issue with Cdi annotation and alternatives
1 parent 0376bd7 commit 41a5596

File tree

5 files changed

+200
-2
lines changed

5 files changed

+200
-2
lines changed

webbeans-impl/src/main/java/org/apache/webbeans/config/BeansDeployer.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,16 @@
108108
import javax.enterprise.inject.spi.ObserverMethod;
109109
import javax.enterprise.inject.spi.Producer;
110110

111+
import java.io.IOException;
111112
import java.lang.annotation.Annotation;
112113
import java.lang.reflect.Constructor;
113114
import java.lang.reflect.Method;
114115
import java.lang.reflect.Modifier;
115116
import java.lang.reflect.ParameterizedType;
116117
import java.lang.reflect.Type;
117118
import java.net.URL;
119+
import java.net.URLConnection;
120+
import java.net.URLStreamHandler;
118121
import java.security.PrivilegedActionException;
119122
import java.util.ArrayList;
120123
import java.util.Collection;
@@ -1707,6 +1710,35 @@ protected void deployFromXML(ScannerService scanner)
17071710
}
17081711

17091712
logger.fine("Deploying configurations from XML has ended successfully.");
1713+
1714+
try
1715+
{
1716+
final URL url = new URL("openwebbeans", null, 0, "cdi-standalone", new URLStreamHandler()
1717+
{
1718+
@Override
1719+
protected URLConnection openConnection(URL u) throws IOException
1720+
{
1721+
return null;
1722+
}
1723+
});
1724+
1725+
final BeanArchiveInformation beanArchiveInformation = beanArchiveService.getBeanArchiveInformation(url);
1726+
if (beanArchiveInformation != null)
1727+
{
1728+
configureDecorators(url, beanArchiveInformation.getDecorators());
1729+
configureInterceptors(url, beanArchiveInformation.getInterceptors());
1730+
configureAlternatives(url, beanArchiveInformation.getAlternativeClasses(), false);
1731+
configureAlternatives(url, beanArchiveInformation.getAlternativeStereotypes(), true);
1732+
configureAllowProxying(url, beanArchiveInformation.getAllowProxyingClasses());
1733+
}
1734+
1735+
logger.fine("Deploying embedded configurations has ended successfully.");
1736+
}
1737+
catch (Exception e)
1738+
{
1739+
logger.info("Error occurred: " + e.getMessage());
1740+
e.printStackTrace();
1741+
}
17101742
}
17111743

17121744
private void configureAlternatives(URL bdaLocation, List<String> alternatives, boolean isStereotype)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.openwebbeans.junit5.features;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.enterprise.inject.Alternative;
23+
import javax.enterprise.inject.Default;
24+
import javax.inject.Inject;
25+
import org.apache.openwebbeans.junit5.Cdi;
26+
import org.junit.jupiter.api.Test;
27+
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
29+
30+
@Cdi(disableDiscovery = true, classes = {
31+
AlternativeTest.Service.class, AlternativeTest.Provider.class, AlternativeTest.DefaultProvider.class, AlternativeTest.AlternativeProvider.class
32+
}, alternatives = AlternativeTest.AlternativeProvider.class)
33+
public class AlternativeTest
34+
{
35+
@Inject
36+
private Service service;
37+
38+
@Test
39+
void test1()
40+
{
41+
assertEquals("alternative", service.run());
42+
}
43+
44+
public interface Provider
45+
{
46+
String provide();
47+
}
48+
49+
@Alternative
50+
public static class AlternativeProvider implements Provider
51+
{
52+
@Override
53+
public String provide()
54+
{
55+
return "alternative";
56+
}
57+
}
58+
59+
@Default
60+
public static class DefaultProvider implements Provider
61+
{
62+
@Override
63+
public String provide()
64+
{
65+
return "default";
66+
}
67+
}
68+
69+
@ApplicationScoped
70+
public static class Service
71+
{
72+
73+
@Inject
74+
private Provider provider;
75+
76+
public String run()
77+
{
78+
return provider.provide();
79+
}
80+
81+
}
82+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.openwebbeans.junit5.features;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.enterprise.context.NormalScope;
23+
import javax.enterprise.inject.Alternative;
24+
import javax.enterprise.inject.Default;
25+
import javax.inject.Inject;
26+
import javax.interceptor.AroundInvoke;
27+
import javax.interceptor.Interceptor;
28+
import javax.interceptor.InterceptorBinding;
29+
import javax.interceptor.InvocationContext;
30+
import org.apache.openwebbeans.junit5.Cdi;
31+
import org.junit.jupiter.api.Test;
32+
33+
import java.lang.annotation.ElementType;
34+
import java.lang.annotation.Retention;
35+
import java.lang.annotation.RetentionPolicy;
36+
import java.lang.annotation.Target;
37+
38+
import static org.junit.jupiter.api.Assertions.assertEquals;
39+
40+
@Cdi(disableDiscovery = true, classes = {
41+
InterceptorTest.Service.class, InterceptorTest.MyInterceptor.class, InterceptorTest.Wrap.class
42+
}, interceptors = InterceptorTest.MyInterceptor.class)
43+
public class InterceptorTest
44+
{
45+
@Inject
46+
private Service service;
47+
48+
@Test
49+
void test1()
50+
{
51+
assertEquals("Intercepted Hello World", service.run());
52+
}
53+
54+
@Target({ElementType.TYPE, ElementType.METHOD})
55+
@Retention(RetentionPolicy.RUNTIME)
56+
@InterceptorBinding
57+
public @interface Wrap {
58+
59+
}
60+
61+
@Interceptor
62+
@Wrap
63+
public static class MyInterceptor {
64+
@AroundInvoke
65+
public Object restrictAccessBasedOnTime(InvocationContext ctx) throws Exception {
66+
final Object result = ctx.proceed();
67+
if (result instanceof String) {
68+
return "Intercepted " + result;
69+
} else {
70+
return result;
71+
}
72+
}
73+
}
74+
75+
@ApplicationScoped
76+
public static class Service
77+
{
78+
@Wrap
79+
public String run()
80+
{
81+
return "Hello World";
82+
}
83+
}
84+
}

webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/parameter/ParameterResolutionTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import static org.junit.jupiter.api.Assertions.assertNull;
3535
import static org.junit.jupiter.api.Assertions.assertSame;
3636

37-
@Cdi(classes = MyService.class)
37+
@Cdi(classes = MyService.class, disableDiscovery = true)
3838
class ParameterResolutionTest
3939
{
4040
@Inject MyService service;

webbeans-junit5/src/test/java/org/apache/openwebbeans/junit5/perclass/PerMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public Property[] properties()
180180
@Override
181181
public boolean disableDiscovery()
182182
{
183-
return false;
183+
return true;
184184
}
185185

186186
@Override

0 commit comments

Comments
 (0)