Skip to content

Commit

Permalink
PAYARA-3121 Jersey Crashes for EJB Classes of Same Name (eclipse-ee4j…
Browse files Browse the repository at this point in the history
…#3950)

* PAYARA-3121 Jersey Crashes for EJB Classes of Same Name

Signed-off-by: Gaurav Gupta <[email protected]>
  • Loading branch information
jGauravGupta authored and senivam committed Jun 24, 2019
1 parent b4497f5 commit a843c05
Showing 1 changed file with 29 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) [2018-2019] [Payara Foundation and/or its affiliates].
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -13,7 +14,6 @@
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.gf.ejb.internal;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -331,7 +331,7 @@ private void logLookupException(final Method method, final Class<?> component, C
LocalizationMessages.EJB_INTERFACE_HANDLING_METHOD_LOOKUP_EXCEPTION(method, component, iFace), ex);
}

private List<Class> remoteAndLocalIfaces(final Class<?> resourceClass) {
private static List<Class> remoteAndLocalIfaces(final Class<?> resourceClass) {
final List<Class> allLocalOrRemoteIfaces = new LinkedList<>();
if (resourceClass.isAnnotationPresent(Remote.class)) {
allLocalOrRemoteIfaces.addAll(Arrays.asList(resourceClass.getAnnotation(Remote.class).value()));
Expand All @@ -357,18 +357,22 @@ private static InitialContext getInitialContext() {
}
}

private static Object lookup(InitialContext ic, Class<?> c, String name, EjbComponentProvider provider)
private static Object lookup(InitialContext ic, Class<?> rawType, String name, EjbComponentProvider provider)
throws NamingException {
try {
return lookupSimpleForm(ic, name, provider);
return lookupSimpleForm(ic, rawType, name, provider);
} catch (NamingException ex) {
LOGGER.log(Level.WARNING, LocalizationMessages.EJB_CLASS_SIMPLE_LOOKUP_FAILED(c.getName()), ex);
LOGGER.log(Level.WARNING, LocalizationMessages.EJB_CLASS_SIMPLE_LOOKUP_FAILED(rawType.getName()), ex);

return lookupFullyQualifiedForm(ic, c, name, provider);
return lookupFullyQualifiedForm(ic, rawType, name, provider);
}
}

private static Object lookupSimpleForm(InitialContext ic, String name, EjbComponentProvider provider) throws NamingException {
private static Object lookupSimpleForm(
InitialContext ic,
Class<?> rawType,
String name,
EjbComponentProvider provider) throws NamingException {
if (provider.libNames.isEmpty()) {
String jndiName = "java:module/" + name;
return ic.lookup(jndiName);
Expand All @@ -379,7 +383,7 @@ private static Object lookupSimpleForm(InitialContext ic, String name, EjbCompon
Object result;
try {
result = ic.lookup(jndiName);
if (result != null) {
if (result != null && isLookupInstanceValid(rawType, result)) {
return result;
}
} catch (NamingException e) {
Expand All @@ -390,19 +394,22 @@ private static Object lookupSimpleForm(InitialContext ic, String name, EjbCompon
}
}

private static Object lookupFullyQualifiedForm(InitialContext ic, Class<?> c, String name, EjbComponentProvider provider)
throws NamingException {
private static Object lookupFullyQualifiedForm(
InitialContext ic,
Class<?> rawType,
String name,
EjbComponentProvider provider) throws NamingException {
if (provider.libNames.isEmpty()) {
String jndiName = "java:module/" + name + "!" + c.getName();
String jndiName = "java:module/" + name + "!" + rawType.getName();
return ic.lookup(jndiName);
} else {
NamingException ne = null;
for (String moduleName : provider.libNames) {
String jndiName = "java:app/" + moduleName + "/" + name + "!" + c.getName();
String jndiName = "java:app/" + moduleName + "/" + name + "!" + rawType.getName();
Object result;
try {
result = ic.lookup(jndiName);
if (result != null) {
if (result != null && isLookupInstanceValid(rawType, result)) {
return result;
}
} catch (NamingException e) {
Expand All @@ -412,4 +419,13 @@ private static Object lookupFullyQualifiedForm(InitialContext ic, Class<?> c, St
throw (ne != null) ? ne : new NamingException();
}
}

private static boolean isLookupInstanceValid(Class<?> rawType, Object result){
return rawType.isInstance(result)
|| remoteAndLocalIfaces(rawType)
.stream()
.filter(iface -> iface.isInstance(result))
.findAny()
.isPresent();
}
}

0 comments on commit a843c05

Please sign in to comment.