User: stark   
  Date: 01/03/05 02:11:03

  Added:       src/main/org/jboss/test/security/ejb/project/support
                        DefaultName.java DirBinding.java
                        DirContextStringImpl.java HeirMemoryMap.java
                        NameBindingIterator.java
  Log:
  Tests of the JBossSX security framework
  
  Revision  Changes    Path
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/project/support/DefaultName.java
  
  Index: DefaultName.java
  ===================================================================
  package org.jboss.test.security.ejb.project.support;
  
  import java.util.Enumeration;
  import java.util.Properties;
  import javax.naming.CompoundName;
  import javax.naming.InvalidNameException;
  import javax.naming.Name;
  import javax.naming.NameParser;
  import javax.naming.NamingException;
  
  /** A simple subclass of CompoundName that fixes the name syntax to:
        jndi.syntax.direction = left_to_right
        jndi.syntax.separator = "/"
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class DefaultName extends CompoundName
  {
        /** The Properties used for the project directory heirarchical names */
        static Name emptyName;
        static Properties nameSyntax = new Properties();
        static
        {
                nameSyntax.put("jndi.syntax.direction", "left_to_right");
                nameSyntax.put("jndi.syntax.separator", "/");
                try
                {
                        emptyName = new DefaultName("");
                }
                catch(InvalidNameException e)
                {
                }       
        }
  
      private static class DefaultNameParser implements NameParser
      {
          public Name parse(String path) throws NamingException
          {
              DefaultName name = new DefaultName(path);
              return name;
          }
      }
  
      public static NameParser getNameParser()
      {
          return new DefaultNameParser();
      }
  
        /** Creates new DefaultName */
      public DefaultName(Enumeration comps)
        {
                super(comps, nameSyntax);
      }
      public DefaultName(String name) throws InvalidNameException
        {
                super(name, nameSyntax);
      }
      public DefaultName(Name name)
        {
                super(name.getAll(), nameSyntax);
      }
      public DefaultName()
        {
                this(emptyName);
      }
  
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/project/support/DirBinding.java
  
  Index: DirBinding.java
  ===================================================================
  package org.jboss.test.security.ejb.project.support;
  
  import javax.naming.Binding;
  import javax.naming.directory.Attributes;
  
  /** A subclass of Binding that adds support for Attributes. This class is used
  to pass a contexts raw bindings to NameBindingIterator.
  
  @author [EMAIL PROTECTED]
  @version $Revision: 1.1 $
  */
  public class DirBinding extends Binding
  {
        private transient Attributes attributes;
  
        /** Constructs an instance of a Binding given its relative name, object,
         attributes and whether the name is relative. 
        @param obj - The possibly null object bound to name.
        @param attributes - the attributes associated with obj
        */
      public DirBinding(String name, Object obj, Attributes attributes)
        {
                this(name, null, obj, true, attributes);
        }
        /** Constructs an instance of a Binding given its relative name, class name,
         object, attributes and whether the name is relative.
        @param name - The non-null string name of the object.
        @param className - The possibly null class name of the object bound to name.
         If null, the class name of obj is returned by getClassName(). If obj is
         also null, getClassName() will return null.
        @param obj - The possibly null object bound to name.
        @param attributes - the attributes associated with obj
        */
      public DirBinding(String name, String className, Object obj, Attributes 
attributes)
        {
                this(name, className, obj, true, attributes);
        }
        /** Constructs an instance of a Binding given its name, object, attributes
         and whether the name is relative. 
        @param name - The non-null string name of the object.
        @param obj - The possibly null object bound to name.
        @param isRelative - true if name is a name relative to the target context
         (which is named by the first parameter of the listBindings() method);
         false if name is a URL string.
        @param attributes - the attributes associated with obj
        */
      public DirBinding(String name, String className, Object obj, boolean isRelative,
                Attributes attributes)
        {
                super(name, className, obj, isRelative);
                this.attributes = attributes;
      }
  
        public Attributes getAttributes()
        {
                return attributes;
        }
        public void setAttributes(Attributes attributes)
        {
                this.attributes = attributes;
        }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/project/support/DirContextStringImpl.java
  
  Index: DirContextStringImpl.java
  ===================================================================
  package org.jboss.test.security.ejb.project.support;
  
  import java.util.Hashtable;
  import javax.naming.CompositeName;
  import javax.naming.Context;
  import javax.naming.Name;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  import javax.naming.NameParser;
  import javax.naming.directory.Attribute;
  import javax.naming.directory.Attributes;
  import javax.naming.directory.BasicAttributes;
  import javax.naming.directory.DirContext;
  import javax.naming.directory.ModificationItem;
  import javax.naming.directory.SearchControls;
  
  /** An abstract implementation of DirContext that simply takes every DirContext
  method that accepts the String form of a Name and invokes the corresponding
  method that accecpts a Name.
  
  @author [EMAIL PROTECTED]
  @version $Id: DirContextStringImpl.java,v 1.1 2001/03/05 10:11:02 stark Exp $
  */
  public abstract class DirContextStringImpl implements DirContext
  {
        private NameParser nameParser;
  
        /** Creates new DirContextStringImpl */
      public DirContextStringImpl(NameParser nameParser)
        {
                this.nameParser = nameParser;
      }
      public DirContextStringImpl()
        {
                this(DefaultName.getNameParser());
      }
  
  // --- Begin DirContext interface methods that accept a String name
        public void bind(java.lang.String name, Object obj) throws NamingException
        {
                bind(nameParser.parse(name), obj);
        }
        
        public String composeName(String name,String name1) throws NamingException
        {
                return null;
        }
        
        public Context createSubcontext(java.lang.String name) throws NamingException
        {
                return createSubcontext(nameParser.parse(name));
        }
        
        public void destroySubcontext(java.lang.String name) throws NamingException
        {
                destroySubcontext(nameParser.parse(name));
        }
        
        public NameParser getNameParser(java.lang.String name) throws NamingException
        {
                return getNameParser(nameParser.parse(name));
        }
        
        public NamingEnumeration list(java.lang.String name) throws NamingException
        {
                return list(nameParser.parse(name));
        }
        
        public NamingEnumeration listBindings(java.lang.String name) throws 
NamingException
        {
                return listBindings(nameParser.parse(name));
        }
        
        public java.lang.Object lookup(String name) throws NamingException
        {
                return lookup(nameParser.parse(name));
        }
        public java.lang.Object lookupLink(String name) throws NamingException
        {
                return lookupLink(nameParser.parse(name));
        }
  
        public void rebind(String name,Object obj) throws NamingException
        {
                rebind(nameParser.parse(name), obj);
        }
        
        public void rename(String name,String name1) throws NamingException
        {
                rename(nameParser.parse(name), nameParser.parse(name1));
        }
  
        public void unbind(String name) throws NamingException
        {
                unbind(nameParser.parse(name));
        }
  
        public void bind(String name,Object obj, Attributes attributes) throws 
NamingException
        {
                bind(nameParser.parse(name), obj, attributes);
        }
        
        public DirContext createSubcontext(String name, Attributes attributes) throws 
NamingException
        {
                return createSubcontext(nameParser.parse(name), attributes);
        }
  
        public Attributes getAttributes(String name) throws NamingException
        {
                return getAttributes(nameParser.parse(name));
        }
  
        public Attributes getAttributes(String name,String[] attrNames) throws 
NamingException
        {
                return getAttributes(nameParser.parse(name), attrNames);
        }
        
        public DirContext getSchema(String name) throws NamingException
        {
                return getSchema(nameParser.parse(name));
        }
        
        public DirContext getSchemaClassDefinition(String name) throws NamingException
        {
                return getSchemaClassDefinition(nameParser.parse(name));
        }
  
        public void modifyAttributes(String name, ModificationItem[] modificationItem) 
throws NamingException
        {
                modifyAttributes(nameParser.parse(name), modificationItem);
        }
  
        public void modifyAttributes(String name,int index, Attributes attributes) 
throws NamingException
        {
                modifyAttributes(nameParser.parse(name), index, attributes);
        }
  
        public void rebind(String name,Object obj, Attributes attributes) throws 
NamingException
        {
                rebind(nameParser.parse(name), obj, attributes);
        }
  
        public NamingEnumeration search(String name, Attributes attributes) throws 
NamingException
        {
                return search(nameParser.parse(name), attributes);
        }
  
        public NamingEnumeration search(String name,String name1, SearchControls 
searchControls) throws NamingException
        {
                return search(nameParser.parse(name), name1, searchControls);
        }
        
        public  NamingEnumeration search(String name, Attributes attributes, String[] 
str2) throws NamingException
        {
                return search(nameParser.parse(name), attributes, str2);
        }
        
        public NamingEnumeration search(String name,String name1, Object[] obj, 
SearchControls searchControls) throws NamingException {
                return null;
        }
        
  // --- End DirContext interface methods
  
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/project/support/HeirMemoryMap.java
  
  Index: HeirMemoryMap.java
  ===================================================================
  package org.jboss.test.security.ejb.project.support;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.Hashtable;
  import java.util.Iterator;
  import javax.naming.Binding;
  import javax.naming.CompositeName;
  import javax.naming.Context;
  import javax.naming.InvalidNameException;
  import javax.naming.Name;
  import javax.naming.NameAlreadyBoundException;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  import javax.naming.NameNotFoundException;
  import javax.naming.NameParser;
  import javax.naming.NotContextException;
  import javax.naming.OperationNotSupportedException;
  import javax.naming.directory.Attribute;
  import javax.naming.directory.Attributes;
  import javax.naming.directory.BasicAttributes;
  import javax.naming.directory.DirContext;
  import javax.naming.directory.ModificationItem;
  import javax.naming.directory.SearchControls;
  
  /** A simple in memory implementation of DirContext that uses a HashMap as the
   store and unix style path names.
  
  @author [EMAIL PROTECTED]
  @version $Id: HeirMemoryMap.java,v 1.1 2001/03/05 10:11:02 stark Exp $
  */
  public class HeirMemoryMap extends DirContextStringImpl implements DirContext, 
Serializable
  {
        private static NameParser nameParser = DefaultName.getNameParser();
        private HashMap bindings = new HashMap();
        private HashMap bindingAttrs = new HashMap();
        private HeirMemoryMap parent;
        private String contextName;
        private Hashtable env;
  
        /** Creates new HeirMemoryMap */
      public HeirMemoryMap()
        {
                this.contextName = "";
      }
      public HeirMemoryMap(String contextName, HeirMemoryMap parent, Attributes 
attributes) throws NamingException
        {
                this(contextName, parent, attributes, null);
        }
      public HeirMemoryMap(String contextName, HeirMemoryMap parent, Attributes 
attributes, Hashtable env) throws NamingException
        {
                this.contextName = contextName == null ? "" : contextName;
                this.parent = parent;
                bindingAttrs.put("", attributes.clone());
                if( parent != null )
                        parent.bind(contextName, this);
                this.env = env;
      }
  
        public String toString()
        {
                Name name = null;
                try
                {
                        name = getFullName();
                }
                catch(NamingException e)
                {
                }
                return name.toString();
        }
  
        String getName()
        {
                return contextName;
        }
        void setName(String contextName)
        {
                this.contextName = contextName;
        }
        Name getFullName() throws NamingException
        {
                CompositeName name = new CompositeName(getName());
                HeirMemoryMap context = parent;
                if( context == null )
                        return name;
  
                try
                {
                        while( context.parent != null )
                        {
                                name.add(0, context.getName());
                                context = context.parent;
                        }
                }
                catch(NamingException e)
                {
                }
                return name;
        }
  
  // --- 
        public Object addToEnvironment(String p1,Object p2) throws NamingException
        {
                return null;
        }
        public Object removeFromEnvironment(String p1) throws NamingException
        {
                return null;
        }
        
        public void bind(Name name, Object value) throws NamingException
        {
                bind(name, value, null);
        }
        
        public void bind(Name name, Object value, Attributes attributes) throws 
NamingException
        {
          if( name.isEmpty() )
                {
              throw new InvalidNameException("Cannot bind empty name");
          }
  
                internalBind(name, value, attributes, true);
        }
  
        public void close() throws NamingException
        {
        }
        
        public Name composeName(Name p1,Name p2) throws NamingException {
                return null;
        }
        
        public Context createSubcontext(Name name) throws NamingException
        {
                return createSubcontext(name, null);
        }
        
        public DirContext createSubcontext(Name name, Attributes attributes) throws 
NamingException
        {
          if( name.isEmpty() )
                {
              throw new InvalidNameException("Cannot createSubcontext with empty 
name");
          }
  
          DirContext subctx = null;
          String atom = name.get(0);
          if( name.size() == 1 )
          {
              subctx = new HeirMemoryMap(atom, this, attributes, env);
          }
                else
                {
                        DirContext context = (DirContext) bindings.get(atom);
                        subctx = context.createSubcontext(name.getSuffix(1), 
attributes);
                }
  
                return subctx;
        }
        
        public void destroySubcontext(Name name) throws NamingException
        {
                unbind(name);
        }
  
        public Attributes getAttributes(Name name) throws NamingException
        {
                return getAttributes(name, null);
        }
  
        public Attributes getAttributes(Name name, String[] attrIDs) throws 
NamingException
        {
                Attributes nameAttributes = null;
          String atom = name.get(0);
                if( name.isEmpty() == true )
                {
              nameAttributes = (Attributes) bindingAttrs.get("");
                }
          else if( name.size() == 1 )
          {
              Object binding = bindings.get(atom);
              if( binding != null )
              {
                  if( binding instanceof DirContext )
                  {
                      DirContext dirCtx = (DirContext) binding;
                      return dirCtx.getAttributes(name.getSuffix(1), attrIDs);
                  }
              }
              nameAttributes = (Attributes) bindingAttrs.get(atom);
          }
                else
                {
                        DirContext context = (DirContext) bindings.get(atom);
                        nameAttributes = context.getAttributes(name.getSuffix(1), 
attrIDs);
                }
  
                if( nameAttributes != null && attrIDs != null )
                {
                        BasicAttributes matches = new 
BasicAttributes(nameAttributes.isCaseIgnored());
                        for(int a = 0; a < attrIDs.length; a ++)
                        {
                                Attribute attr = nameAttributes.get(attrIDs[a]);
                                if( attr != null )
                                        matches.put(attr);
                        }
                        nameAttributes = matches;
                }
                return nameAttributes;
        }
  
        public java.util.Hashtable getEnvironment() throws NamingException
        {
                return env;
        }
  
        public String getNameInNamespace() throws NamingException
        {
                return toString();
        }
  
        public NameParser getNameParser(Name p1) throws NamingException
        {
                return nameParser;
        }
  
        public DirContext getSchema(Name p1) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
        
        public DirContext getSchemaClassDefinition(Name p1) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
        
        public NamingEnumeration list(Name p1) throws NamingException
        {
                return null;
        }
  
        public NamingEnumeration listBindings(Name name) throws NamingException
        {
                NamingEnumeration iter = null;
  
                if( name.isEmpty() == true )
                {
                        Iterator keys = bindings.keySet().iterator();
                        ArrayList tmp = new ArrayList();
                        while( keys.hasNext() )
                        {
                                String key = (String) keys.next();
                                Object value = bindings.get(key);
                                Attributes attributes = (Attributes) 
bindingAttrs.get(key);
                  DirBinding tuple = new DirBinding(key, value, attributes);
                                tmp.add(tuple);
                        }
                        iter = new NameBindingIterator(tmp.iterator(), this);
                }
                else
                {
                        String atom = name.get(0);
                        Context context = (Context) bindings.get(atom);
                        iter = context.listBindings(name.getSuffix(1));
                }
  
                return iter;
        }
  
        public Object lookup(Name name) throws NamingException
        {
                if( name.isEmpty() == true )
                        return this;
  
                String atom = name.get(0);
                Object binding = bindings.get(atom);
                if( name.size() == 1 )
                {   /* Need to check that binding is null and atom is not a key
                  since a null value could have been bound.
              */
                        if( binding == null && bindings.containsKey(atom) == false )
                        {
                                NameNotFoundException e = new 
NameNotFoundException("Failed to find: "+atom);
                                e.setRemainingName(name);
                                e.setResolvedObj(this);
                                throw e;
                        }
                }
                else if( (binding instanceof Context) )
                {
                        Context context = (Context) binding;
                        binding = context.lookup(name.getSuffix(1));
                }
                else
                {
                        NotContextException e = new NotContextException(atom + " does 
not name a directory context that supports attributes");
                        e.setRemainingName(name);
                        e.setResolvedObj(binding);
                        throw e;
                }
                return binding;
        }
  
        public Object lookupLink(Name p1) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
        
        public void modifyAttributes(Name p1,ModificationItem[] p2) throws 
NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
        
        public void modifyAttributes(Name p1,int p2,Attributes p3) throws 
NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
        
        public void rebind(Name name, Object value) throws NamingException
        {
                rebind(name, value, null);
        }
        
        public void rebind(Name name, Object value, Attributes attributes) throws 
NamingException
        {
          if( name.isEmpty() )
                {
              throw new InvalidNameException("Cannot bind empty name");
          }
  
                internalBind(name, value, attributes, false);
        }
  
        public void rename(Name p1,Name p2) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
  
        public NamingEnumeration search(Name p1,Attributes p2) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
  
        public NamingEnumeration search(Name p1,String p2,SearchControls p3) throws 
NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
  
        public NamingEnumeration search(Name p1,Attributes p2,String[] p3) throws 
NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
  
        public NamingEnumeration search(Name p1,String p2,Object[] p3,SearchControls 
p4) throws NamingException
        {
                throw new OperationNotSupportedException("Not implemented yet");
        }
  
        public void unbind(Name name) throws NamingException
        {
          if( name.isEmpty() )
                {
              throw new InvalidNameException("Cannot unbind empty name");
          }
  
                String atom = name.get(0);
                Object binding = bindings.get(atom);
                if( name.size() == 1 )
                {   /* Need to check that binding is null and atom is not a key
                  since a null value could have been bound.
              */
                        if( binding == null && bindings.containsKey(atom) == false )
                        {
                                NameNotFoundException e = new 
NameNotFoundException("Failed to find: "+atom);
                                e.setRemainingName(name);
                                e.setResolvedObj(this);
                                throw e;
                        }
                bindings.remove(atom);
              bindingAttrs.remove(atom);
                }
                else if( (binding instanceof Context) )
                {
                        Context context = (Context) binding;
                        context.unbind(name.getSuffix(1));
                }
                else
                {
                        NotContextException e = new NotContextException(atom + " does 
not name a directory context that supports attributes");
                        e.setRemainingName(name);
                        e.setResolvedObj(binding);
                        throw e;
                }
        }
  // ---
  
        private void internalBind(Name name, Object value, Attributes attributes, 
boolean isBind) throws NamingException
        {
                String atom = name.get(0);
                Object binding = bindings.get(atom);
  
                if( name.size() == 1 )
                {
                if( binding != null && isBind == false )
                        {
                                throw new NameAlreadyBoundException("Use rebind to 
override");
                        }
  
                        // Add object to internal data structure
                        bindings.put(atom, value);
  
                        // Add attributes
                        if( attributes != null )
                        {
                                bindingAttrs.put(atom, attributes);
                    }
                }
                else
                {
                    // Intermediate name: Consume name in this context and continue
                        if( (binding instanceof Context) == false )
                        {
                                NotContextException e = new NotContextException(atom + 
" does not name a context");
                                e.setRemainingName(name);
                                e.setResolvedObj(binding);
                                throw e;
                        }
  
                        if( attributes == null )
                        {
                                Context context = (Context) binding;
                                if( isBind == true )
                                        context.bind(name.getSuffix(1), value);
                                else
                                        context.rebind(name.getSuffix(1), value);
                        }
                        else if( (binding instanceof DirContext) == false )
                        {
                                NotContextException e = new NotContextException(atom + 
" does not name a directory context that supports attributes");
                                e.setRemainingName(name);
                                e.setResolvedObj(binding);
                                throw e;
                        }
                        else
                        {
                                DirContext context = (DirContext) binding;
                                if( isBind == true )
                                        context.bind(name.getSuffix(1), value, 
attributes);
                                else
                                        context.rebind(name.getSuffix(1), value, 
attributes);
                        }
                }
        }
  }
  
  
  
  1.1                  
jbosstest/src/main/org/jboss/test/security/ejb/project/support/NameBindingIterator.java
  
  Index: NameBindingIterator.java
  ===================================================================
  package org.jboss.test.security.ejb.project.support;
  
  import java.util.Hashtable;
  import java.util.Iterator;
  import java.util.NoSuchElementException;
  import javax.naming.Binding;
  import javax.naming.Name;
  import javax.naming.NamingEnumeration;
  import javax.naming.NamingException;
  import javax.naming.directory.DirContext;
  import javax.naming.spi.DirectoryManager;
  
  /** An implementation of NamingEnumeration for listing the Bindings
   in a context. It accepts an Iterator of DirBindings and transforms
   the raw object and attributes into the output object using the
   DirectoryManager.getObjectInstance method.
  
  @see DirBinding
  @see DirectoryManager.getObjectInstance(Object,Name,Context,Hashtable,Attributes)
  
  @author [EMAIL PROTECTED]
  @version $Id: NameBindingIterator.java,v 1.1 2001/03/05 10:11:02 stark Exp $
  */
  public class NameBindingIterator implements NamingEnumeration
  {
        private Iterator bindings;
        private DirContext context;
  
        /** Creates new NameBindingIterator for enumerating a list of Bindings.
         *@param names, an Iterator of DirBindings for the raw context bindings.
         * This is the name and raw object data/attributes that should be input into 
         * DirectoryManager.getObjectInstance().
         */
      public NameBindingIterator(Iterator bindings, DirContext context)
        {
                this.bindings = bindings;
                this.context = context;
      }
  
        public void close() throws NamingException
        {
        }
  
        public boolean hasMore() throws NamingException
        {
                return bindings.hasNext();
        }
  
        public Object next() throws NamingException
        {
                DirBinding binding = (DirBinding) bindings.next();
                Object rawObject = binding.getObject();
                Name name = new DefaultName(binding.getName());
                Hashtable env = context.getEnvironment();
                try
                {
                        Object instanceObject = 
DirectoryManager.getObjectInstance(rawObject,
                                name, context, env, binding.getAttributes());
                        binding.setObject(instanceObject);
                }
                catch(Exception e)
                {
                        NamingException ne = new NamingException("getObjectInstance 
failed");
                        ne.setRootCause(e);
                        throw ne;
                }
                return binding;
        }
  
        public boolean hasMoreElements()
        {
                boolean hasMore = false;
                try
                {
                        hasMore = hasMore();
                }
                catch(NamingException e)
                {
                }
                return hasMore;
        }
        
        public Object nextElement()
        {
                Object next = null;
                try
                {
                        next = next();
                }
                catch(NamingException e)
                {
                        throw new NoSuchElementException(e.toString());
                }
                return next;
        }       
  }
  
  
  

Reply via email to