Search in sources :

Example 1 with EjbsContext

use of com.sun.enterprise.deployment.annotation.context.EjbsContext in project Payara by payara.

the class WebServiceHandler method processAnnotation.

@Override
public HandlerProcessingResult processAnnotation(AnnotationInfo annInfo) throws AnnotationProcessorException {
    AnnotatedElementHandler annCtx = annInfo.getProcessingContext().getHandler();
    AnnotatedElement annElem = annInfo.getAnnotatedElement();
    AnnotatedElement origAnnElem = annElem;
    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(wsLocalStrings.getLocalString("enterprise.deployment.annotation.handlers.wrongannotationlocation", "WS00022: symbol annotation can only be specified on TYPE"), annInfo);
        annInfo.getProcessingContext().getErrorHandler().error(ape);
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
    }
    // Ignore @WebService annotation on an interface; process only those in an actual service impl class
    if (((Class) annElem).isInterface()) {
        return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
    }
    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);
    }
    // let's get the main annotation of interest.
    javax.jws.WebService ann = (javax.jws.WebService) annInfo.getAnnotation();
    BundleDescriptor bundleDesc = null;
    // Ensure that an EJB endpoint is packaged in EJBJAR and a servlet endpoint is packaged in a WAR
    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 since there is no @Servlet annotation yet
            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()));
    }
    // WebService.name in the impl class identifies port-component-name
    // If this is specified in impl class, then that takes precedence
    String portComponentName = ann.name();
    // 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 svcNameFromImplClass = ann.serviceName();
    String implClassName = ((Class) annElem).getSimpleName();
    String implClassFullName = ((Class) annElem).getName();
    // 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();
    // As per JSR181, the portName is either specified in deployment desc or in @WebService
    // in impl class; if neither, it will @WebService.name+Port; if @WebService.name is not there,
    // then port name is implClass+Port
    String portNameFromImplClass = ann.portName();
    if ((portNameFromImplClass == null) || (portNameFromImplClass.length() == 0)) {
        if ((portComponentName != null) && (portComponentName.length() != 0)) {
            portNameFromImplClass = portComponentName + "Port";
        } else {
            portNameFromImplClass = implClassName + "Port";
        }
    }
    // 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();
    }
    // Store wsdlLocation in the impl class (if any)
    String wsdlLocation = null;
    if (ann.wsdlLocation() != null && ann.wsdlLocation().length() != 0) {
        wsdlLocation = ann.wsdlLocation();
    }
    // remaining attributes should be extracted from the SEI instead of SIB.
    if (ann.endpointInterface() != null && ann.endpointInterface().length() > 0) {
        Class endpointIntf;
        try {
            endpointIntf = ((Class) annElem).getClassLoader().loadClass(ann.endpointInterface());
        } catch (java.lang.ClassNotFoundException cfne) {
            throw new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.classnotfound", "class {0} referenced from annotation symbol cannot be loaded", new Object[] { ann.endpointInterface() }), annInfo);
        }
        annElem = endpointIntf;
        ann = annElem.getAnnotation(javax.jws.WebService.class);
        if (ann == null) {
            throw new AnnotationProcessorException(wsLocalStrings.getLocalString("no.webservice.annotation", "WS00025: SEI {0} referenced from the @WebService annotation on {1}  does not contain a @WebService annotation", ((javax.jws.WebService) annInfo.getAnnotation()).endpointInterface(), ((Class) annElem).getName()));
        }
        // SEI cannot have @BindingType
        if (annElem.getAnnotation(javax.xml.ws.BindingType.class) != null) {
            throw new AnnotationProcessorException(wsLocalStrings.getLocalString("cannot.have.bindingtype", "WS00026: SEI {0} cannot have @BindingType", ((javax.jws.WebService) annInfo.getAnnotation()).endpointInterface()));
        }
    }
    WebServicesDescriptor wsDesc = bundleDesc.getWebServices();
    // unique port-component-name for this module
    if (portComponentName == null || portComponentName.length() == 0) {
        portComponentName = implClassName;
    }
    // Check if this port-component-name is unique for this module
    WebServiceEndpoint wep = wsDesc.getEndpointByName(portComponentName);
    if (wep != null) {
        // use fully qualified class name as port-component-name for the current endpoint
        if ((wep.getServiceEndpointInterface() != null) && (wep.getServiceEndpointInterface().length() != 0) && (!((Class) annElem).getName().equals(wep.getServiceEndpointInterface()))) {
            portComponentName = implClassFullName;
        }
    }
    // Check if the same endpoint is already defined in webservices.xml
    // This has to be done again after applying the 109 rules as above
    // for port-component-name
    WebServiceEndpoint endpoint = wsDesc.getEndpointByName(portComponentName);
    WebService newWS;
    if (endpoint == null) {
        if (DOLUtils.warType().equals(bundleDesc.getModuleType())) {
            // http://java.net/jira/browse/GLASSFISH-17204
            WebComponentDescriptor[] wcByImplName = ((WebBundleDescriptor) bundleDesc).getWebComponentByImplName(implClassFullName);
            for (WebComponentDescriptor wc : wcByImplName) {
                if (!wsDesc.getEndpointsImplementedBy(wc).isEmpty()) {
                    // URL mapping for annotated service exists - it can be JAX-RPC service
                    // as well as some servlet or maybe only invalid port-component-name,
                    // so let user know about possible error
                    logger.log(Level.SEVERE, LogUtils.WS_URLMAPPING_EXISTS, new Object[] { implClassFullName });
                    break;
                }
            }
        }
        // If so, add this endpoint to the existing service
        if (svcNameFromImplClass != null && svcNameFromImplClass.length() != 0) {
            newWS = wsDesc.getWebServiceByName(svcNameFromImplClass);
        } else {
            newWS = wsDesc.getWebServiceByName(implClassName + "Service");
        }
        if (newWS == null) {
            newWS = new WebService();
            // service name from annotation
            if (svcNameFromImplClass != null && svcNameFromImplClass.length() != 0) {
                newWS.setName(svcNameFromImplClass);
            } else {
                newWS.setName(implClassName + "Service");
            }
            wsDesc.addWebService(newWS);
        }
        endpoint = new WebServiceEndpoint();
        if (portComponentName != null && portComponentName.length() != 0) {
            endpoint.setEndpointName(portComponentName);
        } else {
            endpoint.setEndpointName(((Class) annElem).getName());
        }
        newWS.addEndpoint(endpoint);
        wsDesc.setSpecVersion(WebServicesDescriptorNode.SPEC_VERSION);
    } else {
        newWS = endpoint.getWebService();
    }
    // present overrides everything else
    if (endpoint.getWsdlService() != null) {
        if ((targetNameSpace != null) && (targetNameSpace.length() != 0) && (!endpoint.getWsdlService().getNamespaceURI().equals(targetNameSpace))) {
            AnnotationProcessorException ape = new AnnotationProcessorException(wsLocalStrings.getLocalString("mismatch.targetnamespace", "WS00027: Target Namespace in wsdl-service element does not match @WebService.targetNamespace"), annInfo);
            annInfo.getProcessingContext().getErrorHandler().error(ape);
            return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
        }
        targetNameSpace = endpoint.getWsdlService().getNamespaceURI();
    }
    // Service and port should reside in the same namespace - assert that
    if ((endpoint.getWsdlService() != null) && (endpoint.getWsdlPort() != null)) {
        if (!endpoint.getWsdlService().getNamespaceURI().equals(endpoint.getWsdlPort().getNamespaceURI())) {
            AnnotationProcessorException ape = new AnnotationProcessorException(wsLocalStrings.getLocalString("mismatch.port.targetnamespace", "WS00028: Target Namespace for wsdl-service and wsdl-port should be the same"), annInfo);
            annInfo.getProcessingContext().getErrorHandler().error(ape);
            return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.FAILED);
        }
    }
    // Precedence given for wsdlLocation in impl class
    if (newWS.getWsdlFileUri() == null) {
        if (wsdlLocation != null) {
            newWS.setWsdlFileUri(wsdlLocation);
        } else {
            if (ann.wsdlLocation() != null && ann.wsdlLocation().length() != 0) {
                newWS.setWsdlFileUri(ann.wsdlLocation());
            }
        }
    }
    // 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);
    }
    if (endpoint.getServiceEndpointInterface() == null) {
        // take SEI from annotation
        if (ann.endpointInterface() != null && ann.endpointInterface().length() != 0) {
            endpoint.setServiceEndpointInterface(ann.endpointInterface());
        } else {
            endpoint.setServiceEndpointInterface(((Class) annElem).getName());
        }
    }
    // at this point the SIB has to be used no matter what @WebService was used.
    annElem = annInfo.getAnnotatedElement();
    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) {
            // <servlet-link> = fully qualified name of the implementation class
            endpoint.setWebComponentLink(implClassFullName);
        }
        if (endpoint.getWebComponentImpl() == null) {
            WebComponentDescriptor webComponent = (WebComponentDescriptor) webBundle.getWebComponentByCanonicalName(endpoint.getWebComponentLink());
            if (webComponent == null) {
                // GLASSFISH-3297
                WebComponentDescriptor[] wcs = webBundle.getWebComponentByImplName(implClassFullName);
                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 {
        // TODO BM handle stateless
        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) {
        // during wsgen phase
        if (targetNameSpace == null || targetNameSpace.length() == 0) {
            // the reverse order prepended with http://
            if (((Class) annElem).getPackage() != null) {
                StringTokenizer tokens = new StringTokenizer(((Class) annElem).getPackage().getName(), ".", false);
                if (tokens.hasMoreElements()) {
                    while (tokens.hasMoreElements()) {
                        if (targetNameSpace == null || targetNameSpace.length() == 0) {
                            targetNameSpace = tokens.nextElement().toString();
                        } else {
                            targetNameSpace = tokens.nextElement().toString() + "." + targetNameSpace;
                        }
                    }
                } else {
                    targetNameSpace = ((Class) annElem).getPackage().getName();
                }
            } else {
                throw new AnnotationProcessorException(wsLocalStrings.getLocalString("missing.targetnamespace", "WS00029: The javax.jws.WebService annotation targetNamespace must be used for classes or interfaces that are in no package"));
            }
            targetNameSpace = "http://" + (targetNameSpace == null ? "" : targetNameSpace + "/");
        }
        // WebService.portName = wsdl-port
        endpoint.setWsdlPort(new QName(targetNameSpace, portNameFromImplClass, "ns1"));
    }
    if (endpoint.getWsdlService() == null) {
        // Set wsdl-service properly; namespace is the same as that of wsdl port;
        // service name derived from deployment desc / annotation / default
        String serviceNameSpace = endpoint.getWsdlPort().getNamespaceURI();
        String serviceName;
        if ((svcNameFromImplClass != null) && (svcNameFromImplClass.length() != 0)) {
            // Use the serviceName annotation if available
            serviceName = svcNameFromImplClass;
        } else {
            serviceName = newWS.getName();
        }
        endpoint.setWsdlService(new QName(serviceNameSpace, serviceName, "ns1"));
    }
    // have @HandlerChain but the SEI has one specified through JAXWS customization
    if ((((Class) origAnnElem).getAnnotation(javax.jws.HandlerChain.class)) == null) {
        return (new HandlerChainHandler()).processHandlerChainAnnotation(annInfo, annCtx, origAnnElem, (Class) origAnnElem, true);
    }
    return HandlerProcessingResultImpl.getDefaultResult(getAnnotationType(), ResultType.PROCESSED);
}
Also used : AnnotatedElement(java.lang.reflect.AnnotatedElement) WebBundleContext(com.sun.enterprise.deployment.annotation.context.WebBundleContext) WebComponentDescriptorImpl(org.glassfish.web.deployment.descriptor.WebComponentDescriptorImpl) QName(javax.xml.namespace.QName) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext) StringTokenizer(java.util.StringTokenizer) WebComponentContext(com.sun.enterprise.deployment.annotation.context.WebComponentContext) EjbContext(com.sun.enterprise.deployment.annotation.context.EjbContext)

