Search in sources :

Example 16 with Reference

use of javax.naming.Reference in project voltdb by VoltDB.

the class JDBCDataSourceFactory method getObjectInstance.

/**
     * Creates a JDBCDataSource object using the location or reference
     * information specified.<p>
     *
     * The Reference object should support the properties, database, user,
     * password.
     *
     * @param obj The reference information used in creating a
     *      JDBCDatasource object.
     * @param name ignored
     * @param nameCtx ignored
     * @param environment ignored
     * @return A newly created JDBCDataSource object; null if an object
     *      cannot be created.
     * @exception Exception never
     */
public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
    String dsClass = "org.hsqldb_voltpatches.jdbc.JDBCDataSource";
    Reference ref = (Reference) obj;
    if (ref.getClassName().equals(dsClass)) {
        JDBCDataSource ds = new JDBCDataSource();
        ds.setDatabase((String) ref.get("database").getContent());
        ds.setUser((String) ref.get("user").getContent());
        ds.setPassword((String) ref.get("password").getContent());
        return ds;
    } else {
        return null;
    }
}
Also used : Reference(javax.naming.Reference)

Example 17 with Reference

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

the class ObjectFactoryHelper method getObjectInstance.

public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception {
    // Step 1 ensure we have a reference rather than a referenceable
    if (obj instanceof Referenceable) {
        obj = ((Referenceable) obj).getReference();
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "obj = " + obj);
    Object result = obj;
    // Step 2 - if we have a reference process it as a reference
    if (obj instanceof Reference) {
        Reference ref = (Reference) obj;
        String className = ref.getFactoryClassName();
        if (className != null) {
            // Step 3 - use the class name in the reference to get the factory class name
            result = getObjectInstanceUsingClassName(obj, className, obj, name, nameCtx, environment);
        } else {
            // Step 4 - look, assuming url string ref addrs, for a url context object factory.
            result = getObjectInstanceUsingRefAddress(ref.getAll(), obj, name, nameCtx, environment);
        }
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 4: result = " + result);
    // Step 5 - if we still don't have a resolved object goto the object factory builds in the SR.
    if (result == null || result == obj) {
        result = getObjectInstanceUsingObjectFactoryBuilders(obj, name, nameCtx, environment);
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 5: result = " + result);
    // Step 6 - Attempt to use all the registered ObjectFactories in the SR.
    if (result == null || result == obj) {
        if ((obj instanceof Reference && ((Reference) obj).getFactoryClassName() == null) || !(obj instanceof Reference)) {
            result = getObjectInstanceUsingObjectFactories(obj, name, nameCtx, environment);
        }
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 6: result = " + result);
    // to https://www.osgi.org/bugzilla/show_bug.cgi?id=138 
    if (result == null || result == obj) {
        result = getObjectInstanceViaContextDotObjectFactories(obj, name, nameCtx, environment);
    }
    if (logger.isLoggable(Level.FINE))
        logger.log(Level.FINE, "Step 7: result = " + result);
    return (result == null) ? obj : result;
}
Also used : Referenceable(javax.naming.Referenceable) Reference(javax.naming.Reference) ServiceReference(org.osgi.framework.ServiceReference)

Example 18 with Reference

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

the class JndiContext method lookup.

public Object lookup(String name) throws NamingException {
    if (name.length() == 0) {
        return this;
    }
    Object result = treeBindings.get(name);
    if (result == null) {
        result = bindings.get(name);
    }
    if (result == null) {
        int pos = name.indexOf(':');
        if (pos > 0) {
            String scheme = name.substring(0, pos);
            Context ctx = NamingManager.getURLContext(scheme, environment);
            if (ctx == null) {
                throw new NamingException("scheme " + scheme + " not recognized");
            }
            return ctx.lookup(name);
        } else {
            // Split out the first name of the path
            // and look for it in the bindings map.
            CompositeName path = new CompositeName(name);
            if (path.size() == 0) {
                return this;
            } else {
                String first = path.get(0);
                Object value = bindings.get(first);
                if (value == null) {
                    throw new NameNotFoundException(name);
                } else if (value instanceof Context && path.size() > 1) {
                    Context subContext = (Context) value;
                    value = subContext.lookup(path.getSuffix(1));
                }
                return value;
            }
        }
    }
    if (result instanceof Provider) {
        Provider provider = (Provider) result;
        result = provider.get();
    }
    if (result instanceof LinkRef) {
        LinkRef ref = (LinkRef) result;
        result = lookup(ref.getLinkName());
    }
    if (result instanceof Reference) {
        try {
            result = NamingManager.getObjectInstance(result, null, null, this.environment);
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) {
            throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
        }
    }
    if (result instanceof JndiContext) {
        String prefix = getNameInNamespace();
        if (prefix.length() > 0) {
            prefix = prefix + SEPARATOR;
        }
        result = new JndiContext((JndiContext) result, environment, prefix + name);
    }
    return result;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NotContextException(javax.naming.NotContextException) NamingException(javax.naming.NamingException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) NameNotFoundException(javax.naming.NameNotFoundException) Provider(com.google.inject.Provider) LinkRef(javax.naming.LinkRef)

Example 19 with Reference

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

the class JndiContext method lookup.

public Object lookup(String name) throws NamingException {
    if (name.length() == 0) {
        return this;
    }
    Object result = treeBindings.get(name);
    if (result == null) {
        result = bindings.get(name);
    }
    if (result == null) {
        int pos = name.indexOf(':');
        if (pos > 0) {
            String scheme = name.substring(0, pos);
            Context ctx = NamingManager.getURLContext(scheme, environment);
            if (ctx == null) {
                throw new NamingException("scheme " + scheme + " not recognized");
            }
            return ctx.lookup(name);
        } else {
            // Split out the first name of the path
            // and look for it in the bindings map.
            CompositeName path = new CompositeName(name);
            if (path.size() == 0) {
                return this;
            } else {
                String first = path.get(0);
                Object value = bindings.get(first);
                if (value == null) {
                    throw new NameNotFoundException(name);
                } else if (value instanceof Context && path.size() > 1) {
                    Context subContext = (Context) value;
                    value = subContext.lookup(path.getSuffix(1));
                }
                return value;
            }
        }
    }
    if (result instanceof LinkRef) {
        LinkRef ref = (LinkRef) result;
        result = lookup(ref.getLinkName());
    }
    if (result instanceof Reference) {
        try {
            result = NamingManager.getObjectInstance(result, null, null, this.environment);
        } catch (NamingException e) {
            throw e;
        } catch (Exception e) {
            throw (NamingException) new NamingException("could not look up : " + name).initCause(e);
        }
    }
    if (result instanceof JndiContext) {
        String prefix = getNameInNamespace();
        if (prefix.length() > 0) {
            prefix = prefix + SEPARATOR;
        }
        result = new JndiContext((JndiContext) result, environment, prefix + name);
    }
    return result;
}
Also used : Context(javax.naming.Context) NameNotFoundException(javax.naming.NameNotFoundException) Reference(javax.naming.Reference) CompositeName(javax.naming.CompositeName) NamingException(javax.naming.NamingException) NamingException(javax.naming.NamingException) NameNotFoundException(javax.naming.NameNotFoundException) NotContextException(javax.naming.NotContextException) OperationNotSupportedException(javax.naming.OperationNotSupportedException) LinkRef(javax.naming.LinkRef)

Example 20 with Reference

use of javax.naming.Reference in project datanucleus-rdbms by datanucleus.

the class InstanceKeyDataSourceFactory method getObjectInstance.

/**
 * implements ObjectFactory to create an instance of SharedPoolDataSource
 * or PerUserPoolDataSource
 */
@Override
public Object getObjectInstance(Object refObj, Name name, Context context, Hashtable<?, ?> env) throws IOException, ClassNotFoundException {
    // The spec says to return null if we can't create an instance
    // of the reference
    Object obj = null;
    if (refObj instanceof Reference) {
        Reference ref = (Reference) refObj;
        if (isCorrectClass(ref.getClassName())) {
            RefAddr ra = ref.get("instanceKey");
            if (ra != null && ra.getContent() != null) {
                // object was bound to jndi via Referenceable api.
                obj = instanceMap.get(ra.getContent());
            } else {
                // tomcat jndi creates a Reference out of server.xml
                // <ResourceParam> configuration and passes it to an
                // instance of the factory given in server.xml.
                String key = null;
                if (name != null) {
                    key = name.toString();
                    obj = instanceMap.get(key);
                }
                if (obj == null) {
                    InstanceKeyDataSource ds = getNewInstance(ref);
                    setCommonProperties(ref, ds);
                    obj = ds;
                    if (key != null) {
                        instanceMap.put(key, ds);
                    }
                }
            }
        }
    }
    return obj;
}
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