Search in sources :

Example 1 with ResourceEnvRef

use of org.apache.naming.ResourceEnvRef in project tomee by apache.

the class TomcatWebAppBuilder method createReference.

private static Reference createReference(final ResourceBase resource) {
    final Reference ref;
    if (resource instanceof ContextResource) {
        final ContextResource cr = (ContextResource) resource;
        ref = new ResourceRef(resource.getType(), resource.getDescription(), cr.getScope(), cr.getAuth(), cr.getSingleton());
    } else {
        ref = new ResourceEnvRef(resource.getType());
    }
    final Iterator<String> params = resource.listProperties();
    while (params.hasNext()) {
        final String paramName = params.next();
        final String paramValue = (String) resource.getProperty(paramName);
        final StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        ref.add(refAddr);
    }
    return ref;
}
Also used : StringRefAddr(javax.naming.StringRefAddr) SystemComponentReference(org.apache.openejb.core.ivm.naming.SystemComponentReference) Reference(javax.naming.Reference) AtomicReference(java.util.concurrent.atomic.AtomicReference) ResourceRef(org.apache.naming.ResourceRef) ResourceEnvRef(org.apache.naming.ResourceEnvRef) ContextResource(org.apache.tomcat.util.descriptor.web.ContextResource)

Example 2 with ResourceEnvRef

use of org.apache.naming.ResourceEnvRef in project tomcat by apache.

the class NamingContextListener method addResourceEnvRef.

/**
 * Set the specified resources in the naming context.
 *
 * @param resourceEnvRef the resource reference
 */
public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {
    Reference ref = lookForLookupRef(resourceEnvRef);
    if (ref == null) {
        // Create a reference to the resource env.
        ref = new ResourceEnvRef(resourceEnvRef.getType());
        // Adding the additional parameters, if any
        Iterator<String> params = resourceEnvRef.listProperties();
        while (params.hasNext()) {
            String paramName = params.next();
            String paramValue = (String) resourceEnvRef.getProperty(paramName);
            StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
            ref.add(refAddr);
        }
    }
    try {
        if (log.isDebugEnabled()) {
            log.debug(sm.getString("naming.addResourceEnvRef", resourceEnvRef.getName()));
        }
        createSubcontexts(envCtx, resourceEnvRef.getName());
        envCtx.bind(resourceEnvRef.getName(), ref);
    } catch (NamingException e) {
        log.error(sm.getString("naming.bindFailed", e));
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) ResourceEnvRef(org.apache.naming.ResourceEnvRef) ContextResourceEnvRef(org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)

Example 3 with ResourceEnvRef

use of org.apache.naming.ResourceEnvRef in project Payara by payara.

the class ResourceEnvFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new Resource env instance.
 *
 * @param obj The reference object describing the DataSource
 */
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof ResourceEnvRef) {
        Reference ref = (Reference) obj;
        ObjectFactory factory = null;
        RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
        if (factoryRefAddr != null) {
            // Using the specified factory
            String factoryClassName = factoryRefAddr.getContent().toString();
            // Loading factory
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            Class<?> factoryClass = null;
            if (tcl != null) {
                try {
                    factoryClass = tcl.loadClass(factoryClassName);
                } catch (ClassNotFoundException e) {
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Throwable t) {
                }
            }
        }
        // Note: No defaults here
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) ResourceEnvRef(org.apache.naming.ResourceEnvRef)

Example 4 with ResourceEnvRef

use of org.apache.naming.ResourceEnvRef in project tomcat70 by apache.

the class ResourceEnvFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new Resource env instance.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if (obj instanceof ResourceEnvRef) {
        Reference ref = (Reference) obj;
        ObjectFactory factory = null;
        RefAddr factoryRefAddr = ref.get(Constants.FACTORY);
        if (factoryRefAddr != null) {
            // Using the specified factory
            String factoryClassName = factoryRefAddr.getContent().toString();
            // Loading factory
            ClassLoader tcl = Thread.currentThread().getContextClassLoader();
            Class<?> factoryClass = null;
            if (tcl != null) {
                try {
                    factoryClass = tcl.loadClass(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(e);
                    throw ex;
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Throwable t) {
                    if (t instanceof NamingException)
                        throw (NamingException) t;
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(t);
                    throw ex;
                }
            }
        }
        // Note: No defaults here
        if (factory != null) {
            return factory.getObjectInstance(obj, name, nameCtx, environment);
        } else {
            throw new NamingException("Cannot create resource instance");
        }
    }
    return null;
}
Also used : RefAddr(javax.naming.RefAddr) ObjectFactory(javax.naming.spi.ObjectFactory) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) ResourceEnvRef(org.apache.naming.ResourceEnvRef)

Example 5 with ResourceEnvRef

use of org.apache.naming.ResourceEnvRef in project tomcat70 by apache.

the class NamingContextListener method addResourceEnvRef.

/**
 * Set the specified resources in the naming context.
 */
public void addResourceEnvRef(ContextResourceEnvRef resourceEnvRef) {
    // Create a reference to the resource env.
    Reference ref = new ResourceEnvRef(resourceEnvRef.getType());
    // Adding the additional parameters, if any
    Iterator<String> params = resourceEnvRef.listProperties();
    while (params.hasNext()) {
        String paramName = params.next();
        String paramValue = (String) resourceEnvRef.getProperty(paramName);
        StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        ref.add(refAddr);
    }
    try {
        if (logger.isDebugEnabled())
            log.debug("  Adding resource env ref " + resourceEnvRef.getName());
        createSubcontexts(envCtx, resourceEnvRef.getName());
        envCtx.bind(resourceEnvRef.getName(), ref);
    } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException) ResourceEnvRef(org.apache.naming.ResourceEnvRef) ContextResourceEnvRef(org.apache.catalina.deploy.ContextResourceEnvRef)

Aggregations

Reference (javax.naming.Reference)5 ResourceEnvRef (org.apache.naming.ResourceEnvRef)5 NamingException (javax.naming.NamingException)4 StringRefAddr (javax.naming.StringRefAddr)3 RefAddr (javax.naming.RefAddr)2 ObjectFactory (javax.naming.spi.ObjectFactory)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 ContextResourceEnvRef (org.apache.catalina.deploy.ContextResourceEnvRef)1 ResourceRef (org.apache.naming.ResourceRef)1 SystemComponentReference (org.apache.openejb.core.ivm.naming.SystemComponentReference)1 ContextResource (org.apache.tomcat.util.descriptor.web.ContextResource)1 ContextResourceEnvRef (org.apache.tomcat.util.descriptor.web.ContextResourceEnvRef)1