/*
 * Copyright (C) The Spice Group. All rights reserved.
 *
 * This software is published under the terms of the Spice
 * Software License version 1.1, a copy of which has been included
 * with this distribution in the LICENSE.txt file.
 */
package org.realityforge.spice.jndikit;

import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NamingException;
import javax.naming.spi.ObjectFactory;
import javax.naming.spi.StateFactory;

/**
 * This is the class to extend that provides
 * basic facilities for Namespace management.
 */
public abstract class AbstractNamespace
  implements Namespace
{
  protected ObjectFactory[] m_objectFactorySet;
  protected StateFactory[] m_stateFactorySet;

  public Object getStateToBind( final Object object,
                                final Name name,
                                final Context parent,
                                final Hashtable environment )
    throws NamingException
  {
    //for thread safety so that member variable can be updated
    //at any time
    final StateFactory[] stateFactorySet = m_stateFactorySet;

    for ( int i = 0; i < stateFactorySet.length; i++ )
    {
      final Object result =
        stateFactorySet[ i ].getStateToBind( object, name, parent, environment );

      if ( null != result )
      {
        return result;
      }
    }

    return object;
  }

  public Object getObjectInstance( final Object object,
                                   final Name name,
                                   final Context parent,
                                   final Hashtable environment )
    throws Exception
  {
    //for thread safety so that member variable can be updated
    //at any time
    final ObjectFactory[] objectFactorySet = m_objectFactorySet;

    for ( int i = 0; i < objectFactorySet.length; i++ )
    {
      final Object result =
        objectFactorySet[ i ].getObjectInstance( object, name, parent, environment );

      if ( null != result )
      {
        return result;
      }
    }

    return object;
  }

  /**
   * Utility method for subclasses to add factorys.
   *
   * @param stateFactory the StateFactory to add
   */
  protected synchronized void addStateFactory( final StateFactory stateFactory )
  {
    //create new array of factory objects
    final StateFactory[] stateFactorySet =
      new StateFactory[ m_stateFactorySet.length + 1 ];

    //copy old factory objects to new array
    System.arraycopy( m_stateFactorySet, 0, stateFactorySet, 0, m_stateFactorySet.length );

    //add in new factory at end
    stateFactorySet[ m_stateFactorySet.length ] = stateFactory;

    //update factory set
    m_stateFactorySet = stateFactorySet;
  }

  /**
   * Utility method for subclasses to add factorys.
   *
   * @param objectFactory the ObjectFactory to add
   */
  protected synchronized void addObjectFactory( final ObjectFactory objectFactory )
  {
    //create new array of factory objects
    final ObjectFactory[] objectFactorySet =
      new ObjectFactory[ m_objectFactorySet.length + 1 ];

    //copy old factory objects to new array
    System.arraycopy( m_objectFactorySet, 0, objectFactorySet, 0, m_objectFactorySet.length );

    //add in new factory at end
    objectFactorySet[ m_objectFactorySet.length ] = objectFactory;

    //update factory set
    m_objectFactorySet = objectFactorySet;
  }
}

@@ -522,19 +522,16 @@
 provides
-%0A *
  basic f
@@ -1033,39 +1033,33 @@
 e can be updated
-%0A    //
+ 
 at any time%0A    
@@ -1108,33 +1108,32 @@
 tateFactorySet;%0A
-%0A
     for ( int i 
@@ -1126,30 +1126,44 @@
   for ( 
+f
 in
-t i = 0; i %3C
+al StateFactory factory :
  stateFa
@@ -1166,36 +1166,24 @@
 teFactorySet
-.length; i++
  )%0A    %7B%0A   
@@ -1210,37 +1210,16 @@
 lt =
-%0A        stateFactorySet%5B i %5D
+ factory
 .get
@@ -1260,33 +1260,32 @@
  environment );%0A
-%0A
       if ( null 
@@ -1333,33 +1333,32 @@
 ;%0A      %7D%0A    %7D%0A
-%0A
     return objec
@@ -1682,23 +1682,17 @@
  updated
-%0A    //
+ 
 at any t
@@ -1760,17 +1760,16 @@
 orySet;%0A
-%0A
     for 
@@ -1774,22 +1774,37 @@
 r ( 
+f
 in
-t i = 0; i %3C
+al ObjectFactory factory :
  obj
@@ -1820,20 +1820,8 @@
 ySet
-.length; i++
  )%0A 
@@ -1856,38 +1856,16 @@
 lt =
-%0A        objectFactorySet%5B i %5D
+ factory
 .get
@@ -1913,25 +1913,24 @@
 ironment );%0A
-%0A
       if ( n
@@ -1990,17 +1990,16 @@
 %7D%0A    %7D%0A
-%0A
     retu
@@ -2060,33 +2060,34 @@
 es to add factor
-y
+ie
 s.%0A   *%0A   * @pa
@@ -2298,30 +2298,24 @@
 FactorySet =
-%0A     
  new StateFa
@@ -2713,17 +2713,18 @@
 d factor
-y
+ie
 s.%0A   *%0A
@@ -2954,22 +2954,16 @@
 orySet =
-%0A     
  new Obj

Fix some spelling mistakes and update to use for-each style loops