Search in sources :

Example 11 with InjectionTarget

use of com.sun.enterprise.deployment.InjectionTarget in project Payara by payara.

the class EntityManagerFactoryReferenceHandler method processEmfRef.

/**
 * 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.
 */
protected HandlerProcessingResult processEmfRef(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts, PersistenceUnit emfRefAn) throws AnnotationProcessorException {
    EntityManagerFactoryReferenceDescriptor[] emfRefs = null;
    if (ElementType.FIELD.equals(ainfo.getElementType())) {
        Field f = (Field) ainfo.getAnnotatedElement();
        String targetClassName = f.getDeclaringClass().getName();
        String logicalName = emfRefAn.name();
        // applying with default
        if (logicalName.equals("")) {
            logicalName = targetClassName + "/" + f.getName();
        }
        emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts);
        InjectionTarget target = new InjectionTarget();
        target.setFieldName(f.getName());
        target.setClassName(targetClassName);
        target.setMetadataSource(MetadataSource.ANNOTATION);
        for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) {
            emfRef.addInjectionTarget(target);
            if (emfRef.getName().length() == 0) {
                // a new one
                processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
            }
        }
    } else if (ElementType.METHOD.equals(ainfo.getElementType())) {
        Method m = (Method) ainfo.getAnnotatedElement();
        String targetClassName = m.getDeclaringClass().getName();
        String logicalName = emfRefAn.name();
        if (logicalName.equals("")) {
            // Derive javabean property name.
            String propertyName = getInjectionMethodPropertyName(m, ainfo);
            // prefixing with fully qualified type name
            logicalName = targetClassName + "/" + propertyName;
        }
        validateInjectionMethod(m, ainfo);
        emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts);
        InjectionTarget target = new InjectionTarget();
        target.setMethodName(m.getName());
        target.setClassName(targetClassName);
        target.setMetadataSource(MetadataSource.ANNOTATION);
        for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) {
            emfRef.addInjectionTarget(target);
            if (emfRef.getName().length() == 0) {
                // a new one
                processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
            }
        }
    } else if (ElementType.TYPE.equals(ainfo.getElementType())) {
        // name() is required for TYPE-level usage
        String logicalName = emfRefAn.name();
        if ("".equals(logicalName)) {
            log(Level.SEVERE, ainfo, localStrings.getLocalString("enterprise.deployment.annotation.handlers.nonametypelevel", "TYPE-Level annotation symbol on class must specify name."));
            return getDefaultFailedResult();
        }
        emfRefs = getEmfReferenceDescriptors(logicalName, rcContexts);
        for (EntityManagerFactoryReferenceDescriptor emfRef : emfRefs) {
            if (emfRef.getName().length() == 0) {
                // a new one
                processNewEmfRefAnnotation(emfRef, logicalName, emfRefAn);
            }
        }
    }
    return getDefaultProcessedResult();
}
Also used : Field(java.lang.reflect.Field) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) Method(java.lang.reflect.Method) EntityManagerFactoryReferenceDescriptor(com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor)

Example 12 with InjectionTarget

use of com.sun.enterprise.deployment.InjectionTarget in project Payara by payara.

the class ServiceReferenceNode method writeDeploymentDescriptor.

