Package net.timewalker.ffmq3.jndi

Source Code of net.timewalker.ffmq3.jndi.FFMQJNDIContext$FlatNameParser

/*
* This file is part of FFMQ.
*
* FFMQ is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* FFMQ is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with FFMQ; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/
package net.timewalker.ffmq3.jndi;

import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Properties;

import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.CompoundName;
import javax.naming.Context;
import javax.naming.InvalidNameException;
import javax.naming.Name;
import javax.naming.NameAlreadyBoundException;
import javax.naming.NameClassPair;
import javax.naming.NameNotFoundException;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.NotContextException;
import javax.naming.OperationNotSupportedException;

import net.timewalker.ffmq3.common.destination.QueueRef;
import net.timewalker.ffmq3.common.destination.TopicRef;

/**
* <p>Implementation of a JNDI {@link Context}, providing lookup for FFMQ connection factories.</p>
*/
public final class FFMQJNDIContext implements Context
{
    private Hashtable               env;
    protected Hashtable             bindings   = new Hashtable();
    private static final NameParser flatParser = new FlatNameParser();

    /**
     * Constructor
     */
    public FFMQJNDIContext(Hashtable environment)
    {
        // Copy environment
        if (environment != null)
            env = (Hashtable)environment.clone();
        else
            env = new Hashtable();
    }