Example 2 with EjbsContext

use of com.sun.enterprise.deployment.annotation.context.EjbsContext in project Payara by payara.

the class AbstractResourceHandler method processAnnotation.

/**
 * Process a particular annotation which type is the same as the
 * one returned by @see getAnnotationType(). All information
 * pertinent to the annotation and its context is encapsulated
 * in the passed AnnotationInfo instance.
 *
 * @param ainfo the annotation information
 * @return
 * @throws AnnotationProcessorException
 */
@Override
public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo) throws AnnotationProcessorException {
    AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
    if (aeHandler instanceof EjbBundleContext) {
        EjbBundleContext ejbBundleContext = (EjbBundleContext) aeHandler;
        aeHandler = ejbBundleContext.createContextForEjb();
        if (aeHandler == null) {
            aeHandler = ejbBundleContext.createContextForEjbInterceptor();
        }
        // If it's still null and we're in an ejb-jar, use the EjbBundleContext.
        // This way we process dependencies on any classes (other than ejbs ,
        // interceptors , and their super-classes) that have annotations in case
        // we need the info for managed classes we wouldn't normally know about
        // (e.g. 299 classes).   In a .war, those are already processed during the
        // .war annotation scanning.
        EjbBundleDescriptor bundleDesc = ejbBundleContext.getDescriptor();
        RootDeploymentDescriptor enclosingBundle = bundleDesc.getModuleDescriptor().getDescriptor();
        boolean ejbJar = enclosingBundle instanceof EjbBundleDescriptor;
        if ((aeHandler == null) && ejbJar) {
            aeHandler = ejbBundleContext;
        }
    }
    if (aeHandler == null) {
        // not an ejb, interceptor in ejbBundle
        return getInvalidAnnotatedElementHandlerResult(ainfo.getProcessingContext().getHandler(), ainfo);
    }
    ResourceContainerContext[] rcContexts = null;
    if (aeHandler instanceof EjbsContext) {
        EjbsContext ejbsContext = (EjbsContext) aeHandler;
        rcContexts = (ResourceContainerContext[]) ejbsContext.getEjbContexts();
    } else if (aeHandler instanceof WebComponentsContext) {
        WebComponentsContext webCompsContext = (WebComponentsContext) aeHandler;
        rcContexts = (ResourceContainerContext[]) webCompsContext.getWebComponentContexts();
    } else if (aeHandler instanceof ResourceContainerContext) {
        rcContexts = new ResourceContainerContext[] { (ResourceContainerContext) aeHandler };
    } else {
        return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
    }
    return processAnnotation(ainfo, rcContexts);
}
Also used : EjbBundleContext(com.sun.enterprise.deployment.annotation.context.EjbBundleContext) ResourceContainerContext(com.sun.enterprise.deployment.annotation.context.ResourceContainerContext) WebComponentsContext(com.sun.enterprise.deployment.annotation.context.WebComponentsContext) EjbBundleDescriptor(com.sun.enterprise.deployment.EjbBundleDescriptor) RootDeploymentDescriptor(org.glassfish.deployment.common.RootDeploymentDescriptor) AnnotatedElementHandler(org.glassfish.apf.AnnotatedElementHandler) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext)

