Search in sources :

Example 56 with RefAddr

use of javax.naming.RefAddr 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 57 with RefAddr

use of javax.naming.RefAddr in project tomcat70 by apache.

the class DataSourceFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * <p>Create and return a new <code>BasicDataSource</code> instance.  If no
 * instance can be created, return <code>null</code> instead.</p>
 *
 * @param obj The possibly null object containing location or
 *  reference information that can be used in creating an object
 * @param name The name of this object relative to <code>nameCtx</code>
 * @param nameCtx The context relative to which the <code>name</code>
 *  parameter is specified, or <code>null</code> if <code>name</code>
 *  is relative to the default initial context
 * @param environment The possibly null environment that is used in
 *  creating this object
 *
 * @exception Exception if an exception occurs creating the instance
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // that specify a class name of "javax.sql.DataSource"
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    boolean XA = false;
    boolean ok = false;
    if ("javax.sql.DataSource".equals(ref.getClassName())) {
        ok = true;
    }
    if ("javax.sql.XADataSource".equals(ref.getClassName())) {
        ok = true;
        XA = true;
    }
    if (org.apache.tomcat.jdbc.pool.DataSource.class.getName().equals(ref.getClassName())) {
        ok = true;
    }
    if (!ok) {
        log.warn(ref.getClassName() + " is not a valid class name/type for this JNDI factory.");
        return null;
    }
    Properties properties = new Properties();
    for (int i = 0; i < ALL_PROPERTIES.length; i++) {
        String propertyName = ALL_PROPERTIES[i];
        RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            String propertyValue = ra.getContent().toString();
            properties.setProperty(propertyName, propertyValue);
        }
    }
    return createDataSource(properties, nameCtx, XA);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) Properties(java.util.Properties) DataSource(javax.sql.DataSource)

Example 58 with RefAddr

use of javax.naming.RefAddr in project activemq-artemis by apache.

the class InVMNamingContext method lookup.

@Override
public Object lookup(String name) throws NamingException {
    name = trimSlashes(name);
    int i = name.indexOf("/");
    String tok = i == -1 ? name : name.substring(0, i);
    Object value = map.get(tok);
    if (value == null) {
        throw new NameNotFoundException("Name not found: " + tok);
    }
    if (value instanceof InVMNamingContext && i != -1) {
        return ((InVMNamingContext) value).lookup(name.substring(i));
    }
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        RefAddr refAddr = ref.get("nns");
        // we only deal with references create by NonSerializableFactory
        String key = (String) refAddr.getContent();
        return NonSerializableFactory.lookup(key);
    } else {
        return value;
    }
}
Also used : RefAddr(javax.naming.RefAddr) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference)

Example 59 with RefAddr

use of javax.naming.RefAddr in project activemq-artemis by apache.

the class InVMNamingContext method lookup.

@Override
public Object lookup(String name) throws NamingException {
    name = trimSlashes(name);
    int i = name.indexOf("/");
    String tok = i == -1 ? name : name.substring(0, i);
    Object value = map.get(tok);
    if (value == null) {
        throw new NameNotFoundException("Name not found: " + tok);
    }
    if (value instanceof InVMNamingContext && i != -1) {
        return ((InVMNamingContext) value).lookup(name.substring(i));
    }
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        RefAddr refAddr = ref.get("nns");
        // we only deal with references create by NonSerializableFactory
        String key = (String) refAddr.getContent();
        return NonSerializableFactory.lookup(key);
    } else {
        return value;
    }
}
Also used : RefAddr(javax.naming.RefAddr) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference)

Example 60 with RefAddr

use of javax.naming.RefAddr in project activemq-artemis by apache.

the class InVMContext method lookup.

@Override
public Object lookup(String name) throws NamingException {
    name = trimSlashes(name);
    int i = name.indexOf("/");
    String tok = i == -1 ? name : name.substring(0, i);
    Object value = map.get(tok);
    if (value == null) {
        throw new NameNotFoundException("Name not found: " + tok);
    }
    if (value instanceof InVMContext && i != -1) {
        return ((InVMContext) value).lookup(name.substring(i));
    }
    if (value instanceof Reference) {
        Reference ref = (Reference) value;
        RefAddr refAddr = ref.get("nns");
        // we only deal with references create by NonSerializableFactory
        String key = (String) refAddr.getContent();
        return NonSerializableFactory.lookup(key);
    } else {
        return value;
    }
}
Also used : RefAddr(javax.naming.RefAddr) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference)

Aggregations

RefAddr (javax.naming.RefAddr)94 Reference (javax.naming.Reference)60 NamingException (javax.naming.NamingException)23 StringRefAddr (javax.naming.StringRefAddr)18 Properties (java.util.Properties)16 ObjectFactory (javax.naming.spi.ObjectFactory)12 InitialContext (javax.naming.InitialContext)9 Method (java.lang.reflect.Method)7 NameNotFoundException (javax.naming.NameNotFoundException)6 EjbRef (org.apache.naming.EjbRef)5 ResourceRef (org.apache.naming.ResourceRef)5 ResourceInfo (org.glassfish.resourcebase.resources.api.ResourceInfo)5 SerializableObjectRefAddr (org.glassfish.resources.naming.SerializableObjectRefAddr)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)4 ArrayList (java.util.ArrayList)4 Enumeration (java.util.Enumeration)4 DataSource (javax.sql.DataSource)4 BeanInfo (java.beans.BeanInfo)3 PropertyDescriptor (java.beans.PropertyDescriptor)3 ResourceLinkRef (org.apache.naming.ResourceLinkRef)3