    /**
     * Constructor
     */
    protected FFMQJNDIContext(Hashtable environment,Hashtable bindings)
    {
        this.env = (Hashtable)environment.clone();
        this.bindings = bindings;
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#lookup(java.lang.String)
     */
    public Object lookup(String name) throws NamingException
    {
        return lookup(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#lookup(javax.naming.Name)
     */
    public Object lookup(Name name) throws NamingException
    {
        if (name.isEmpty())
            return new FFMQJNDIContext(env, bindings);

        // Extract components that belong to this namespace
        String nm = name.toString();

        // Find object in internal hash table
        Object answer = bindings.get(nm);
        if (answer == null)
        {
          // Dynamic queue or topic lookup
          if (nm.startsWith("queue/") && nm.length() > 6)
            return new QueueRef(nm.substring(nm.indexOf('/')+1));
          if (nm.startsWith("topic/") && nm.length() > 6)
            return new TopicRef(nm.substring(nm.indexOf('/')+1));
         
            throw new NameNotFoundException(name + " not found");
        }

        return answer;
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
     */
    public void bind(String name, Object obj) throws NamingException
    {
        bind(new CompositeName(name), obj);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
     */
    public void bind(Name name, Object obj) throws NamingException
    {
        if (name.isEmpty())
            throw new InvalidNameException("Cannot bind empty name");

        // Extract components that belong to this namespace
        String nm = name.toString();

        // Find object in internal hash table
        if (bindings.get(nm) != null)
        {
            throw new NameAlreadyBoundException("Use rebind to override");
        }

        // Add object to internal hash table
        bindings.put(nm, obj);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
     */
    public void rebind(String name, Object obj) throws NamingException
    {
        rebind(new CompositeName(name), obj);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
     */
    public void rebind(Name name, Object obj) throws NamingException
    {
        if (name.isEmpty())
            throw new InvalidNameException("Cannot bind empty name");

        // Extract components that belong to this namespace
        String nm = name.toString();

        // Add object to internal hash table
        bindings.put(nm, obj);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#unbind(java.lang.String)
     */
    public void unbind(String name) throws NamingException
    {
        unbind(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#unbind(javax.naming.Name)
     */
    public void unbind(Name name) throws NamingException
    {
        if (name.isEmpty())
            throw new InvalidNameException("Cannot unbind empty name");

        // Extract components that belong to this namespace
        String nm = name.toString();

        // Remove object from internal hash table
        bindings.remove(nm);
    }
   
    /*
     * (non-Javadoc)
     * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
     */
    public void rename(String oldname, String newname) throws NamingException
    {
        rename(new CompositeName(oldname), new CompositeName(newname));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
     */
    public void rename(Name oldname, Name newname) throws NamingException
    {
        if (oldname.isEmpty() || newname.isEmpty())
            throw new InvalidNameException("Cannot rename empty name");

        // Extract components that belong to this namespace
        String oldnm = oldname.toString();
        String newnm = newname.toString();

        // Check if new name exists
        if (bindings.get(newnm) != null)
            throw new NameAlreadyBoundException(newname.toString() + " is already bound");

        // Check if old name is bound
        Object oldBinding = bindings.remove(oldnm);
        if (oldBinding == null)
            throw new NameNotFoundException(oldname.toString() + " not bound");

        bindings.put(newnm, oldBinding);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#list(java.lang.String)
     */
    public NamingEnumeration list(String name) throws NamingException
    {
        return list(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#list(javax.naming.Name)
     */
    public NamingEnumeration list(Name name) throws NamingException
    {
        if (name.isEmpty())
            return new ListOfNames(bindings.keys());

        Object target = lookup(name);
        if (target instanceof Context)
        {
            try
            {
                return ((Context)target).list("");
            }
            finally
            {
                ((Context)target).close();
            }
        }
        throw new NotContextException(name + " cannot be listed");
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#listBindings(java.lang.String)
     */
    public NamingEnumeration listBindings(String name) throws NamingException
    {
        return listBindings(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#listBindings(javax.naming.Name)
     */
    public NamingEnumeration listBindings(Name name) throws NamingException
    {
        if (name.isEmpty())
            return new ListOfBindings(bindings.keys());

        Object target = lookup(name);
        if (target instanceof Context)
        {
            try
            {
                return ((Context)target).listBindings("");
            }
            finally
            {
                ((Context)target).close();
            }
        }
        throw new NotContextException(name + " cannot be listed");
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#destroySubcontext(java.lang.String)
     */
    public void destroySubcontext(String name) throws NamingException
    {
        destroySubcontext(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
     */
    public void destroySubcontext(Name name) throws NamingException
    {
        throw new OperationNotSupportedException("This context does not support subcontexts");
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#createSubcontext(java.lang.String)
     */
    public Context createSubcontext(String name) throws NamingException
    {
        return createSubcontext(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#createSubcontext(javax.naming.Name)
     */
    public Context createSubcontext(Name name) throws NamingException
    {
        throw new OperationNotSupportedException("This context does not support subcontexts");
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#lookupLink(java.lang.String)
     */
    public Object lookupLink(String name) throws NamingException
    {
        return lookupLink(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#lookupLink(javax.naming.Name)
     */
    public Object lookupLink(Name name) throws NamingException
    {
        return lookup(name);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#getNameParser(java.lang.String)
     */
    public NameParser getNameParser(String name) throws NamingException
    {
        return getNameParser(new CompositeName(name));
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#getNameParser(javax.naming.Name)
     */
    public NameParser getNameParser(Name name) throws NamingException
    {
        // Do lookup to verify name exists
        Object obj = lookup(name);
        if (obj instanceof Context)
        {
            ((Context)obj).close();
        }
        return flatParser;
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
     */
    public String composeName(String name, String prefix) throws NamingException
    {
        Name result = composeName(new CompositeName(name), new CompositeName(prefix));
        return result.toString();
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
     */
    public Name composeName(Name name, Name prefix) throws NamingException
    {
        Name result = (Name)(prefix.clone());
        result.addAll(name);
        return result;
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
     */
    public Object addToEnvironment(String propName, Object propVal)
    {
        return env.put(propName, propVal);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
     */
    public Object removeFromEnvironment(String propName)
    {
        return env.remove(propName);
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#getEnvironment()
     */
    public Hashtable getEnvironment()
    {
        return (Hashtable)env.clone();
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#getNameInNamespace()
     */
    public String getNameInNamespace()
    {
        return "";
    }

    /*
     * (non-Javadoc)
     * @see javax.naming.Context#close()
     */
    public void close()
    {
        // Nothing to do
    }

    //--------------------------------------------------------------------------------------
   
    // Class for enumerating name/class pairs
    private class ListOfNames implements NamingEnumeration
    {
        protected Enumeration names;

        /**
         * Constructor
         */
        public ListOfNames(Enumeration names)
        {
            this.names = names;
        }

        /*
         * (non-Javadoc)
         * @see java.util.Enumeration#hasMoreElements()
         */
        public boolean hasMoreElements()
        {
            return hasMore();
        }

        /*
         * (non-Javadoc)
         * @see javax.naming.NamingEnumeration#hasMore()
         */
        public boolean hasMore()
        {
            return names.hasMoreElements();
        }

        /*
         * (non-Javadoc)
         * @see javax.naming.NamingEnumeration#next()
         */
        public Object next()
        {
            String name = (String)names.nextElement();
            String className = bindings.get(name).getClass().getName();
            return new NameClassPair(name, className);
        }

        /*
         * (non-Javadoc)
         * @see java.util.Enumeration#nextElement()
         */
        public Object nextElement()
        {
            return next();
        }

        /*
         * (non-Javadoc)
         * @see javax.naming.NamingEnumeration#close()
         */
        public void close()
        {
            // Nothing
        }
    }

    // Class for enumerating bindings
    private class ListOfBindings extends ListOfNames
    {
        /**
         * Constructor
         */
        public ListOfBindings(Enumeration names)
        {
            super(names);
        }

        /*
         * (non-Javadoc)
         * @see net.timewalker.ffmq3.common.jndi.FFMQJNDIContext.ListOfNames#next()
         */
        public Object next()
        {
            String name = (String)names.nextElement();
            return new Binding(name, bindings.get(name));
        }
    }

    private static class FlatNameParser implements NameParser
    {
        private static final Properties syntax = new Properties();
        static
        {
            syntax.put("jndi.syntax.direction", "flat");
            syntax.put("jndi.syntax.ignorecase", "false");
        }
       
    /**
     * Constructor
     */
        public FlatNameParser()
    {
      super();
    }
       
        /*
         * (non-Javadoc)
         * @see javax.naming.NameParser#parse(java.lang.String)
         */
        public Name parse(String name) throws NamingException
        {
            return new CompoundName(name, syntax);
        }
    }
}
TOP

Related Classes of net.timewalker.ffmq3.jndi.FFMQJNDIContext$FlatNameParser

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.