Skip to content

Commit

Permalink
JNDI: webservices vs. SimpleJndiName
Browse files Browse the repository at this point in the history
Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Nov 14, 2022
1 parent 4979035 commit dfd850c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 135 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,32 +17,39 @@

package org.glassfish.webservices.connector.annotation.handlers;

import java.lang.reflect.AnnotatedElement;
import com.sun.enterprise.deployment.InjectionTarget;
import com.sun.enterprise.deployment.ServiceRefPortInfo;
import com.sun.enterprise.deployment.ServiceReferenceDescriptor;
import com.sun.enterprise.deployment.annotation.context.AppClientContext;
import com.sun.enterprise.deployment.annotation.context.ServiceReferenceContainerContext;
import com.sun.enterprise.deployment.annotation.handlers.AbstractHandler;
import com.sun.enterprise.deployment.types.ServiceReferenceContainer;

import jakarta.xml.ws.RespectBinding;
import jakarta.xml.ws.WebServiceClient;
import jakarta.xml.ws.WebServiceRef;
import jakarta.xml.ws.soap.Addressing;
import jakarta.xml.ws.soap.MTOM;
import jakarta.xml.ws.spi.WebServiceFeatureAnnotation;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
import java.lang.reflect.AnnotatedElement;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import java.util.Map;
import java.util.HashMap;
import java.util.logging.Level;

import jakarta.xml.ws.*;
import jakarta.xml.ws.RespectBinding;
import jakarta.xml.ws.spi.WebServiceFeatureAnnotation;
import jakarta.xml.ws.soap.MTOM;
import jakarta.xml.ws.soap.Addressing;
import java.util.Map;

import org.glassfish.apf.*;
import org.glassfish.apf.AnnotatedElementHandler;
import org.glassfish.apf.AnnotationHandlerFor;
import org.glassfish.apf.AnnotationInfo;
import org.glassfish.apf.AnnotationProcessorException;
import org.glassfish.apf.HandlerProcessingResult;
import org.glassfish.apf.ResultType;
import org.glassfish.apf.impl.HandlerProcessingResultImpl;

import com.sun.enterprise.deployment.annotation.context.AppClientContext;
import com.sun.enterprise.deployment.annotation.context.ServiceReferenceContainerContext;
import com.sun.enterprise.deployment.*;
import com.sun.enterprise.deployment.types.ServiceReferenceContainer;
import org.glassfish.api.naming.SimpleJndiName;
import org.jvnet.hk2.annotations.Service;
import com.sun.enterprise.deployment.annotation.handlers.AbstractHandler;

import static com.sun.enterprise.util.StringUtils.ok;