Example 3 with EjbsContext

use of com.sun.enterprise.deployment.annotation.context.EjbsContext in project Payara by payara.

the class AbstractAttributeHandler method processAnnotation.

/**
 * Process a particular annotation which type is the same as the
 * one returned by @see getAnnotationType(). All information
 * pertinent to the annotation and its context is encapsulated
 * in the passed AnnotationInfo instance.
 * This is a method in interface AnnotationHandler.
 *
 * @param ainfo the annotation information
 */
public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo) throws AnnotationProcessorException {
    AnnotatedElement ae = ainfo.getAnnotatedElement();
    Annotation annotation = ainfo.getAnnotation();
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("@process annotation " + annotation + " in " + ae);
    }
    AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
    if (aeHandler instanceof EjbBundleContext) {
        EjbBundleContext ejbBundleContext = (EjbBundleContext) aeHandler;
        AnnotatedElementHandler aeh = ejbBundleContext.createContextForEjb();
        if (aeh != null) {
            aeHandler = aeh;
        } else {
            if (isDelegatee()) {
                aeHandler = ejbBundleContext.createContextForEjbInterceptor();
            }
            if (aeHandler == null) {
                return getInvalidAnnotatedElementHandlerResult(null, ainfo);
            }
        }
    }
    if (!supportTypeInheritance() && ElementType.TYPE.equals(ainfo.getElementType()) && aeHandler instanceof ComponentContext) {
        ComponentContext context = (ComponentContext) aeHandler;
        Class clazz = (Class) ainfo.getAnnotatedElement();
        if (!clazz.getName().equals(context.getComponentClassName())) {
            if (logger.isLoggable(Level.WARNING)) {
                log(Level.WARNING, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.typeinhernotsupp", "The annotation symbol inheritance is not supported."));
            }
            return getDefaultProcessedResult();
        }
    }
    EjbContext[] ejbContexts = null;
    EjbInterceptorContext ejbInterceptorContext = null;
    if (aeHandler instanceof EjbContext) {
        EjbContext ejbContext = (EjbContext) aeHandler;
        ejbContexts = new EjbContext[] { ejbContext };
    } else if (aeHandler instanceof EjbsContext) {
        ejbContexts = ((EjbsContext) aeHandler).getEjbContexts();
    } else if (isDelegatee() && aeHandler instanceof EjbInterceptorContext) {
        ejbInterceptorContext = (EjbInterceptorContext) aeHandler;
    } else {
        return getInvalidAnnotatedElementHandlerResult(aeHandler, ainfo);
    }
    HandlerProcessingResult procResult = null;
    if (ejbInterceptorContext != null) {
        procResult = processAnnotation(ainfo, ejbInterceptorContext);
    } else {
        procResult = processAnnotation(ainfo, ejbContexts);
    }
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("New annotation for " + annotation);
    }
    return procResult;
}
Also used : EjbBundleContext(com.sun.enterprise.deployment.annotation.context.EjbBundleContext) ComponentContext(com.sun.enterprise.deployment.annotation.context.ComponentContext) EjbInterceptorContext(com.sun.enterprise.deployment.annotation.context.EjbInterceptorContext) EjbContext(com.sun.enterprise.deployment.annotation.context.EjbContext) HandlerProcessingResult(org.glassfish.apf.HandlerProcessingResult) AnnotatedElement(java.lang.reflect.AnnotatedElement) AnnotatedElementHandler(org.glassfish.apf.AnnotatedElementHandler) Annotation(java.lang.annotation.Annotation) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext)