public Node writeDeploymentDescriptor(Node parent, ServiceReferenceDescriptor descriptor) {
    Node serviceRefNode = super.writeDescriptor(parent, WebServicesTagNames.SERVICE_REF, descriptor);
    writeDisplayableComponentInfo(serviceRefNode, descriptor);
    appendTextChild(serviceRefNode, WebServicesTagNames.SERVICE_REF_NAME, descriptor.getName());
    appendTextChild(serviceRefNode, WebServicesTagNames.SERVICE_INTERFACE, descriptor.getServiceInterface());
    appendTextChild(serviceRefNode, TagNames.LOOKUP_NAME, descriptor.getLookupName());
    appendTextChild(serviceRefNode, WebServicesTagNames.SERVICE_REF_TYPE, descriptor.getInjectionTargetType());
    appendTextChild(serviceRefNode, WebServicesTagNames.WSDL_FILE, descriptor.getWsdlFileUri());
    appendTextChild(serviceRefNode, WebServicesTagNames.JAXRPC_MAPPING_FILE, descriptor.getMappingFileUri());
    if (descriptor.hasServiceName()) {
        QName serviceName = descriptor.getServiceName();
        appendQNameChild(WebServicesTagNames.SERVICE_QNAME, serviceRefNode, serviceName.getNamespaceURI(), serviceName.getLocalPart(), descriptor.getServiceNameNamespacePrefix());
    }
    for (Iterator iter = descriptor.getPortsInfo().iterator(); iter.hasNext(); ) {
        ServiceRefPortInfo next = (ServiceRefPortInfo) iter.next();
        String sei = next.getServiceEndpointInterface();
        Node portComponentRefNode = appendChild(serviceRefNode, WebServicesTagNames.PORT_COMPONENT_REF);
        appendTextChild(portComponentRefNode, WebServicesTagNames.SERVICE_ENDPOINT_INTERFACE, sei);
        appendTextChild(portComponentRefNode, WebServicesTagNames.ENABLE_MTOM, next.getMtomEnabled());
        if (descriptor.getAddressing() != null) {
            AddressingNode adNode = new AddressingNode();
            adNode.writeDescriptor(portComponentRefNode, WebServicesTagNames.ADDRESSING, descriptor.getAddressing());
        }
        appendTextChild(portComponentRefNode, WebServicesTagNames.PORT_COMPONENT_LINK, next.getPortComponentLinkName());
    }
    WebServiceHandlerNode handlerNode = new WebServiceHandlerNode();
    handlerNode.writeWebServiceHandlers(serviceRefNode, descriptor.getHandlers());
    WebServiceHandlerChainNode handlerChainNode = new WebServiceHandlerChainNode();
    handlerChainNode.writeWebServiceHandlerChains(serviceRefNode, descriptor.getHandlerChain());
    appendTextChild(serviceRefNode, WebServicesTagNames.MAPPED_NAME, descriptor.getMappedName());
    if (descriptor.isInjectable()) {
        InjectionTargetNode ijNode = new InjectionTargetNode();
        for (InjectionTarget target : descriptor.getInjectionTargets()) {
            ijNode.writeDescriptor(serviceRefNode, TagNames.INJECTION_TARGET, target);
        }
    }
    return serviceRefNode;
}
Also used : InjectionTargetNode(com.sun.enterprise.deployment.node.InjectionTargetNode) QName(javax.xml.namespace.QName) JndiEnvRefNode(com.sun.enterprise.deployment.node.JndiEnvRefNode) InjectionTargetNode(com.sun.enterprise.deployment.node.InjectionTargetNode) Node(org.w3c.dom.Node) DisplayableComponentNode(com.sun.enterprise.deployment.node.DisplayableComponentNode) Iterator(java.util.Iterator) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) ServiceRefPortInfo(com.sun.enterprise.deployment.ServiceRefPortInfo)

Example 13 with InjectionTarget

use of com.sun.enterprise.deployment.InjectionTarget in project Payara by payara.

the class ResourceInjectorImpl method inject.

