Search in sources :

Example 86 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class AnnotationProcessorImpl method process.

private void process(ProcessingContext ctx, AnnotationInfo element, HandlerProcessingResultImpl result) throws AnnotationProcessorException {
    Annotation annotation = element.getAnnotation();
    if (AnnotationUtils.shouldLog("annotation")) {
        logger.log(Level.FINER, "Annotation : {0} delegate = {1}", new Object[] { annotation.annotationType().getName(), delegate });
    }
    result.addResult(annotation.annotationType(), ResultType.UNPROCESSED);
    // we ignore all java.* annotations
    Package annPackage = annotation.annotationType().getPackage();
    if (annPackage != null && annPackage.getName().startsWith("java.lang"))
        return;
    List<AnnotationHandler> annotationHandlers = handlers.get(annotation.annotationType().getName());
    if (annotationHandlers != null) {
        for (AnnotationHandler handler : annotationHandlers) {
            // here we need to be careful, we are ready to invoke a handler
            // to process a particular annotation type. However, this handler
            // may have defined a list of annotations that should be processed
            // (if present on the annotated element) before itself.
            // do this check and process those annotations first.
            Class<? extends Annotation>[] dependencies = handler.getTypeDependencies();
            if (dependencies != null) {
                AnnotatedElement ae = element.getAnnotatedElement();
                for (Class<? extends Annotation> annotationType : dependencies) {
                    Annotation depAnnotation = ae.getAnnotation(annotationType);
                    if (depAnnotation != null) {
                        ResultType resultType = result.processedAnnotations().get(annotationType);
                        if (resultType == null || resultType == ResultType.UNPROCESSED) {
                            // annotation is present, process it.
                            AnnotationInfo info = new AnnotationInfo(ctx, ae, depAnnotation, getTopElementType());
                            process(ctx, info, result);
                        }
                    }
                }
            }
            // at this point, all annotation that I declared depending on
            // are processed
            HandlerProcessingResult processingResult = null;
            try {
                processingResult = handler.processAnnotation(element);
            } catch (AnnotationProcessorException ape) {
                // I am logging this exception
                log(Level.SEVERE, ape.getLocator(), ape.getMessage());
                // errors.
                if (ape.isFatal()) {
                    throw ape;
                }
                if (++errorCount > 100) {
                    throw new AnnotationProcessorException(AnnotationUtils.getLocalString("enterprise.deployment.annotation.toomanyerror", "Too many errors, annotation processing abandoned."));
                }
                processingResult = HandlerProcessingResultImpl.getDefaultResult(annotation.annotationType(), ResultType.FAILED);
            } catch (Throwable e) {
                AnnotationProcessorException ape = new AnnotationProcessorException(e.getMessage(), element);
                ape.initCause(e);
                throw ape;
            }
            result.addAll(processingResult);
        }
    } else {
        if (delegate != null) {
            delegate.process(ctx, element, result);
        } else {
            ctx.getErrorHandler().fine(new AnnotationProcessorException("No handler defined for " + annotation.annotationType()));
        }
    }
}
Also used : HandlerProcessingResult(org.glassfish.apf.HandlerProcessingResult) AnnotatedElement(java.lang.reflect.AnnotatedElement) ResultType(org.glassfish.apf.ResultType) Annotation(java.lang.annotation.Annotation) AnnotationHandler(org.glassfish.apf.AnnotationHandler) AnnotationProcessorException(org.glassfish.apf.AnnotationProcessorException) AnnotationInfo(org.glassfish.apf.AnnotationInfo)

Example 87 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class WebBundleContext method createContextForWeb.

/**
 * This method create a context for web component(s) by using
 * descriptor(s) associated to given webComponet impl class.
 * Return null if corresponding descriptor is not found.
 */
