Search in sources :

Example 1 with JndiUrlReference

use of org.apache.openejb.core.ivm.naming.JndiUrlReference in project tomee by apache.

the class InjectionProcessor method fillInjectionProperties.

private void fillInjectionProperties(final ObjectRecipe objectRecipe) {
    if (injections == null) {
        return;
    }
    boolean usePrefix = true;
    try {
        if (beanClass != null) {
            beanClass.getConstructor();
        }
    } catch (final NoSuchMethodException e) {
        // Using constructor injection
        // xbean can't handle the prefix yet
        usePrefix = false;
    }
    Class clazz = beanClass;
    if (suppliedInstance != null) {
        clazz = suppliedInstance.getClass();
    }
    if (context != null) {
        for (final Injection injection : injections) {
            if (injection.getTarget() == null) {
                continue;
            }
            if (!injection.getTarget().isAssignableFrom(clazz)) {
                continue;
            }
            final String jndiName = injection.getJndiName();
            Object value;
            try {
                value = context.lookup(jndiName);
            } catch (final NamingException ne) {
                // some fallback
                value = bindings.get(jndiName);
                if (value instanceof SimpleReference) {
                    try {
                        value = ((SimpleReference) value).getContent();
                    } catch (final NamingException e) {
                        if (value instanceof JndiUrlReference) {
                            try {
                                value = SystemInstance.get().getComponent(ContainerSystem.class).getJNDIContext().lookup(((JndiUrlReference) value).getJndiName());
                            } catch (final NamingException e1) {
                                value = null;
                            }
                        }
                    }
                }
            }
            if (value == null) {
                // used for testing/mocking
                final FallbackPropertyInjector fallback = SystemInstance.get().getComponent(FallbackPropertyInjector.class);
                if (fallback != null) {
                    value = fallback.getValue(injection);
                }
            }
            if (value != null) {
                final String prefix;
                if (usePrefix) {
                    prefix = injection.getTarget().getName() + "/";
                } else {
                    prefix = "";
                }
                objectRecipe.setProperty(prefix + injection.getName(), value);
            } else {
                logger.warning("Injection data not found in JNDI context: jndiName='" + injection.getJndiName() + "', target=" + injection.getTarget().getName() + "/" + injection.getName());
            }
        }
    }
}
Also used : ContainerSystem(org.apache.openejb.spi.ContainerSystem) SimpleReference(org.apache.xbean.naming.reference.SimpleReference) NamingException(javax.naming.NamingException) JndiUrlReference(org.apache.openejb.core.ivm.naming.JndiUrlReference) FallbackPropertyInjector(org.apache.openejb.injection.FallbackPropertyInjector)

Aggregations

NamingException (javax.naming.NamingException)1 JndiUrlReference (org.apache.openejb.core.ivm.naming.JndiUrlReference)1 FallbackPropertyInjector (org.apache.openejb.injection.FallbackPropertyInjector)1 ContainerSystem (org.apache.openejb.spi.ContainerSystem)1 SimpleReference (org.apache.xbean.naming.reference.SimpleReference)1