Search in sources :

Example 1 with EjbRef

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

the class EjbFactory method getObjectInstance.

// ----------------------------------------------------------- Constructors
// -------------------------------------------------------------- Constants
// ----------------------------------------------------- Instance Variables
// --------------------------------------------------------- Public Methods
// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB 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 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)) {
                        System.out.println("Bean of type " 
                                           + beanObj.getClass().getName() 
                                           + " implements home interface " 
                                           + home.getName());
                    } else {
                        System.out.println("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) {
                    System.out.println("Couldn't load home interface "
                                       + homeClassName);
                }
                */
            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) {
                    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) {
                    NamingException ex = new NamingException("Could not load resource factory class");
                    ex.initCause(t);
                    throw ex;
                }
            }
        } else {
            String javaxEjbFactoryClassName = System.getProperty("javax.ejb.Factory", Constants.OPENEJB_EJB_FACTORY);
            try {
                factory = (ObjectFactory) Class.forName(javaxEjbFactoryClassName).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;
            }
        }
        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 2 with EjbRef

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

the class OpenEjbFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB instance using OpenEJB.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object beanObj = null;
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }
    }
    return beanObj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Example 3 with EjbRef

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

the class NamingContextListener method addEjb.

/**
 * Set the specified EJBs in the naming context.
 */
public void addEjb(ContextEjb ejb) {
    // Create a reference to the EJB.
    Reference ref = new EjbRef(ejb.getType(), ejb.getHome(), ejb.getRemote(), ejb.getLink());
    // Adding the additional parameters, if any
    Iterator<String> params = ejb.listProperties();
    while (params.hasNext()) {
        String paramName = params.next();
        String paramValue = (String) ejb.getProperty(paramName);
        StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        ref.add(refAddr);
    }
    try {
        createSubcontexts(envCtx, ejb.getName());
        envCtx.bind(ejb.getName(), ref);
    } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) NamingException(javax.naming.NamingException)

Example 4 with EjbRef

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

the class NamingContextListener method addEjb.

/**
 * Set the specified EJBs in the naming context.
 *
 * @param ejb the EJB descriptor
 */
public void addEjb(ContextEjb ejb) {
    Reference ref = lookForLookupRef(ejb);
    if (ref == null) {
        // Create a reference to the EJB.
        ref = new EjbRef(ejb.getType(), ejb.getHome(), ejb.getRemote(), ejb.getLink());
        // Adding the additional parameters, if any
        Iterator<String> params = ejb.listProperties();
        while (params.hasNext()) {
            String paramName = params.next();
            String paramValue = (String) ejb.getProperty(paramName);
            StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
            ref.add(refAddr);
        }
    }
    try {
        createSubcontexts(envCtx, ejb.getName());
        envCtx.bind(ejb.getName(), ref);
    } catch (NamingException e) {
        log.error(sm.getString("naming.bindFailed", e));
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) NamingException(javax.naming.NamingException)

Example 5 with EjbRef

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

the class OpenEjbFactory method getObjectInstance.

// -------------------------------------------------- ObjectFactory Methods
/**
 * Create a new EJB instance using OpenEJB.
 *
 * @param obj The reference object describing the DataSource
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    Object beanObj = null;
    if (obj instanceof EjbRef) {
        Reference ref = (Reference) obj;
        String factory = DEFAULT_OPENEJB_FACTORY;
        RefAddr factoryRefAddr = ref.get("openejb.factory");
        if (factoryRefAddr != null) {
            // Retrieving the OpenEJB factory
            factory = factoryRefAddr.getContent().toString();
        }
        Properties env = new Properties();
        env.put(Context.INITIAL_CONTEXT_FACTORY, factory);
        RefAddr linkRefAddr = ref.get("openejb.link");
        if (linkRefAddr != null) {
            String ejbLink = linkRefAddr.getContent().toString();
            beanObj = (new InitialContext(env)).lookup(ejbLink);
        }
    }
    return beanObj;
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference) EjbRef(org.apache.naming.EjbRef) Properties(java.util.Properties) InitialContext(javax.naming.InitialContext)

Aggregations

Reference (javax.naming.Reference)7 EjbRef (org.apache.naming.EjbRef)7 InitialContext (javax.naming.InitialContext)5 RefAddr (javax.naming.RefAddr)5 NamingException (javax.naming.NamingException)4 Properties (java.util.Properties)3 StringRefAddr (javax.naming.StringRefAddr)2 ObjectFactory (javax.naming.spi.ObjectFactory)2