public AnnotatedElementHandler createContextForWeb() {
    AnnotatedElement anTypeElement = this.getProcessingContext().getProcessor().getLastAnnotatedElement(ElementType.TYPE);
    WebComponentDescriptor[] webComps = null;
    if (anTypeElement != null) {
        String implClassName = ((Class) anTypeElement).getName();
        webComps = getDescriptor().getWebComponentByImplName(implClassName);
    }
    AnnotatedElementHandler aeHandler = null;
    if (webComps != null && webComps.length > 1) {
        aeHandler = new WebComponentsContext(webComps);
    } else if (webComps != null && webComps.length == 1) {
        aeHandler = new WebComponentContext(webComps[0]);
    }
    if (aeHandler != null) {
        // push a WebComponent(s)Context to stack
        this.getProcessingContext().pushHandler(aeHandler);
    }
    return aeHandler;
}
Also used : WebComponentDescriptor(com.sun.enterprise.deployment.WebComponentDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElementHandler(org.glassfish.apf.AnnotatedElementHandler)

Example 88 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class MessageDrivenHandler method createEjbDescriptor.

/**
 * Create a new EjbDescriptor for a given elementName and AnnotationInfo.
 * @param elementName
 * @param ainfo
 * @return a new EjbDescriptor
 */
protected EjbDescriptor createEjbDescriptor(String elementName, AnnotationInfo ainfo) throws AnnotationProcessorException {
    AnnotatedElement ae = ainfo.getAnnotatedElement();
    EjbMessageBeanDescriptor newDescriptor = new EjbMessageBeanDescriptor();
    Class ejbClass = (Class) ae;
    newDescriptor.setName(elementName);
    newDescriptor.setEjbClassName(ejbClass.getName());
    return newDescriptor;
}
Also used : EjbMessageBeanDescriptor(org.glassfish.ejb.deployment.descriptor.EjbMessageBeanDescriptor) AnnotatedElement(java.lang.reflect.AnnotatedElement)

Example 89 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class HandlerChainHandler method processAnnotation.

public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo) throws AnnotationProcessorException {
    AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
    AnnotatedElement annElem = annInfo.getAnnotatedElement();
    Class declaringClass;
    boolean serviceSideChain = ((annElem.getAnnotation(WebService.class) != null) || (annElem.getAnnotation(WebServiceProvider.class) != null)) ? true : false;
    if (serviceSideChain) {
        declaringClass = (Class) annElem;
    } else {
        if (annInfo.getElementType().equals(ElementType.FIELD)) {
            // this is a field injection
            declaringClass = ((Field) annElem).getDeclaringClass();
        } else if (annInfo.getElementType().equals(ElementType.METHOD)) {
            // this is a method injection
            declaringClass = ((Method) annElem).getDeclaringClass();
        } else if (annInfo.getElementType().equals(ElementType.TYPE)) {
            declaringClass = (Class) annElem;
        } else {
            throw new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidtype", "annotation not allowed on this element."), annInfo);
        }
    }
    return processHandlerChainAnnotation(annInfo, annCtx, annElem, declaringClass, serviceSideChain);
}
Also used : WebServiceProvider(javax.xml.ws.WebServiceProvider) WebService(javax.jws.WebService) AnnotatedElement(java.lang.reflect.AnnotatedElement) Method(java.lang.reflect.Method)

Example 90 with AnnotatedElement

use of java.lang.reflect.AnnotatedElement in project Payara by payara.

the class WebServiceProviderHandler method processAnnotation.

