Search in sources :

Example 1 with InjectionCapable

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

the class EjbBundleValidator method accept.

@Override
public void accept(BundleDescriptor descriptor) {
    this.bundleDescriptor = descriptor;
    this.application = descriptor.getApplication();
    if (descriptor instanceof EjbBundleDescriptorImpl) {
        EjbBundleDescriptorImpl ejbBundle = (EjbBundleDescriptorImpl) descriptor;
        accept(ejbBundle);
        for (EjbDescriptor anEjb : ejbBundle.getEjbs()) {
            anEjb.visit(getSubDescriptorVisitor(anEjb));
        }
        if (ejbBundle.hasRelationships()) {
            for (Iterator itr = ejbBundle.getRelationships().iterator(); itr.hasNext(); ) {
                RelationshipDescriptor rd = (RelationshipDescriptor) itr.next();
                accept(rd);
            }
        }
        for (WebService aWebService : ejbBundle.getWebServices().getWebServices()) {
            accept(aWebService);
        }
        // inject field.
        for (InjectionCapable injectable : ejbBundle.getInjectableResources(ejbBundle)) {
            accept(injectable);
        }
        super.accept(descriptor);
    } else {
        super.accept(descriptor);
    }
}
Also used : InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) WebService(com.sun.enterprise.deployment.WebService) Iterator(java.util.Iterator) RelationshipDescriptor(org.glassfish.ejb.deployment.descriptor.RelationshipDescriptor) DummyEjbDescriptor(org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor) EjbDescriptor(org.glassfish.ejb.deployment.descriptor.EjbDescriptor) EjbBundleDescriptorImpl(org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl)

Example 2 with InjectionCapable

use of com.sun.enterprise.deployment.InjectionCapable 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 3 with InjectionCapable

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

the class EjbBundleValidator method accept.

/**
 * visits an ejb descriptor
 * @param ejb descriptor
 */