Example 4 with EjbsContext

use of com.sun.enterprise.deployment.annotation.context.EjbsContext in project Payara by payara.

the class AbstractEjbHandler method processAnnotation.

/**
 * Process a particular annotation which type is the same as the
 * one returned by @see getAnnotationType(). All information
 * pertinent to the annotation and its context is encapsulated
 * in the passed AnnotationInfo instance.
 * This is a method in interface AnnotationHandler.
 *
 * @param ainfo the annotation information
 */
public HandlerProcessingResult processAnnotation(AnnotationInfo ainfo) throws AnnotationProcessorException {
    Class ejbClass = (Class) ainfo.getAnnotatedElement();
    Annotation annotation = ainfo.getAnnotation();
    if (logger.isLoggable(Level.FINER)) {
        logger.finer("@ process ejb annotation " + annotation + " in " + ejbClass);
    }
    AnnotatedElementHandler aeHandler = ainfo.getProcessingContext().getHandler();
    if (aeHandler != null && aeHandler instanceof EjbContext) {
        EjbContext context = (EjbContext) aeHandler;
        EjbDescriptor desc = (EjbDescriptor) context.getDescriptor();
        if (isValidEjbDescriptor(desc, annotation)) {
            return getDefaultProcessedResult();
        } else {
            log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.notcompsuperclass", "The annotation symbol defined in super-class is not compatible with {0} ejb {1}.", new Object[] { desc.getType(), desc.getName() }));
            return getDefaultFailedResult();
        }
    } else if (aeHandler == null || !(aeHandler instanceof EjbBundleContext)) {
        return getInvalidAnnotatedElementHandlerResult(ainfo.getProcessingContext().getHandler(), ainfo);
    }
    EjbBundleContext ctx = (EjbBundleContext) aeHandler;
    if (logger.isLoggable(Level.FINE)) {
        logger.fine("My context is " + ctx);
    }
    String elementName = getAnnotatedName(annotation);
    if (elementName.length() == 0) {
        elementName = ejbClass.getSimpleName();
    } else {
        elementName = (String) TranslatedConfigView.getTranslatedValue(elementName);
    }
    EjbBundleDescriptorImpl currentBundle = (EjbBundleDescriptorImpl) ctx.getDescriptor();
    EjbDescriptor ejbDesc = null;
    try {
        ejbDesc = currentBundle.getEjbByName(elementName);
    } catch (IllegalArgumentException ex) {
    // getEjbByName throws IllegalArgumentException when no ejb is found
    }
    if (ejbDesc != null && !(ejbDesc instanceof DummyEjbDescriptor)) {
        // overriding rules applies
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Overriding rules apply for " + ejbClass.getName());
        }
        // don't allow ejb-jar.xml overwrite ejb type
        if (!isValidEjbDescriptor(ejbDesc, annotation)) {
            // this is an error
            log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.wrongejbtype", "Wrong annotation symbol for ejb {0}", new Object[] { ejbDesc }));
            return getDefaultFailedResult();
        }
        // <ejb-class> is optional if a component-defining
        // annotation is used.  If present, <ejb-class> element
        // must match the class on which the component defining annotation
        // appears.
        String descriptorEjbClass = ejbDesc.getEjbClassName();
        if (descriptorEjbClass == null) {
            ejbDesc.setEjbClassName(ejbClass.getName());
            ejbDesc.applyDefaultClassToLifecycleMethods();
        } else if (!descriptorEjbClass.equals(ejbClass.getName())) {
            log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.ejbclsmismatch", "", new Object[] { descriptorEjbClass, elementName, ejbClass.getName() }));
            return getDefaultFailedResult();
        }
    } else {
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("Creating a new descriptor for " + ejbClass.getName());
        }
        EjbDescriptor dummyEjbDesc = ejbDesc;
        ejbDesc = createEjbDescriptor(elementName, ainfo);
        // the information from dummy ejb descriptor if applicable
        if (dummyEjbDesc != null) {
            currentBundle.removeEjb(dummyEjbDesc);
            ejbDesc.addEjbDescriptor(dummyEjbDesc);
            // reset ejbClassName on ejbDesc
            ejbDesc.setEjbClassName(ejbClass.getName());
        }
        // add the actual ejb descriptor to the ejb bundle
        currentBundle.addEjb(ejbDesc);
        if (logger.isLoggable(Level.FINE)) {
            logger.fine("New " + getAnnotationType().getName() + " bean " + elementName);
        }
    }
    // We need to include all ejbs of the same name in the annotation processing context
    // in order to handle the case that a bean class has both a component-defining
    // annotation and there are other ejb-jar.xml-defined beans with the same bean class.
    EjbDescriptor[] ejbDescs = currentBundle.getEjbByClassName(ejbClass.getName());
    HandlerProcessingResult procResult = null;
    for (EjbDescriptor next : ejbDescs) {
        procResult = setEjbDescriptorInfo(next, ainfo);
        doTimedObjectProcessing(ejbClass, next);
    }
    AnnotationContext annContext = null;
    if (ejbDescs.length == 1) {
        annContext = new EjbContext(ejbDesc, ejbClass);
    } else {
        annContext = new EjbsContext(ejbDescs, ejbClass);
    }
    // we push the new context on the stack...
    ctx.getProcessingContext().pushHandler(annContext);
    return procResult;
}
Also used : DummyEjbDescriptor(org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor) HandlerProcessingResult(org.glassfish.apf.HandlerProcessingResult) AnnotationContext(org.glassfish.apf.context.AnnotationContext) Annotation(java.lang.annotation.Annotation) DummyEjbDescriptor(org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor) EjbDescriptor(org.glassfish.ejb.deployment.descriptor.EjbDescriptor) EjbsContext(com.sun.enterprise.deployment.annotation.context.EjbsContext) EjbBundleContext(com.sun.enterprise.deployment.annotation.context.EjbBundleContext) EjbContext(com.sun.enterprise.deployment.annotation.context.EjbContext) AnnotatedElementHandler(org.glassfish.apf.AnnotatedElementHandler) EjbBundleDescriptorImpl(org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl)