@Override
public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo) throws AnnotationProcessorException {
    AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
    AnnotatedElement annElem = annInfo.getAnnotatedElement();
    boolean ejbInWar = ignoreWebserviceAnnotations(annElem, annCtx);
    // and add webservices to that BundleDescriptor
    if (ejbInWar) {
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
    }
    // sanity check
    if (!(annElem instanceof Class)) {
        AnnotationProcessorException ape = new AnnotationProcessorException("@WebServiceProvider can only be specified on TYPE", annInfo);
        annInfo.getProcessingContext().getErrorHandler().error(ape);
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
    }
    if (isJaxwsRIDeployment(annInfo)) {
        // Looks like JAX-WS RI specific deployment, do not process Web Service annotations otherwise would end up as two web service endpoints
        conLogger.log(Level.INFO, LogUtils.DEPLOYMENT_DISABLED, new Object[] { annInfo.getProcessingContext().getArchive().getName(), "WEB-INF/sun-jaxws.xml" });
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
    }
    // WebServiceProvider MUST implement the provider interface, let's check this
    if (!javax.xml.ws.Provider.class.isAssignableFrom((Class) annElem)) {
        AnnotationProcessorException ape = new AnnotationProcessorException(annElem.toString() + "does not implement the javax.xml.ws.Provider interface", annInfo);
        annInfo.getProcessingContext().getErrorHandler().error(ape);
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
    }
    // let's get the main annotation of interest.
    javax.xml.ws.WebServiceProvider ann = (javax.xml.ws.WebServiceProvider) annInfo.getAnnotation();
    BundleDescriptor bundleDesc = null;
    try {
        // let's see the type of web service we are dealing with...
        if (ejbProvider != null && ejbProvider.getType("javax.ejb.Stateless") != null) {
            // this is an ejb !
            if (annCtx instanceof EjbContext) {
                EjbContext ctx = (EjbContext) annCtx;
                bundleDesc = ctx.getDescriptor().getEjbBundleDescriptor();
                bundleDesc.setSpecVersion("3.0");
            } else if (annCtx instanceof EjbsContext) {
                String name = getEjbName(annElem);
                for (EjbContext ejbCtx : ((EjbsContext) annCtx).getEjbContexts()) {
                    EjbDescriptor descriptor = ejbCtx.getDescriptor();
                    if (name.equals(descriptor.getName())) {
                        bundleDesc = descriptor.getEjbBundleDescriptor();
                        bundleDesc.setSpecVersion("3.0");
                        break;
                    }
                }
            }
        }
        if (bundleDesc == null) {
            // this has to be a servlet
            if (annCtx instanceof WebComponentContext) {
                bundleDesc = ((WebComponentContext) annCtx).getDescriptor().getWebBundleDescriptor();
            } else if (!(annCtx instanceof WebBundleContext)) {
                return getInvalidAnnotatedElementHandlerResult(annInfo.getProcessingContext().getHandler(), annInfo);
            }
            bundleDesc = ((WebBundleContext) annCtx).getDescriptor();
            bundleDesc.setSpecVersion("2.5");
        }
    } catch (Exception e) {
        throw new AnnotationProcessorException(wsLocalStrings.getLocalString("webservice.annotation.exception", "WS00023: Exception in processing @Webservice : {0}", e.getMessage()));
    }
    // For WSProvider, portComponentName is the fully qualified class name
    String portComponentName = ((Class) annElem).getName();
    // As per JSR181, the serviceName is either specified in the deployment descriptor
    // or in @WebSErvice annotation in impl class; if neither service name implclass+Service
    String svcName = ann.serviceName();
    if (svcName == null) {
        svcName = "";
    }
    // Store binding type specified in Impl class
    String userSpecifiedBinding = null;
    javax.xml.ws.BindingType bindingAnn = (javax.xml.ws.BindingType) ((Class) annElem).getAnnotation(javax.xml.ws.BindingType.class);
    if (bindingAnn != null) {
        userSpecifiedBinding = bindingAnn.value();
    }
    // In case user gives targetNameSpace in the Impl class, that has to be used as
    // the namespace for service, port; typically user will do this in cases where
    // port_types reside in a different namespace than that of server/port.
    // Store the targetNameSpace, if any, in the impl class for later use
    String targetNameSpace = ann.targetNamespace();
    if (targetNameSpace == null) {
        targetNameSpace = "";
    }
    String portName = ann.portName();
    if (portName == null) {
        portName = "";
    }
    // Check if the same endpoint is already defined in webservices.xml
    WebServicesDescriptor wsDesc = bundleDesc.getWebServices();
    WebServiceEndpoint endpoint = wsDesc.getEndpointByName(portComponentName);
    WebService newWS;
    if (endpoint == null) {
        // If so, add this endpoint to the existing service
        if (svcName.length() != 0) {
            newWS = wsDesc.getWebServiceByName(svcName);
        } else {
            newWS = wsDesc.getWebServiceByName(((Class) annElem).getSimpleName());
        }
        if (newWS == null) {
            newWS = new WebService();
            // service name from annotation
            if (svcName.length() != 0) {
                newWS.setName(svcName);
            } else {
                newWS.setName(((Class) annElem).getSimpleName());
            }
            wsDesc.addWebService(newWS);
        }
        endpoint = new WebServiceEndpoint();
        // port-component-name is fully qualified class name
        endpoint.setEndpointName(portComponentName);
        newWS.addEndpoint(endpoint);
        wsDesc.setSpecVersion(WebServicesDescriptorNode.SPEC_VERSION);
    } else {
        newWS = endpoint.getWebService();
    }
    // present overrides everything else
    if (endpoint.getWsdlService() != null) {
        if ((targetNameSpace.length() > 0) && (!endpoint.getWsdlService().getNamespaceURI().equals(targetNameSpace))) {
            throw new AnnotationProcessorException("Target Namespace inwsdl-service element does not match @WebService.targetNamespace", annInfo);
        }
        targetNameSpace = endpoint.getWsdlService().getNamespaceURI();
    }
    // Set binding id id @BindingType is specified by the user in the impl class
    if ((!endpoint.hasUserSpecifiedProtocolBinding()) && (userSpecifiedBinding != null) && (userSpecifiedBinding.length() != 0)) {
        endpoint.setProtocolBinding(userSpecifiedBinding);
    }
    // Use annotated values only if the deployment descriptor equivalent has not been specified
    if (newWS.getWsdlFileUri() == null) {
        // take wsdl location from annotation
        if (ann.wsdlLocation() != null && ann.wsdlLocation().length() != 0) {
            newWS.setWsdlFileUri(ann.wsdlLocation());
        }
    }
    annElem = annInfo.getAnnotatedElement();
    // we checked that the endpoint implements the provider interface above
    Class clz = (Class) annElem;
    Class serviceEndpointIntf = null;
    for (Class intf : clz.getInterfaces()) {
        if (javax.xml.ws.Provider.class.isAssignableFrom(intf)) {
            serviceEndpointIntf = intf;
            break;
        }
    }
    if (serviceEndpointIntf == null) {
        endpoint.setServiceEndpointInterface("javax.xml.ws.Provider");
    } else {
        endpoint.setServiceEndpointInterface(serviceEndpointIntf.getName());
    }
    if (DOLUtils.warType().equals(bundleDesc.getModuleType())) {
        if (endpoint.getServletImplClass() == null) {
            // Set servlet impl class here
            endpoint.setServletImplClass(((Class) annElem).getName());
        }
        // Servlet link name
        WebBundleDescriptor webBundle = (WebBundleDescriptor) bundleDesc;
        if (endpoint.getWebComponentLink() == null) {
            endpoint.setWebComponentLink(portComponentName);
        }
        if (endpoint.getWebComponentImpl() == null) {
            WebComponentDescriptor webComponent = (WebComponentDescriptor) webBundle.getWebComponentByCanonicalName(endpoint.getWebComponentLink());
            if (webComponent == null) {
                // GLASSFISH-3297
                WebComponentDescriptor[] wcs = webBundle.getWebComponentByImplName(((Class) annElem).getCanonicalName());
                if (wcs.length > 0) {
                    webComponent = wcs[0];
                }
            }
            // if servlet is not known, we should add it now
            if (webComponent == null) {
                webComponent = new WebComponentDescriptorImpl();
                webComponent.setServlet(true);
                webComponent.setWebComponentImplementation(((Class) annElem).getCanonicalName());
                webComponent.setName(endpoint.getEndpointName());
                webComponent.addUrlPattern("/" + newWS.getName());
                webBundle.addWebComponentDescriptor(webComponent);
            }
            endpoint.setWebComponentImpl(webComponent);
        }
    } else {
        String name = getEjbName(annElem);
        EjbDescriptor ejb = ((EjbBundleDescriptor) bundleDesc).getEjbByName(name);
        endpoint.setEjbComponentImpl(ejb);
        ejb.setWebServiceEndpointInterfaceName(endpoint.getServiceEndpointInterface());
        if (endpoint.getEjbLink() == null) {
            endpoint.setEjbLink(ejb.getName());
        }
    }
    if (endpoint.getWsdlPort() == null) {
        endpoint.setWsdlPort(new QName(targetNameSpace, portName, "ns1"));
    }
    if (endpoint.getWsdlService() == null) {
        endpoint.setWsdlService(new QName(targetNameSpace, svcName, "ns1"));
    }
    return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
}
Also used : WebService(com.sun.enterprise.deployment.WebService) AnnotatedElement(java.lang.reflect.AnnotatedElement) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) WebServicesDescriptor(com.sun.enterprise.deployment.WebServicesDescriptor) WebBundleContext(com.sun.enterprise.deployment.annotation.context.WebBundleContext) WebComponentDescriptorImpl(org.glassfish.web.deployment.descriptor.WebComponentDescriptorImpl) QName(javax.xml.namespace.QName) EjbDescriptor(com.sun.enterprise.deployment.EjbDescriptor) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext) BundleDescriptor(com.sun.enterprise.deployment.BundleDescriptor) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) WebComponentDescriptor(com.sun.enterprise.deployment.WebComponentDescriptor) WebComponentContext(com.sun.enterprise.deployment.annotation.context.WebComponentContext) WebServiceEndpoint(com.sun.enterprise.deployment.WebServiceEndpoint) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) EjbContext(com.sun.enterprise.deployment.annotation.context.EjbContext)

Aggregations

AnnotatedElement (java.lang.reflect.AnnotatedElement)106 Method (java.lang.reflect.Method)23 Annotation (java.lang.annotation.Annotation)17 Field (java.lang.reflect.Field)17 ArrayList (java.util.ArrayList)12 Test (org.junit.Test)11 HashMap (java.util.HashMap)9 Test (org.junit.jupiter.api.Test)8 Member (java.lang.reflect.Member)7 LinkedHashSet (java.util.LinkedHashSet)7 List (java.util.List)7 Constructor (java.lang.reflect.Constructor)6 Type (java.lang.reflect.Type)6 Map (java.util.Map)6 HashSet (java.util.HashSet)5 By (org.openqa.selenium.By)5 Statement (org.junit.runners.model.Statement)4 FindBy (org.openqa.selenium.support.FindBy)4 EntityType (com.querydsl.codegen.EntityType)3 Collection (java.util.Collection)3