@Override
public void accept(EjbDescriptor ejb) {
    // application
    if (ejb instanceof DummyEjbDescriptor) {
        throw new IllegalArgumentException(localStrings.getLocalString("enterprise.deployment.exceptionbeanbundle", "Referencing error: this bundle has no bean of name: {0}", new Object[] { ejb.getName() }));
    }
    this.ejb = ejb;
    setDOLDefault(ejb);
    computeRuntimeDefault(ejb);
    checkDependsOn(ejb);
    validateConcurrencyMetadata(ejb);
    validateStatefulTimeout(ejb);
    validatePassivationConfiguration(ejb);
    try {
        ClassLoader cl = ejb.getEjbBundleDescriptor().getClassLoader();
        Class ejbClass = cl.loadClass(ejb.getEjbClassName());
        if (Globals.getDefaultHabitat() == null) {
            return;
        }
        if (ejb instanceof EjbSessionDescriptor) {
            EjbSessionDescriptor desc = (EjbSessionDescriptor) ejb;
            if (desc.isClustered()) {
                if (!desc.isSingleton()) {
                    throw new IllegalArgumentException("Only Sinlgeton beans can be Clustered: " + desc.getName());
                }
                if (!Serializable.class.isAssignableFrom(ejbClass)) {
                    throw new IllegalStateException(String.format("Clustered Singleton %s must be Serializable", desc.getName()));
                }
                if (desc.getClusteredLockType() == DistributedLockType.LOCK) {
                    throw new IllegalStateException(String.format("Clustered Singleton %s - incompatible lock type LOCK", desc.getName()));
                }
            }
        }
        // Perform 2.x style TimedObject processing if the class
        // hasn't already been identified as a timed object.
        AnnotationTypesProvider provider = Globals.getDefaultHabitat().getService(AnnotationTypesProvider.class, "EJB");
        if (provider == null) {
            throw new RuntimeException("Cannot find AnnotationTypesProvider named 'EJB'");
        }
        if (ejb.getEjbTimeoutMethod() == null && provider.getType("javax.ejb.TimedObject").isAssignableFrom(ejbClass)) {
            MethodDescriptor timedObjectMethod = new MethodDescriptor("ejbTimeout", "TimedObject timeout method", new String[] { "javax.ejb.Timer" }, MethodDescriptor.TIMER_METHOD);
            ejb.setEjbTimeoutMethod(timedObjectMethod);
        } else if (ejb.getEjbTimeoutMethod() != null) {
            // If timeout-method was only processed from the descriptor,
            // we need to create a MethodDescriptor using the actual
            // Method object corresponding to the timeout method.  The
            // timeout method can have any access type and be anywhere
            // in the bean class hierarchy.
            MethodDescriptor timeoutMethodDescOrig = ejb.getEjbTimeoutMethod();
            MethodDescriptor timeoutMethodDesc = processTimeoutMethod(ejb, timeoutMethodDescOrig, provider, ejbClass);
            ejb.setEjbTimeoutMethod(timeoutMethodDesc);
        }
        for (ScheduledTimerDescriptor sd : ejb.getScheduledTimerDescriptors()) {
            try {
                // This method creates new schedule and attempts to calculate next timeout.
                // The second part ensures that all values that are not verified up-front
                // are also validated.
                // It does not check that such timeout date is a valid date.
                EJBTimerSchedule.isValid(sd);
            } catch (Exception e) {
                throw new RuntimeException(ejb.getName() + ": Invalid schedule " + "defined on method " + sd.getTimeoutMethod().getFormattedString() + ": " + e.getMessage());
            }
            MethodDescriptor timeoutMethodDescOrig = sd.getTimeoutMethod();
            MethodDescriptor timeoutMethodDesc = processTimeoutMethod(ejb, timeoutMethodDescOrig, provider, ejbClass);
            sd.setTimeoutMethod(timeoutMethodDesc);
        }
    } catch (Exception e) {
        RuntimeException re = new RuntimeException("Error processing EjbDescriptor");
        re.initCause(e);
        throw re;
    }
    // has to be derived from target inject method or inject field.
    for (InjectionCapable injectable : ejb.getEjbBundleDescriptor().getInjectableResources(ejb)) {
        accept(injectable);
    }
    for (Iterator itr = ejb.getEjbReferenceDescriptors().iterator(); itr.hasNext(); ) {
        EjbReference aRef = (EjbReference) itr.next();
        accept(aRef);
    }
    for (Iterator it = ejb.getResourceReferenceDescriptors().iterator(); it.hasNext(); ) {
        ResourceReferenceDescriptor next = (ResourceReferenceDescriptor) it.next();
        accept(next);
    }
    for (Iterator it = ejb.getResourceEnvReferenceDescriptors().iterator(); it.hasNext(); ) {
        ResourceEnvReferenceDescriptor next = (ResourceEnvReferenceDescriptor) it.next();
        accept(next);
    }
    for (Iterator it = ejb.getMessageDestinationReferenceDescriptors().iterator(); it.hasNext(); ) {
        MessageDestinationReferencer next = (MessageDestinationReferencer) it.next();
        accept(next);
    }
    // referencer as well.
    if (ejb.getType().equals(EjbMessageBeanDescriptor.TYPE)) {
        if (ejb instanceof MessageDestinationReferencer) {
            MessageDestinationReferencer msgDestReferencer = (MessageDestinationReferencer) ejb;
            if (msgDestReferencer.getMessageDestinationLinkName() != null) {
                accept(msgDestReferencer);
            }
        }
    }
    Set serviceRefs = ejb.getServiceReferenceDescriptors();
    for (Iterator itr = serviceRefs.iterator(); itr.hasNext(); ) {
        accept((ServiceReferenceDescriptor) itr.next());
    }
    if (ejb instanceof EjbCMPEntityDescriptor) {
        EjbCMPEntityDescriptor cmp = (EjbCMPEntityDescriptor) ejb;
        PersistenceDescriptor persistenceDesc = cmp.getPersistenceDescriptor();
        for (Iterator e = persistenceDesc.getCMPFields().iterator(); e.hasNext(); ) {
            FieldDescriptor fd = (FieldDescriptor) e.next();
            accept(fd);
        }
    }
}
Also used : Serializable(java.io.Serializable) Set(java.util.Set) DummyEjbDescriptor(org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor) ScheduledTimerDescriptor(org.glassfish.ejb.deployment.descriptor.ScheduledTimerDescriptor) MethodDescriptor(com.sun.enterprise.deployment.MethodDescriptor) FieldDescriptor(org.glassfish.ejb.deployment.descriptor.FieldDescriptor) EjbReference(com.sun.enterprise.deployment.types.EjbReference) PersistenceDescriptor(org.glassfish.ejb.deployment.descriptor.PersistenceDescriptor) MessageDestinationReferencer(com.sun.enterprise.deployment.types.MessageDestinationReferencer) InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) Iterator(java.util.Iterator) ResourceEnvReferenceDescriptor(com.sun.enterprise.deployment.ResourceEnvReferenceDescriptor) ResourceReferenceDescriptor(com.sun.enterprise.deployment.ResourceReferenceDescriptor) EjbSessionDescriptor(org.glassfish.ejb.deployment.descriptor.EjbSessionDescriptor) EjbCMPEntityDescriptor(org.glassfish.ejb.deployment.descriptor.EjbCMPEntityDescriptor) AnnotationTypesProvider(org.glassfish.internal.deployment.AnnotationTypesProvider)

