Search in sources :

Example 1 with InjectionTarget

use of org.apache.tomcat.util.descriptor.web.InjectionTarget in project tomcat by apache.

the class NamingResourcesImpl method addEnvironment.

/**
 * Add an environment entry for this web application.
 *
 * @param environment New environment entry
 */
@Override
public void addEnvironment(ContextEnvironment environment) {
    if (entries.contains(environment.getName())) {
        ContextEnvironment ce = findEnvironment(environment.getName());
        ContextResourceLink rl = findResourceLink(environment.getName());
        if (ce != null) {
            if (ce.getOverride()) {
                removeEnvironment(environment.getName());
            } else {
                return;
            }
        } else if (rl != null) {
            // Link. Need to look at the global resources
            NamingResourcesImpl global = getServer().getGlobalNamingResources();
            if (global.findEnvironment(rl.getGlobal()) != null) {
                if (global.findEnvironment(rl.getGlobal()).getOverride()) {
                    removeResourceLink(environment.getName());
                } else {
                    return;
                }
            }
        } else {
            // It exists but it isn't an env or a res link...
            return;
        }
    }
    List<InjectionTarget> injectionTargets = environment.getInjectionTargets();
    String value = environment.getValue();
    String lookupName = environment.getLookupName();
    // Entries with injection targets but no value are effectively ignored
    if (injectionTargets != null && injectionTargets.size() > 0 && (value == null || value.length() == 0)) {
        return;
    }
    // Entries with lookup-name and value are an error (EE.5.4.1.3)
    if (value != null && value.length() > 0 && lookupName != null && lookupName.length() > 0) {
        throw new IllegalArgumentException(sm.getString("namingResources.envEntryLookupValue", environment.getName()));
    }
    if (!checkResourceType(environment)) {
        throw new IllegalArgumentException(sm.getString("namingResources.resourceTypeFail", environment.getName(), environment.getType()));
    }
    entries.add(environment.getName());
    synchronized (envs) {
        environment.setNamingResources(this);
        envs.put(environment.getName(), environment);
    }
    support.firePropertyChange("environment", null, environment);
    // Register with JMX
    if (resourceRequireExplicitRegistration) {
        try {
            MBeanUtils.createMBean(environment);
        } catch (Exception e) {
            log.warn(sm.getString("namingResources.mbeanCreateFail", environment.getName()), e);
        }
    }
}
Also used : ContextEnvironment(org.apache.tomcat.util.descriptor.web.ContextEnvironment) ContextResourceLink(org.apache.tomcat.util.descriptor.web.ContextResourceLink) InjectionTarget(org.apache.tomcat.util.descriptor.web.InjectionTarget) NamingException(javax.naming.NamingException) LifecycleException(org.apache.catalina.LifecycleException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with InjectionTarget

use of org.apache.tomcat.util.descriptor.web.InjectionTarget in project tomcat by apache.

the class StandardContext method addInjectionTarget.

private void addInjectionTarget(Injectable resource, Map<String, Map<String, String>> injectionMap) {
    List<InjectionTarget> injectionTargets = resource.getInjectionTargets();
    if (injectionTargets != null && injectionTargets.size() > 0) {
        String jndiName = resource.getName();
        for (InjectionTarget injectionTarget : injectionTargets) {
            String clazz = injectionTarget.getTargetClass();
            Map<String, String> injections = injectionMap.get(clazz);
            if (injections == null) {
                injections = new HashMap<>();
                injectionMap.put(clazz, injections);
            }
            injections.put(injectionTarget.getTargetName(), jndiName);
        }
    }
}
Also used : InjectionTarget(org.apache.tomcat.util.descriptor.web.InjectionTarget)

Example 3 with InjectionTarget

use of org.apache.tomcat.util.descriptor.web.InjectionTarget in project tomcat by apache.

the class NamingResourcesImpl method getCompatibleType.

private Class<?> getCompatibleType(Context context, ResourceBase resource, Class<?> typeClass) {
    Class<?> result = null;
    for (InjectionTarget injectionTarget : resource.getInjectionTargets()) {
        Class<?> clazz = Introspection.loadClass(context, injectionTarget.getTargetClass());
        if (clazz == null) {
            // Can't load class - therefore ignore this target
            continue;
        }
        // Look for a match
        String targetName = injectionTarget.getTargetName();
        // Look for a setter match first
        Class<?> targetType = getSetterType(clazz, targetName);
        if (targetType == null) {
            // Try a field match if no setter match
            targetType = getFieldType(clazz, targetName);
        }
        if (targetType == null) {
            // No match - ignore this injection target
            continue;
        }
        targetType = Introspection.convertPrimitiveType(targetType);
        if (typeClass == null) {
            // Need to find a common type amongst the injection targets
            if (result == null) {
                result = targetType;
            } else if (targetType.isAssignableFrom(result)) {
            // NO-OP - This will work
            } else if (result.isAssignableFrom(targetType)) {
                // Need to use more specific type
                result = targetType;
            } else {
                // Incompatible types
                return null;
            }
        } else {
            // type
            if (targetType.isAssignableFrom(typeClass)) {
                result = typeClass;
            } else {
                // Incompatible types
                return null;
            }
        }
    }
    return result;
}
Also used : InjectionTarget(org.apache.tomcat.util.descriptor.web.InjectionTarget)

Aggregations

InjectionTarget (org.apache.tomcat.util.descriptor.web.InjectionTarget)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 NamingException (javax.naming.NamingException)1 LifecycleException (org.apache.catalina.LifecycleException)1 ContextEnvironment (org.apache.tomcat.util.descriptor.web.ContextEnvironment)1 ContextResourceLink (org.apache.tomcat.util.descriptor.web.ContextResourceLink)1