Search in sources :

Example 81 with Reference

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

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
 */
@Override
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) {
                    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 (Exception e) {
                    if (e instanceof NamingException)
                        throw (NamingException) e;
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(e);
                    throw ex;
                }
            }
        } 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 (Exception e) {
                    NamingException ex = new NamingException("Could not create resource factory instance");
                    ex.initCause(e);
                    throw ex;
                }
            } 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) {
                    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) ResourceRef(org.apache.naming.ResourceRef) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException)

Example 82 with Reference

use of javax.naming.Reference 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 83 with Reference

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

the class NamingContextListener method addResourceLink.

/**
 * Set the specified resource link in the naming context.
 */
public void addResourceLink(ContextResourceLink resourceLink) {
    // Create a reference to the resource.
    Reference ref = new ResourceLinkRef(resourceLink.getType(), resourceLink.getGlobal(), resourceLink.getFactory(), null);
    Iterator<String> i = resourceLink.listProperties();
    while (i.hasNext()) {
        String key = i.next();
        Object val = resourceLink.getProperty(key);
        if (val != null) {
            StringRefAddr refAddr = new StringRefAddr(key, val.toString());
            ref.add(refAddr);
        }
    }
    javax.naming.Context ctx = "UserTransaction".equals(resourceLink.getName()) ? compCtx : envCtx;
    try {
        if (logger.isDebugEnabled())
            log.debug("  Adding resource link " + resourceLink.getName());
        createSubcontexts(envCtx, resourceLink.getName());
        ctx.bind(resourceLink.getName(), ref);
    } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
    }
    ResourceLinkFactory.registerGlobalResourceAccess(getGlobalNamingContext(), resourceLink.getName(), resourceLink.getGlobal());
}
Also used : ResourceLinkRef(org.apache.naming.ResourceLinkRef) StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) NamingException(javax.naming.NamingException)

Example 84 with Reference

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

the class NamingContextListener method addResource.

/**
 * Set the specified resources in the naming context.
 */
public void addResource(ContextResource resource) {
    // Create a reference to the resource.
    Reference ref = new ResourceRef(resource.getType(), resource.getDescription(), resource.getScope(), resource.getAuth(), resource.getSingleton());
    // Adding the additional parameters, if any
    Iterator<String> params = resource.listProperties();
    while (params.hasNext()) {
        String paramName = params.next();
        String paramValue = (String) resource.getProperty(paramName);
        StringRefAddr refAddr = new StringRefAddr(paramName, paramValue);
        ref.add(refAddr);
    }
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("  Adding resource ref " + resource.getName() + "  " + ref);
        }
        createSubcontexts(envCtx, resource.getName());
        envCtx.bind(resource.getName(), ref);
    } catch (NamingException e) {
        logger.error(sm.getString("naming.bindFailed", e));
    }
    if ("javax.sql.DataSource".equals(ref.getClassName()) && resource.getSingleton()) {
        try {
            ObjectName on = createObjectName(resource);
            Object actualResource = envCtx.lookup(resource.getName());
            Registry.getRegistry(null, null).registerComponent(actualResource, on, null);
            objectNames.put(resource.getName(), on);
        } catch (Exception e) {
            logger.warn(sm.getString("naming.jmxRegistrationFailed", e));
        }
    }
}
Also used : StringRefAddr(javax.naming.StringRefAddr) Reference(javax.naming.Reference) ResourceRef(org.apache.naming.ResourceRef) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) MalformedObjectNameException(javax.management.MalformedObjectNameException) MalformedURLException(java.net.MalformedURLException) NameAlreadyBoundException(javax.naming.NameAlreadyBoundException) ObjectName(javax.management.ObjectName)

Example 85 with Reference

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

the class MemoryUserDatabaseFactory method getObjectInstance.

// --------------------------------------------------------- Public Methods
/**
 * <p>Create and return a new <code>MemoryUserDatabase</code> instance
 * that has been configured according to the properties of the
 * specified <code>Reference</code>.  If you 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
 */
@Override
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // that specify a class name of "org.apache.catalina.UserDatabase"
    if ((obj == null) || !(obj instanceof Reference)) {
        return (null);
    }
    Reference ref = (Reference) obj;
    if (!"org.apache.catalina.UserDatabase".equals(ref.getClassName())) {
        return (null);
    }
    // Create and configure a MemoryUserDatabase instance based on the
    // RefAddr values associated with this Reference
    MemoryUserDatabase database = new MemoryUserDatabase(name.toString());
    RefAddr ra = null;
    ra = ref.get("pathname");
    if (ra != null) {
        database.setPathname(ra.getContent().toString());
    }
    ra = ref.get("readonly");
    if (ra != null) {
        database.setReadonly(Boolean.parseBoolean(ra.getContent().toString()));
    }
    // Return the configured database instance
    database.open();
    // Don't try something we know won't work
    if (!database.getReadonly())
        database.save();
    return (database);
}
Also used : RefAddr(javax.naming.RefAddr) Reference(javax.naming.Reference)

Aggregations

Reference (javax.naming.Reference)226 NamingException (javax.naming.NamingException)79 RefAddr (javax.naming.RefAddr)59 StringRefAddr (javax.naming.StringRefAddr)54 Context (javax.naming.Context)41 NameNotFoundException (javax.naming.NameNotFoundException)40 InitialContext (javax.naming.InitialContext)37 Test (org.junit.Test)33 Name (javax.naming.Name)30 NameAlreadyBoundException (javax.naming.NameAlreadyBoundException)28 Properties (java.util.Properties)26 OperationNotSupportedException (javax.naming.OperationNotSupportedException)25 NotContextException (javax.naming.NotContextException)24 ObjectFactory (javax.naming.spi.ObjectFactory)23 CompositeName (javax.naming.CompositeName)21 Referenceable (javax.naming.Referenceable)18 Binding (javax.naming.Binding)17 CompoundName (javax.naming.CompoundName)16 LinkRef (javax.naming.LinkRef)15 Hashtable (java.util.Hashtable)12