Skip to content

Commit c01f9a8

Browse files
authored
Merge pull request #15492 from andymc12/alternateInjectionFactory
Handle beans.xml with discoveryMode="none" - no beanManager
2 parents 7c2ee79 + 70e0076 commit c01f9a8

File tree

5 files changed

+158
-2
lines changed

5 files changed

+158
-2
lines changed

dev/io.openliberty.org.jboss.resteasy.common/src/org/jboss/resteasy/plugins/server/servlet/ConfigurationBootstrap.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ public ResteasyDeployment createDeployment()
237237

238238
// Liberty Change: TODO: revert this back when we sort out the config via web fragments.
239239
//String injectorFactoryClass = "org.jboss.resteasy.cdi.CdiInjectorFactory"; // getParameter("resteasy.injector.factory");
240-
String injectorFactoryClass = "io.openliberty.org.jboss.resteasy.common.cdi.LibertyCdiInjectorFactory";
240+
String injectorFactoryClass = "io.openliberty.org.jboss.resteasy.common.cdi.LibertyFallbackInjectorFactory";
241241
if (injectorFactoryClass != null)
242242
{
243243
deployment.setInjectorFactoryClass(injectorFactoryClass);

dev/io.openliberty.org.jboss.resteasy.server/src/io/openliberty/org/jboss/resteasy/common/cdi/LibertyCdiInjectorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected BeanManager lookupBeanManager() {
4646
try {
4747
return super.lookupBeanManager();
4848
} catch (Exception ex) {
49-
return null;
49+
throw new RuntimeException("Cannot load bean manager for module " + (cmd == null ? "unknown" :cmd.getJ2EEName()));
5050
}
5151
}
5252

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2021 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
package io.openliberty.org.jboss.resteasy.common.cdi;
12+
13+
import java.lang.annotation.Annotation;
14+
import java.lang.reflect.AccessibleObject;
15+
import java.lang.reflect.Constructor;
16+
import java.lang.reflect.Type;
17+
18+
import org.jboss.resteasy.core.InjectorFactoryImpl;
19+
import org.jboss.resteasy.spi.ConstructorInjector;
20+
import org.jboss.resteasy.spi.InjectorFactory;
21+
import org.jboss.resteasy.spi.MethodInjector;
22+
import org.jboss.resteasy.spi.PropertyInjector;
23+
import org.jboss.resteasy.spi.ResteasyProviderFactory;
24+
import org.jboss.resteasy.spi.ValueInjector;
25+
import org.jboss.resteasy.spi.metadata.Parameter;
26+
import org.jboss.resteasy.spi.metadata.ResourceClass;
27+
import org.jboss.resteasy.spi.metadata.ResourceConstructor;
28+
import org.jboss.resteasy.spi.metadata.ResourceLocator;
29+
30+
import com.ibm.websphere.ras.Tr;
31+
import com.ibm.websphere.ras.TraceComponent;
32+
33+
@SuppressWarnings("rawtypes")
34+
public class LibertyFallbackInjectorFactory implements InjectorFactory {
35+
private static final TraceComponent tc = Tr.register(LibertyFallbackInjectorFactory.class);
36+
37+
private final InjectorFactory delegate;
38+
39+
public LibertyFallbackInjectorFactory() {
40+
InjectorFactory factory;
41+
try {
42+
factory = new LibertyCdiInjectorFactory();
43+
} catch (Throwable t) {
44+
if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
45+
Tr.debug(tc, "Caught exception initializing LibertyCdiInjectorFactory - expected if module declines CDI", t);
46+
}
47+
factory = new InjectorFactoryImpl();
48+
}
49+
delegate = factory;
50+
}
51+
@Override
52+
public ConstructorInjector createConstructor(Constructor constructor, ResteasyProviderFactory factory) {
53+
return delegate.createConstructor(constructor, factory);
54+
}
55+
56+
@Override
57+
public PropertyInjector createPropertyInjector(Class resourceClass, ResteasyProviderFactory factory) {
58+
return delegate.createPropertyInjector(resourceClass, factory);
59+
}
60+
61+
@Override
62+
public ValueInjector createParameterExtractor(Class injectTargetClass, AccessibleObject injectTarget, String defaultName, Class type, Type genericType, Annotation[] annotations, ResteasyProviderFactory factory) {
63+
return delegate.createParameterExtractor(injectTargetClass, injectTarget, defaultName, type, genericType, annotations, factory);
64+
}
65+
66+
@Override
67+
public ValueInjector createParameterExtractor(Class injectTargetClass, AccessibleObject injectTarget, String defaultName, Class type, Type genericType, Annotation[] annotations, boolean useDefault, ResteasyProviderFactory factory) {
68+
return delegate.createParameterExtractor(injectTargetClass, injectTarget, defaultName, type, genericType, annotations, useDefault, factory);
69+
}
70+
71+
@Override
72+
public ValueInjector createParameterExtractor(Parameter parameter, ResteasyProviderFactory providerFactory) {
73+
return delegate.createParameterExtractor(parameter, providerFactory);
74+
}
75+
76+
@Override
77+
public MethodInjector createMethodInjector(ResourceLocator method, ResteasyProviderFactory factory) {
78+
return delegate.createMethodInjector(method, factory);
79+
}
80+
81+
@Override
82+
public PropertyInjector createPropertyInjector(ResourceClass resourceClass, ResteasyProviderFactory providerFactory) {
83+
return delegate.createPropertyInjector(resourceClass, providerFactory);
84+
}
85+
86+
@Override
87+
public ConstructorInjector createConstructor(ResourceConstructor constructor, ResteasyProviderFactory providerFactory) {
88+
return delegate.createConstructor(constructor, providerFactory);
89+
}
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2020 IBM Corporation and others.
3+
* All rights reserved. This program and the accompanying materials
4+
* are made available under the terms of the Eclipse Public License v1.0
5+
* which accompanies this distribution, and is available at
6+
* http://www.eclipse.org/legal/epl-v10.html
7+
*
8+
* Contributors:
9+
* IBM Corporation - initial API and implementation
10+
*******************************************************************************/
11+
package io.openliberty.restfulWS30.fat;
12+
13+
import org.jboss.shrinkwrap.api.ShrinkWrap;
14+
import org.jboss.shrinkwrap.api.asset.StringAsset;
15+
import org.jboss.shrinkwrap.api.spec.WebArchive;
16+
import org.junit.AfterClass;
17+
import org.junit.BeforeClass;
18+
import org.junit.runner.RunWith;
19+
20+
import com.ibm.websphere.simplicity.ShrinkHelper;
21+
import com.ibm.websphere.simplicity.ShrinkHelper.DeployOptions;
22+
23+
import componenttest.annotation.AllowedFFDC;
24+
import componenttest.annotation.Server;
25+
import componenttest.annotation.TestServlet;
26+
import componenttest.custom.junit.runner.FATRunner;
27+
import componenttest.topology.impl.LibertyServer;
28+
import componenttest.topology.utils.FATServletClient;
29+
import io.openliberty.restfulWS30.fat.appandresource.AppAndResourceTestServlet;
30+
31+
/**
32+
* Tests whether a class can be both an <code>Application</code> subclass
33+
* <em>and<em> a resource class.
34+
*/
35+
@RunWith(FATRunner.class)
36+
public class AppAndResourceCDIBeanDiscoveryModeDisabledTest extends FATServletClient {
37+
38+
public static final String APP_NAME = "appandresource";
39+
public static final String SERVER_NAME = APP_NAME;
40+
private static final String BEANS_XML = "<beans xmlns=\"http://xmlns.jcp.org/xml/ns/javaee\"\n"
41+
+ " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
42+
+ " xsi:schemaLocation=\"http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_2_0.xsd\""
43+
+ " bean-discovery-mode=\"none\" version=\"2.0\"></beans>";
44+
45+
@Server(SERVER_NAME)
46+
@TestServlet(servlet = AppAndResourceTestServlet.class, contextRoot = APP_NAME)
47+
public static LibertyServer server;
48+
49+
@BeforeClass
50+
public static void setUp() throws Exception {
51+
WebArchive war = ShrinkWrap.create(WebArchive.class, APP_NAME + ".war")
52+
.addAsWebInfResource(new StringAsset(BEANS_XML), "beans.xml")
53+
.addPackages(true, AppAndResourceTestServlet.class.getPackage());
54+
55+
56+
ShrinkHelper.exportDropinAppToServer(server, war, DeployOptions.SERVER_ONLY);
57+
58+
server.startServer();
59+
}
60+
61+
@AfterClass
62+
public static void tearDown() throws Exception {
63+
server.stopServer();
64+
}
65+
}

dev/io.openliberty.restfulWS.3.0_fat/fat/src/io/openliberty/restfulWS30/fat/FATSuite.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
@RunWith(Suite.class)
1818
@SuiteClasses({
1919
AppAndResourceTest.class,
20+
AppAndResourceCDIBeanDiscoveryModeDisabledTest.class,
2021
InjectAppTest.class,
2122
JsonbTest.class,
2223
ValidatorTest.class,

0 commit comments

Comments
 (0)