Search in sources :

Example 26 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project aries by apache.

the class ContextHelper method getURLObjectFactory.

public static ServicePair<ObjectFactory> getURLObjectFactory(final BundleContext ctx, String urlScheme, Hashtable<?, ?> environment) throws NamingException {
    ServicePair<ObjectFactory> result = null;
    ServiceReference<ObjectFactory> ref = Activator.getUrlFactory(urlScheme);
    if (ref == null) {
        Collection<ServiceReference<URLObjectFactoryFinder>> refs = Activator.getURLObjectFactoryFinderServices();
        for (final ServiceReference<URLObjectFactoryFinder> finderRef : refs) {
            URLObjectFactoryFinder finder = Activator.getService(ctx, finderRef);
            if (finder != null) {
                ObjectFactory f = finder.findFactory(urlScheme, environment);
                if (f != null) {
                    result = new ServicePair<>(ctx, finderRef, f);
                    break;
                }
            }
        }
    } else {
        result = new ServicePair<>(ctx, ref);
    }
    return result;
}
Also used : ObjectFactory(javax.naming.spi.ObjectFactory) URLObjectFactoryFinder(org.apache.aries.jndi.urls.URLObjectFactoryFinder) ServiceReference(org.osgi.framework.ServiceReference)

Example 27 with ObjectFactory

use of javax.naming.spi.ObjectFactory 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 28 with ObjectFactory

use of javax.naming.spi.ObjectFactory 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 29 with ObjectFactory

use of javax.naming.spi.ObjectFactory 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)

Example 30 with ObjectFactory

use of javax.naming.spi.ObjectFactory in project Payara by payara.

the class TransactionFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Crete a new User transaction 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 TransactionRef) {
        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) {
                }
            }
        }
        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) TransactionRef(org.apache.naming.TransactionRef) NamingException(javax.naming.NamingException)

Aggregations

ObjectFactory (javax.naming.spi.ObjectFactory)59 Reference (javax.naming.Reference)22 NamingException (javax.naming.NamingException)21 Test (org.junit.Test)16 DirObjectFactory (javax.naming.spi.DirObjectFactory)14 RefAddr (javax.naming.RefAddr)12 Hashtable (java.util.Hashtable)10 BundleContext (org.osgi.framework.BundleContext)9 Context (javax.naming.Context)7 MethodCall (org.apache.aries.unittest.mocks.MethodCall)6 ServiceAwareObjectFactory (org.jboss.as.naming.ServiceAwareObjectFactory)6 Name (javax.naming.Name)5 Referenceable (javax.naming.Referenceable)5 ServiceReference (org.osgi.framework.ServiceReference)5 InitialContext (javax.naming.InitialContext)4 StringRefAddr (javax.naming.StringRefAddr)4 InitialLdapContext (javax.naming.ldap.InitialLdapContext)4 LdapContext (javax.naming.ldap.LdapContext)4 Properties (java.util.Properties)3 ObjectFactoryBuilder (javax.naming.spi.ObjectFactoryBuilder)3