Example 5 with EjbsContext

use of com.sun.enterprise.deployment.annotation.context.EjbsContext 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

EjbsContext (com.sun.enterprise.deployment.annotation.context.EjbsContext)5 EjbContext (com.sun.enterprise.deployment.annotation.context.EjbContext)4 EjbBundleContext (com.sun.enterprise.deployment.annotation.context.EjbBundleContext)3 AnnotatedElement (java.lang.reflect.AnnotatedElement)3 AnnotatedElementHandler (org.glassfish.apf.AnnotatedElementHandler)3 EjbBundleDescriptor (com.sun.enterprise.deployment.EjbBundleDescriptor)2 WebBundleContext (com.sun.enterprise.deployment.annotation.context.WebBundleContext)2 WebComponentContext (com.sun.enterprise.deployment.annotation.context.WebComponentContext)2 Annotation (java.lang.annotation.Annotation)2 QName (javax.xml.namespace.QName)2 HandlerProcessingResult (org.glassfish.apf.HandlerProcessingResult)2 WebComponentDescriptorImpl (org.glassfish.web.deployment.descriptor.WebComponentDescriptorImpl)2 BundleDescriptor (com.sun.enterprise.deployment.BundleDescriptor)1 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)1 WebBundleDescriptor (com.sun.enterprise.deployment.WebBundleDescriptor)1 WebComponentDescriptor (com.sun.enterprise.deployment.WebComponentDescriptor)1 WebService (com.sun.enterprise.deployment.WebService)1 WebServiceEndpoint (com.sun.enterprise.deployment.WebServiceEndpoint)1 WebServicesDescriptor (com.sun.enterprise.deployment.WebServicesDescriptor)1 ComponentContext (com.sun.enterprise.deployment.annotation.context.ComponentContext)1