Expand Down Expand Up @@ -162,12 +170,13 @@ protected HandlerProcessingResult processAWsRef(AnnotationInfo annInfo,
// Other annotations like SchemaValidation etc to be passed on to
// ServiceReferenceDescriptor
Map<Class<? extends Annotation>, Annotation> otherAnnotations =
new HashMap<Class<? extends Annotation>, Annotation>();
new HashMap<>();

for (Annotation a : annElem.getAnnotations()) {
if (!(a.annotationType().isAnnotationPresent(
WebServiceFeatureAnnotation.class)))
WebServiceFeatureAnnotation.class))) {
continue;
}
if (a instanceof MTOM) {
mtom = (MTOM)a;
} else if (a instanceof Addressing) {
Expand Down Expand Up @@ -215,19 +224,21 @@ protected HandlerProcessingResult processAWsRef(AnnotationInfo annInfo,
// merge other annotations
Map<Class<? extends Annotation>, Annotation> oa =
aRef.getOtherAnnotations();
if (oa == null)
if (oa == null) {
aRef.setOtherAnnotations(otherAnnotations);
else {
} else {
for (Map.Entry<Class<? extends Annotation>, Annotation> entry :
otherAnnotations.entrySet()) {
if (!oa.containsKey(entry.getKey()))
if (!oa.containsKey(entry.getKey())) {
oa.put(entry.getKey(), entry.getValue());
}
}
}

// merge wsdlLocation
if (!ok(aRef.getWsdlFileUri()) && ok(annotation.wsdlLocation()))
if (!ok(aRef.getWsdlFileUri()) && ok(annotation.wsdlLocation())) {
aRef.setWsdlFileUri(annotation.wsdlLocation());
}

if (!aRef.hasMtomEnabled() && mtom != null) {
aRef.setMtomEnabled(mtom.enabled());
Expand All @@ -244,24 +255,24 @@ protected HandlerProcessingResult processAWsRef(AnnotationInfo annInfo,

// check RespectBinding annotation
if (aRef.getRespectBinding() == null && respectBinding != null) {
aRef.setRespectBinding(
new com.sun.enterprise.deployment.RespectBinding(
respectBinding.enabled()));
aRef.setRespectBinding(new com.sun.enterprise.deployment.RespectBinding(respectBinding.enabled()));
}

// Store mapped name that is specified
if (!ok(aRef.getMappedName()) && ok(annotation.mappedName()))
aRef.setMappedName(annotation.mappedName());
if ((aRef.getMappedName() == null || aRef.getMappedName().isEmpty()) && ok(annotation.mappedName())) {
aRef.setMappedName(new SimpleJndiName(annotation.mappedName()));
}

// Store lookup name that is specified
if (!aRef.hasLookupName() &&
ok(getLookupValue(annotation, annInfo)))
aRef.setLookupName(getLookupValue(annotation, annInfo));
if (!aRef.hasLookupName() && ok(annotation.lookup())) {
aRef.setLookupName(new SimpleJndiName(annotation.lookup()));
}

aRef.setInjectResourceType("jakarta.jws.WebServiceRef");

if (target != null)
if (target != null) {
aRef.addInjectionTarget(target);
}

// Read the WebServiceClient annotation for the service name space
// uri and wsdl (if required)
Expand All @@ -288,8 +299,7 @@ protected HandlerProcessingResult processAWsRef(AnnotationInfo annInfo,
if (aRef.getInjectionTargetType() == null) {
aRef.setInjectionTargetType(annotatedType.getName());
}
wsclientAnn = (WebServiceClient)
annotation.value().getAnnotation(WebServiceClient.class);
wsclientAnn = annotation.value().getAnnotation(WebServiceClient.class);
} else {
// no value provided in the annotation
wsclientAnn = (WebServiceClient)
Expand Down Expand Up @@ -334,32 +344,4 @@ public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo)
WebServiceRef annotation = (WebServiceRef) annInfo.getAnnotation();
return(processAWsRef(annInfo, annotation));
}

/**
* Return the value of the "lookup" element of the WebServiceRef annotation.
* This method handles the case where the WebServiceRef class is an older
* version before the lookup element was added; in that case access to
* the lookup element will cause a NoSuchMethodError, which is caught
* and ignored (with a warning message).
*
* @return the value of the lookup element
*/
private String getLookupValue(WebServiceRef annotation,
AnnotationInfo ainfo) {
String lookupValue = "";
try {
lookupValue = annotation.lookup();
} catch(NoSuchMethodError nsme) {
// Probably means an older version of Resource is being picked up from somewhere.
// Don't treat this as a fatal error.
try {
log(Level.WARNING, ainfo,
I18N.getLocalString(
"enterprise.deployment.annotation.handlers.wrongresourceclass",
"Incorrect @Resource annotation class definition - " +
"missing lookup attribute"));
} catch (AnnotationProcessorException ex) { }
}
return lookupValue;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,7 +17,11 @@

package org.glassfish.webservices;

import com.sun.enterprise.deployment.*;
import com.sun.enterprise.deployment.Addressing;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.InjectionTarget;
import com.sun.enterprise.deployment.ResourceReferenceDescriptor;
import com.sun.enterprise.deployment.WebServiceEndpoint;
import com.sun.enterprise.util.io.FileUtils;
import com.sun.xml.ws.api.BindingID;
import com.sun.xml.ws.api.WSBinding;
Expand All @@ -26,26 +31,29 @@
import com.sun.xml.ws.api.server.WSEndpoint;
import com.sun.xml.ws.transport.http.servlet.ServletAdapter;
import com.sun.xml.ws.transport.http.servlet.ServletAdapterList;
import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.ejb.api.EJBInvocation;
import org.glassfish.ejb.api.EjbEndpointFacade;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;

import jakarta.xml.ws.WebServiceContext;
import jakarta.xml.ws.WebServiceFeature;
import jakarta.xml.ws.soap.AddressingFeature;
import jakarta.xml.ws.soap.MTOMFeature;

import java.io.File;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.glassfish.api.invocation.ComponentInvocation;
import org.glassfish.api.invocation.InvocationManager;
import org.glassfish.api.logging.LogHelper;
import org.glassfish.ejb.api.EJBInvocation;
import org.glassfish.ejb.api.EjbEndpointFacade;
import org.glassfish.internal.data.ApplicationInfo;
import org.glassfish.internal.data.ApplicationRegistry;

import static org.glassfish.api.naming.SimpleJndiName.JNDI_CTX_JAVA_COMPONENT_ENV;


/**
Expand Down Expand Up @@ -114,14 +122,10 @@ public Object prepareInvocation(boolean doPreInvoke)


EjbDescriptor ejbDesc = endpoint.getEjbComponentImpl();
Iterator<ResourceReferenceDescriptor> it = ejbDesc.getResourceReferenceDescriptors().iterator();
while(it.hasNext()) {
ResourceReferenceDescriptor r = it.next();
for (ResourceReferenceDescriptor r : ejbDesc.getResourceReferenceDescriptors()) {
if(r.isWebServiceContext()) {
Iterator<InjectionTarget> iter = r.getInjectionTargets().iterator();
boolean matchingClassFound = false;
while(iter.hasNext()) {
InjectionTarget target = iter.next();
for (InjectionTarget target : r.getInjectionTargets()) {
if(ejbDesc.getEjbClassName().equals(target.getClassName())) {
matchingClassFound = true;
break;
Expand All @@ -132,7 +136,8 @@ public Object prepareInvocation(boolean doPreInvoke)
}
try {
javax.naming.InitialContext ic = new javax.naming.InitialContext();
wsCtxt = (WebServiceContextImpl) ic.lookup("java:comp/env/" + r.getName());
wsCtxt = (WebServiceContextImpl) ic
.lookup(JNDI_CTX_JAVA_COMPONENT_ENV + r.getName());
} catch (Throwable t) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogUtils.ERROR_EREI, t.getCause());
Expand Down Expand Up @@ -212,7 +217,7 @@ public Object prepareInvocation(boolean doPreInvoke)
boolean mtomEnabled = wsu.getMtom(endpoint);
WSBinding binding = null;

ArrayList<WebServiceFeature> wsFeatures = new ArrayList<WebServiceFeature>();
ArrayList<WebServiceFeature> wsFeatures = new ArrayList<>();
// Only if MTOm is enabled create the Binding with the MTOMFeature
if (mtomEnabled) {
int mtomThreshold = endpoint.getMtomThreshold() != null ? Integer.parseInt(endpoint.getMtomThreshold()):0;
Expand Down Expand Up @@ -269,8 +274,9 @@ public Object prepareInvocation(boolean doPreInvoke)
adapterList = new ServletAdapterList();
}
Object obj = adapterList.createAdapter(endpoint.getName(), urlPattern, wsep);
if (obj instanceof ServletAdapter)
if (obj instanceof ServletAdapter) {
adapter = ServletAdapter.class.cast(obj);
}
handlersConfigured=true;
} catch (Throwable t) {
LogHelper.log(logger, Level.SEVERE,
Expand Down Expand Up @@ -298,14 +304,11 @@ private void addWSContextInfo(WebServiceContextImpl wsCtxt) {
WebServiceContextImpl wsc = null;

EjbDescriptor bundle = endpoint.getEjbComponentImpl();
Iterator<ResourceReferenceDescriptor> it = bundle.getResourceReferenceDescriptors().iterator();

while(it.hasNext()) {
ResourceReferenceDescriptor r = it.next();
for (ResourceReferenceDescriptor r : bundle.getResourceReferenceDescriptors()) {
if(r.isWebServiceContext()) {
try {
javax.naming.InitialContext ic = new javax.naming.InitialContext();
wsc = (WebServiceContextImpl) ic.lookup("java:comp/env/" + r.getName());
wsc = (WebServiceContextImpl) ic.lookup(JNDI_CTX_JAVA_COMPONENT_ENV + r.getName());
} catch (Throwable t) {
if (logger.isLoggable(Level.FINE)) {
logger.log(Level.FINE, LogUtils.EXCEPTION_THROWN, t);
Expand Down Expand Up @@ -365,8 +368,10 @@ public EjbEndpointFacade getContainer() {

private AddressingFeature.Responses getResponse(String s) {
if (s != null) {
return AddressingFeature.Responses.valueOf(AddressingFeature.Responses.class,s);
} else return AddressingFeature.Responses.ALL;
return Enum.valueOf(AddressingFeature.Responses.class,s);
} else {
return AddressingFeature.Responses.ALL;
}

}

Expand Down
Loading

0 comments on commit dfd850c

Please sign in to comment.