public void inject(WSWebServiceContext context, Object instance) throws WebServiceException {
    try {
        // Set proper component context
        invMgr.preInvoke(inv);
        // Injection first
        InjectionManager injManager = WebServiceContractImpl.getInstance().getInjectionManager();
        injManager.injectInstance(instance);
        // Set webservice context here
        // If the endpoint has a WebServiceContext with @Resource then
        // that has to be used
        WebServiceContextImpl wsc = null;
        WebBundleDescriptor bundle = (WebBundleDescriptor) endpoint.getBundleDescriptor();
        Iterator<ResourceReferenceDescriptor> it = bundle.getResourceReferenceDescriptors().iterator();
        while (it.hasNext()) {
            ResourceReferenceDescriptor r = it.next();
            if (r.isWebServiceContext()) {
                Iterator<InjectionTarget> iter = r.getInjectionTargets().iterator();
                boolean matchingClassFound = false;
                while (iter.hasNext()) {
                    InjectionTarget target = iter.next();
                    if (endpoint.getServletImplClass().equals(target.getClassName())) {
                        matchingClassFound = true;
                        break;
                    }
                }
                if (!matchingClassFound) {
                    continue;
                }
                try {
                    javax.naming.InitialContext ic = new javax.naming.InitialContext();
                    wsc = (WebServiceContextImpl) ic.lookup("java:comp/env/" + r.getName());
                } catch (Throwable t) {
                    // Do something here
                    if (logger.isLoggable(Level.FINE)) {
                        logger.log(Level.FINE, LogUtils.EXCEPTION_THROWN, t);
                    }
                }
                if (wsc != null) {
                    wsc.setContextDelegate(context);
                    // needed to support isUserInRole() on WSC;
                    wsc.setServletName(bundle.getWebComponentDescriptors());
                }
            }
        }
    } catch (InjectionException ie) {
        throw new WebServiceException(ie);
    } finally {
        invMgr.postInvoke(inv);
    }
}
Also used : WebServiceException(javax.xml.ws.WebServiceException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) WebBundleDescriptor(com.sun.enterprise.deployment.WebBundleDescriptor) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) ResourceReferenceDescriptor(com.sun.enterprise.deployment.ResourceReferenceDescriptor) InjectionManager(com.sun.enterprise.container.common.spi.util.InjectionManager)

Example 14 with InjectionTarget

use of com.sun.enterprise.deployment.InjectionTarget in project Payara by payara.

the class InjectionManagerImpl method _inject.

/**
 * Internal injection operation. componentId is only specified if componentId-specific lookup operation should be used.
 */
private void _inject(final Class clazz, final Object instance, String componentId, List<InjectionCapable> injectableResources) throws InjectionException {
    for (InjectionCapable next : injectableResources) {
        try {
            String lookupName = next.getComponentEnvName();
            if (!lookupName.startsWith("java:")) {
                lookupName = "java:comp/env/" + lookupName;
            }
            Object injectedValue = null;
            // do a loop here
            for (InjectionTarget target : next.getInjectionTargets()) {
                // we can just jump to the next target
                if (!clazz.getName().equals(target.getClassName()))
                    continue;
                // otherwise we might get called for the wrong module in the EAR file
                if (injectedValue == null) {
                    injectedValue = (componentId != null) ? glassfishNamingManager.lookup(componentId, lookupName) : glassfishNamingManager.getInitialContext().lookup(lookupName);
                }
                if (target.isFieldInjectable()) {
                    final Field f = getField(target, clazz);
                    if (Modifier.isStatic(f.getModifiers()) && (instance != null)) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-field", "Illegal use of static field on class that only supports instance-based injection: {0}", f));
                    }
                    if ((instance == null) && !Modifier.isStatic(f.getModifiers())) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-field-must-be-static", "Injected field: {0} on Application Client class: {1} must be declared static", f, clazz));
                    }
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-field", "Injecting dependency with logical name: {0} into field: {1} on class: {2}", next.getComponentEnvName(), f, clazz));
                    }
                    final Object value = injectedValue;
                    // allow for private/protected field access.
                    if (System.getSecurityManager() != null) {
                        java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

                            public java.lang.Object run() throws Exception {
                                f.set(instance, value);
                                return null;
                            }
                        });
                    } else {
                        f.set(instance, value);
                    }
                } else if (target.isMethodInjectable()) {
                    final Method m = getMethod(next, target, clazz);
                    if (Modifier.isStatic(m.getModifiers()) && (instance != null)) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.illegal-use-of-static-method", "Illegal use of static method on class that only supports instance-based injection: {0}", m));
                    }
                    if ((instance == null) && !Modifier.isStatic(m.getModifiers())) {
                        throw new InjectionException(localStrings.getLocalString("injection-manager.appclient-injected-method-must-be-static", "Injected method: {0} on Application Client class: {1} must be declared static", m, clazz));
                    }
                    if (_logger.isLoggable(Level.FINE)) {
                        _logger.fine(localStrings.getLocalString("injection-manager.injecting-dependency-method", "Injecting dependency with logical name: {0} into method: {1} on class: {2}", next.getComponentEnvName(), m, clazz));
                    }
                    final Object value = injectedValue;
                    if (System.getSecurityManager() != null) {
                        // Wrap actual value insertion in doPrivileged to
                        // allow for private/protected field access.
                        java.security.AccessController.doPrivileged(new java.security.PrivilegedExceptionAction() {

                            public java.lang.Object run() throws Exception {
                                m.invoke(instance, new Object[] { value });
                                return null;
                            }
                        });
                    } else {
                        m.invoke(instance, new Object[] { value });
                    }
                }
            }
        } catch (Throwable t) {
            Throwable cause = (t instanceof InvocationTargetException) ? ((InvocationTargetException) t).getCause() : t;
            String msg = localStrings.getLocalString("injection-manager.exception-to-inject", "Exception attempting to inject {0} into {1}: {2}", next, clazz, cause.getMessage());
            _logger.log(Level.FINE, msg, t);
            throw new InjectionException(msg, cause);
        }
    }
}
Also used : Method(java.lang.reflect.Method) NamingException(javax.naming.NamingException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectionException(com.sun.enterprise.container.common.spi.util.InjectionException) Field(java.lang.reflect.Field) InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget)

