/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.obj;

import java.util.*;
import com.silverwrist.dynamo.iface.*;

public class JavaObjectBindery
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static JavaObjectBindery _self = null;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private HashMap m_class_to_dc;    // Class object to DynamicClass
  private HashMap m_name_to_dc;     // classname to DynamicClass

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  private JavaObjectBindery()
  {
    m_class_to_dc = new HashMap();
    m_name_to_dc = new HashMap();

  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public synchronized DynamicClass lookupClass(Class klass)
  {
    DynamicClass rc = (DynamicClass)(m_class_to_dc.get(klass));
    if (rc==null)
    { // create new class object and register it
      rc = new JavaDynamicClass(klass);
      m_class_to_dc.put(klass,rc);
      m_name_to_dc.put(klass.getName(),rc);

    } // end if

    return rc;

  } // end lookupClass

  public synchronized DynamicClass lookupClass(String name)
  {
    DynamicClass rc = (DynamicClass)(m_name_to_dc.get(name));
    if (rc==null)
    { // class not present, try loading it
      try
      { // but we have to load the regular class first
	Class klass = Class.forName(name);
	rc = new JavaDynamicClass(klass);
	m_class_to_dc.put(klass,rc);
	m_name_to_dc.put(name,rc);

      } // end try
      catch (ClassNotFoundException e)
      { // no class found...
	return null;  // TODO: exception?

      } // end catch

    } // end if

    return rc;

  } // end lookupClass

  public synchronized void bind(String name, Class klass)
  {
    DynamicClass dc = new JavaDynamicClass(klass);
    m_class_to_dc.put(klass,dc);
    m_name_to_dc.put(name,dc);
    
  } // end bind

  public synchronized void bind(Class klass)
  {
    bind(klass.getName(),klass);

  } // end bind

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  public static synchronized JavaObjectBindery get()
  {
    if (_self==null)
      _self = new JavaObjectBindery();
    return _self;

  } // end get

} // end class JavaObjectBindery


