Platinum Partner

Making A Local Hash Based Context

// This may be total overkill, but I wanted to use a DataSource in a stand alone app.

jndi.properties

java.naming.factory.initial=com.admin.model.naming.ContextFactory
java.naming.provider.url=iiop://localhost:1050


ContextFactory.java

package com.admin.model.naming;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
public class ContextFactory implements InitialContextFactory {
    public ContextFactory() {
    }
    public Context getInitialContext(Hashtable environment) throws NamingException {
        ContextImpl ctx = (ContextImpl)ContextImpl.getInstance();
        ctx.setEnvironment(environment);
        return ctx;
    }
}


ContextImpl.java

package com.admin.model.naming;
import java.util.Hashtable;
import java.util.logging.Logger;
import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
public class ContextImpl implements Context {
    private static Logger logger = Logger.getLogger(ContextImpl.class.getName());
    private static ContextImpl instance = new ContextImpl();
    private Hashtable environment;
    private Hashtable directory = new Hashtable();

    private ContextImpl() {
    }

    static Context getInstance() {
        return instance;
    }
    
    public Object lookup(Name name) throws NamingException {
        logger.entering(getClass().getName(), "lookup", name);
        Object result = null;        
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        result = directory.get(name);
        logger.exiting(getClass().getName(), "lookup", result);
        return result;
    }

    public Object lookup(String name) throws NamingException {
        logger.entering(getClass().getName(), "lookup", name);
        Object result = null;     
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        result = directory.get(properName);
        logger.exiting(getClass().getName(), "lookup", result);
        return result;
    }

    public void bind(Name name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "bind", new Object[]{name, obj});
        if(directory.containsKey(name)) {
            throw new NamingException("Naming directory already contains entry for: " + name);
        }
        directory.put(name, obj);
    }

    public void bind(String name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "bind", new Object[]{name, obj});
        Name properName = new CompositeName(name);
        if(directory.containsKey(properName)) {
            throw new NamingException("Naming directory already contains entry for: " + properName);
        }
        directory.put(properName, obj);
    }

    public void rebind(Name name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "rebind", new Object[]{name, obj});
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        directory.put(name, obj);
    }

    public void rebind(String name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "rebind", new Object[]{name, obj});
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        directory.put(properName, obj);
    }

    public void unbind(Name name) throws NamingException {
        logger.entering(getClass().getName(), "unbind", name);
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        directory.remove(name);
    }

    public void unbind(String name) throws NamingException {
        logger.entering(getClass().getName(), "unbind", name);
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        directory.remove(properName);
    }

    public void rename(Name oldName, Name newName) throws NamingException {
        logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName});
        Object obj = lookup(oldName);
        unbind(oldName);
        bind(newName, obj);
    }

    public void rename(String oldName, String newName) throws NamingException {
        logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName});
        Object obj = lookup(oldName);
        unbind(oldName);
        bind(newName, obj);
    }

    public NamingEnumeration list(Name name) throws NamingException {
        logger.entering(getClass().getName(), "list", name);
        return null;
    }

    public NamingEnumeration list(String name) throws NamingException {
        logger.entering(getClass().getName(), "list", name);
        return null;
    }

    public NamingEnumeration listBindings(Name name) throws NamingException {
        logger.entering(getClass().getName(), "listBindings", name);
        return null;
    }

    public NamingEnumeration listBindings(String name) throws NamingException {
        logger.entering(getClass().getName(), "listBindings", name);
        return null;
    }

    public void destroySubcontext(Name name) throws NamingException {
        logger.entering(getClass().getName(), "destroySubcontext", name);
    }

    public void destroySubcontext(String name) throws NamingException {
        logger.entering(getClass().getName(), "destroySubcontext", name);
    }

    public Context createSubcontext(Name name) throws NamingException {
        logger.entering(getClass().getName(), "createSubcontext", name);
        return null;
    }

    public Context createSubcontext(String name) throws NamingException {
        logger.entering(getClass().getName(), "createSubcontext", name);
        return null;
    }

    public Object lookupLink(Name name) throws NamingException {
        logger.entering(getClass().getName(), "lookupLink", name);
        return null;
    }

    public Object lookupLink(String name) throws NamingException {
        logger.entering(getClass().getName(), "lookupLink", name);
        return null;
    }

    public NameParser getNameParser(Name name) throws NamingException {
        logger.entering(getClass().getName(), "getNameParser", name);
        return null;
    }

    public NameParser getNameParser(String name) throws NamingException {
        logger.entering(getClass().getName(), "getNameParser", name);
        return null;
    }

    public Name composeName(Name name, Name prefix) throws NamingException {
        logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix});
        return null;
    }

    public String composeName(String name, String prefix) throws NamingException {
        logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix});
        return null;
    }

    public Object addToEnvironment(String propName, Object propVal) throws NamingException {
        logger.entering(getClass().getName(), "addToEnvironment", new Object[]{propName, propVal});
        environment.put(propName, propVal);
        return null;
    }

    public Object removeFromEnvironment(String propName) throws NamingException {
        logger.entering(getClass().getName(), "removeFromEnvironment", propName);
        Object result = environment.get(propName);
        environment.remove(propName);
        return result;
    }

    public Hashtable getEnvironment() throws NamingException {
        logger.entering(getClass().getName(), "getEnvironment");
        return environment;
    }

    void setEnvironment(Hashtable env) {
        environment = env;
    }
    
    public void close() throws NamingException {
        logger.entering(getClass().getName(), "close");
    }

    public String getNameInNamespace() throws NamingException {
        logger.entering(getClass().getName(), "getNameInNamespace");
        return null;
    }
}


usage

InitialContext ctx = new InitialContext();
ctx.bind("DataSource", ds);
{{ tag }}, {{tag}},

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}
{{ parent.authors[0].realName || parent.author}}

{{ parent.authors[0].tagline || parent.tagline }}

{{ parent.views }} ViewsClicks
Tweet

{{parent.nComments}}