Example 15 with InjectionTarget

use of com.sun.enterprise.deployment.InjectionTarget in project Payara by payara.

the class EJBHandler method processEJB.

/**
 * Process a particular annotation whose 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
 * @param rcContexts an array of ResourceContainerContext
 * @param ejbAn
 * @return HandlerProcessingResult
 */
protected HandlerProcessingResult processEJB(AnnotationInfo ainfo, ResourceContainerContext[] rcContexts, EJB ejbAn) throws AnnotationProcessorException {
    EjbReferenceDescriptor[] ejbRefs = null;
    String defaultLogicalName = null;
    Class defaultBeanInterface = null;
    InjectionTarget target = null;
    if (ElementType.FIELD.equals(ainfo.getElementType())) {
        Field f = (Field) ainfo.getAnnotatedElement();
        String targetClassName = f.getDeclaringClass().getName();
        defaultLogicalName = targetClassName + "/" + f.getName();
        defaultBeanInterface = f.getType();
        target = new InjectionTarget();
        target.setClassName(targetClassName);
        target.setFieldName(f.getName());
        target.setMetadataSource(MetadataSource.ANNOTATION);
    } else if (ElementType.METHOD.equals(ainfo.getElementType())) {
        Method m = (Method) ainfo.getAnnotatedElement();
        String targetClassName = m.getDeclaringClass().getName();
        validateInjectionMethod(m, ainfo);
        // Derive javabean property name.
        String propertyName = getInjectionMethodPropertyName(m, ainfo);
        defaultLogicalName = targetClassName + "/" + propertyName;
        defaultBeanInterface = m.getParameterTypes()[0];
        target = new InjectionTarget();
        target.setClassName(targetClassName);
        target.setMethodName(m.getName());
        target.setMetadataSource(MetadataSource.ANNOTATION);
    } else if (ElementType.TYPE.equals(ainfo.getElementType())) {
        // if either of them not set, fail fast.  See issue 17284
        if (ejbAn.name().equals("") || ejbAn.beanInterface() == Object.class) {
            Class c = (Class) ainfo.getAnnotatedElement();
            AnnotationProcessorException fatalException = new AnnotationProcessorException(localStrings.getLocalString("enterprise.deployment.annotation.handlers.invalidtypelevelejb", "Invalid TYPE-level @EJB with name() = [{0}] and " + "beanInterface = [{1}] in {2}.  Each TYPE-level @EJB " + "must specify both name() and beanInterface().", new Object[] { ejbAn.name(), ejbAn.beanInterface(), c }), ainfo);
            fatalException.setFatal(true);
            throw fatalException;
        }
    } else {
        // can't happen
        return getDefaultFailedResult();
    }
    // NOTE that default value is Object.class, not null
    Class beanInterface = (ejbAn.beanInterface() == Object.class) ? defaultBeanInterface : ejbAn.beanInterface();
    String logicalName = ejbAn.name().equals("") ? defaultLogicalName : ejbAn.name();
    ejbRefs = getEjbReferenceDescriptors(logicalName, rcContexts);
    for (EjbReferenceDescriptor ejbRef : ejbRefs) {
        if (target != null)
            ejbRef.addInjectionTarget(target);
        if (// a new one
        !ok(ejbRef.getName()))
            ejbRef.setName(logicalName);
        // merge type information
        setEjbType(ejbRef, beanInterface);
        // merge description
        if (!ok(ejbRef.getDescription()) && ok(ejbAn.description()))
            ejbRef.setDescription(ejbAn.description());
        // merge lookup-name and mapped-name
        if (!ejbRef.hasLookupName() && ok(ejbAn.lookup()))
            ejbRef.setLookupName(ejbAn.lookup());
        if (!ok(ejbRef.getMappedName()) && ok(ejbAn.mappedName()))
            ejbRef.setMappedName(ejbAn.mappedName());
        // merge beanName/linkName
        if (!ok(ejbRef.getLinkName()) && ok(ejbAn.beanName()))
            ejbRef.setLinkName(ejbAn.beanName());
    }
    return getDefaultProcessedResult();
}
Also used : Field(java.lang.reflect.Field) EjbReferenceDescriptor(com.sun.enterprise.deployment.EjbReferenceDescriptor) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) Method(java.lang.reflect.Method) AnnotationProcessorException(org.glassfish.apf.AnnotationProcessorException)