Example 4 with InjectionCapable

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

the class InjectionTargetTest method check.

public Result check(Descriptor descriptor) {
    this.descriptor = descriptor;
    result = getInitializedResult();
    compName = getVerifierContext().getComponentNameConstructor();
    ClassLoader cl = getVerifierContext().getClassLoader();
    List<InjectionCapable> injectables = getInjectables(getClassName());
    for (InjectionCapable injectionCapable : injectables) {
        Set<InjectionTarget> iTargets = injectionCapable.getInjectionTargets();
        for (InjectionTarget target : iTargets) {
            try {
                if (target.isFieldInjectable()) {
                    Class classObj = Class.forName(getClassName(), false, cl);
                    Field field = classObj.getDeclaredField(target.getFieldName());
                    testMethodModifiers(field.getModifiers(), "field", field);
                }
                if (target.isMethodInjectable()) {
                    Class classObj = Class.forName(getClassName(), false, cl);
                    Method method = getInjectedMethod(classObj, target.getMethodName());
                    if (method == null)
                        continue;
                    testMethodModifiers(method.getModifiers(), "method", method);
                }
            }// ignore as it will be caught in other tests
             catch (Exception e) {
            }
        }
    }
    if (result.getStatus() != Result.FAILED) {
        addGoodDetails(result, compName);
        result.passed(smh.getLocalString(getClass().getName() + ".passed", "Valid injection method(s)."));
    }
    return result;
}
Also used : Field(java.lang.reflect.Field) InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) InjectionTarget(com.sun.enterprise.deployment.InjectionTarget) Method(java.lang.reflect.Method)

Example 5 with InjectionCapable

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

the class AppClientValidator method accept.

@Override
public void accept(BundleDescriptor descriptor) {
    if (descriptor instanceof ApplicationClientDescriptor) {
        ApplicationClientDescriptor appClientDesc = (ApplicationClientDescriptor) descriptor;
        accept(appClientDesc);
        // inject field.
        for (InjectionCapable injectable : appClientDesc.getInjectableResources(appClientDesc)) {
            accept(injectable);
        }
        super.accept(descriptor);
    }
}
Also used : InjectionCapable(com.sun.enterprise.deployment.InjectionCapable) ApplicationClientDescriptor(com.sun.enterprise.deployment.ApplicationClientDescriptor)

Aggregations

InjectionCapable (com.sun.enterprise.deployment.InjectionCapable)5 InjectionTarget (com.sun.enterprise.deployment.InjectionTarget)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 Iterator (java.util.Iterator)2 DummyEjbDescriptor (org.glassfish.ejb.deployment.descriptor.DummyEjbDescriptor)2 InjectionException (com.sun.enterprise.container.common.spi.util.InjectionException)1 ApplicationClientDescriptor (com.sun.enterprise.deployment.ApplicationClientDescriptor)1 MethodDescriptor (com.sun.enterprise.deployment.MethodDescriptor)1 ResourceEnvReferenceDescriptor (com.sun.enterprise.deployment.ResourceEnvReferenceDescriptor)1 ResourceReferenceDescriptor (com.sun.enterprise.deployment.ResourceReferenceDescriptor)1 WebService (com.sun.enterprise.deployment.WebService)1 EjbReference (com.sun.enterprise.deployment.types.EjbReference)1 MessageDestinationReferencer (com.sun.enterprise.deployment.types.MessageDestinationReferencer)1 Serializable (java.io.Serializable)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Set (java.util.Set)1 NamingException (javax.naming.NamingException)1 EjbBundleDescriptorImpl (org.glassfish.ejb.deployment.descriptor.EjbBundleDescriptorImpl)1 EjbCMPEntityDescriptor (org.glassfish.ejb.deployment.descriptor.EjbCMPEntityDescriptor)1