Search in sources :

Example 36 with RefAddr

use of javax.naming.RefAddr in project druid by alibaba.

the class DruidDataSourceFactory method getObjectInstance.

@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;
    if (// 
    (!"javax.sql.DataSource".equals(ref.getClassName())) && // 
    (!"com.alibaba.druid.pool.DruidDataSource".equals(ref.getClassName()))) {
        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 createDataSourceInternal(properties);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) Properties(java.util.Properties)

Example 37 with RefAddr

use of javax.naming.RefAddr in project kernel by exoplatform.

the class HikariDataSourceFactory method getObjectInstance.

/**
 * {@inheritDoc}
 */
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    if ((obj == null) || !(obj instanceof Reference)) {
        return null;
    }
    Reference ref = (Reference) obj;
    if (!"javax.sql.DataSource".equals(ref.getClassName())) {
        return null;
    }
    Properties properties = new Properties();
    for (int i = 0; i < ref.size(); i++) {
        String propertyName = ref.get(i).getType();
        RefAddr ra = ref.get(propertyName);
        if (ra != null) {
            String propertyValue = ra.getContent().toString();
            properties.setProperty(propertyName, propertyValue);
        }
    }
    return createDataSource(properties);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) Properties(java.util.Properties)

Example 38 with RefAddr

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

use of javax.naming.RefAddr in project Payara by payara.

the class EjbFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new EJB 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 EjbRef) {
        Reference ref = (Reference) obj;
        // If ejb-link has been specified, resolving the link using JNDI
        RefAddr linkRefAddr = ref.get(EjbRef.LINK);
        if (linkRefAddr != null) {
            // Retrieving the EJB link
            String ejbLink = linkRefAddr.getContent().toString();
            Object beanObj = (new InitialContext()).lookup(ejbLink);
            /*
                String homeClassName = ref.getClassName();
                try {
                    Class home = Class.forName(homeClassName);
                    if (home.isInstance(beanObj)) {
                        if (log.isDebugEnabled()) 
                            log.debug("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " implements home interface " 
                                           + home.getName());
                    } else {
                        if (log.isDebugEnabled())
                            log.debug("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " doesn't implement home interface " 
                                           + home.getName());
                        throw new NamingException
                            ("Bean of type " + beanObj.getClass().getName() 
                             + " doesn't implement home interface " 
                             + home.getName());
                    }
                } catch (ClassNotFoundException e) {
                    log.warn("Couldn't load home interface "
                                       + homeClassName, e);
                }
                */
            return beanObj;
        }
        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) {
                }
            }
        } else {
            String javaxEjbFactoryClassName = System.getProperty("javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
            try {
                factory = (ObjectFactory) Class.forName(javaxEjbFactoryClassName).newInstance();
            } catch (Throwable t) {
            }
        }
        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) EjbRef(org.apache.naming.EjbRef) NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext)

Example 40 with RefAddr

use of javax.naming.RefAddr in project Payara by payara.

the class ResourceFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new DataSource 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 ResourceRef) {
        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) {
                    throw new NamingException("Could not create resource factory, ClassNotFoundException:" + e.getMessage());
                }
            } else {
                try {
                    factoryClass = Class.forName(factoryClassName);
                } catch (ClassNotFoundException e) {
                    throw new NamingException("Could not create resource factory, ClassNotFoundException:" + e.getMessage());
                }
            }
            if (factoryClass != null) {
                try {
                    factory = (ObjectFactory) factoryClass.newInstance();
                } catch (Throwable t) {
                    if (t instanceof NamingException)
                        throw (NamingException) t;
                    throw new NamingException("Could not create resource factory instance, " + t.getMessage());
                }
            }
        } else {
            if (ref.getClassName().equals("javax.sql.DataSource")) {
                String javaxSqlDataSourceFactoryClassName = System.getProperty("javax.sql.DataSource.Factory", Constants.DBCP_DATASOURCE_FACTORY);
                try {
                    factory = (ObjectFactory) Class.forName(javaxSqlDataSourceFactoryClassName).newInstance();
                } catch (Throwable t) {
                }
            } else if (ref.getClassName().equals("javax.mail.Session")) {
                String javaxMailSessionFactoryClassName = System.getProperty("javax.mail.Session.Factory", "org.apache.naming.factory.MailSessionFactory");
                try {
                    factory = (ObjectFactory) Class.forName(javaxMailSessionFactoryClassName).newInstance();
                } catch (Throwable t) {
                }
            }
        }
        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) ResourceRef(org.apache.naming.ResourceRef) NamingException(javax.naming.NamingException)

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