Aggregations

InjectionTarget (com.sun.enterprise.deployment.InjectionTarget)24 Node (org.w3c.dom.Node)8 Field (java.lang.reflect.Field)6 Method (java.lang.reflect.Method)6 EntityManagerReferenceDescriptor (com.sun.enterprise.deployment.EntityManagerReferenceDescriptor)3 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)2 EjbReferenceDescriptor (com.sun.enterprise.deployment.EjbReferenceDescriptor)2 EntityManagerFactoryReferenceDescriptor (com.sun.enterprise.deployment.EntityManagerFactoryReferenceDescriptor)2 InjectionCapable (com.sun.enterprise.deployment.InjectionCapable)2 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)2 EjbReference (com.sun.enterprise.deployment.types.EjbReference)2 Result (com.sun.enterprise.tools.verifier.Result)2 InjectionManager (com.sun.enterprise.container.common.spi.util.InjectionManager)1 EjbDescriptor (com.sun.enterprise.deployment.EjbDescriptor)1 EjbSessionDescriptor (com.sun.enterprise.deployment.EjbSessionDescriptor)1 EnvironmentProperty (com.sun.enterprise.deployment.EnvironmentProperty)1 MessageDestinationReferenceDescriptor (com.sun.enterprise.deployment.MessageDestinationReferenceDescriptor)1 ResourceEnvReferenceDescriptor (com.sun.enterprise.deployment.ResourceEnvReferenceDescriptor)1 ServiceRefPortInfo (com.sun.enterprise.deployment.ServiceRefPortInfo)1 ServiceReferenceDescriptor (com.sun.enterprise.deployment.ServiceReferenceDescriptor)1