/*
 * 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.app;

import java.lang.reflect.*;
import org.w3c.dom.*;
import com.silverwrist.util.xml.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;

class ConnectionPoint implements NamedObject, InvocationHandler, ComponentShutdown
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_name;                // name of this connection point
  private Class m_interface;            // interface associated with this connection point
  private Object m_proxy_obj;           // proxy object that provides the "head end" of the cpoint
  private Object m_target_obj = null;   // object on the "tail end" of the cpoint

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

  ConnectionPoint(Element config) throws ConfigException
  {
    XMLLoader loader = XMLLoader.get();
    String iface_name = null;
    try
    { // verify the right node name
      loader.verifyNodeName(config,"connection-point");

      // get the connection point's name
      m_name = loader.getAttribute(config,"name");

      // get the interface represented by this proxy
      iface_name = loader.getAttribute(config,"interface");
      m_interface = Class.forName(iface_name);
      if (!(m_interface.isInterface()))
      { // the class name does not specify an interface
	ConfigException ce = new ConfigException(ConnectionPoint.class,"ConnectionMessages",
						 "cpoint.notValid");
	ce.setParameter(0,iface_name);
	throw ce;

      } // end if

      // Create the proxy object.
      Class[] arg = new Class[1];
      arg[0] = m_interface;
      m_proxy_obj = Proxy.newProxyInstance(m_interface.getClassLoader(),arg,this);

    } // end try
    catch (XMLLoadException e)
    { // error loading XML config data
      throw new ConfigException(e);

    } // end catch
    catch (ClassNotFoundException e)
    { // unable to find the interface
      ConfigException ce = new ConfigException(ConnectionPoint.class,"ConnectionMessages",
					       "cpoint.noInterface",e);
      ce.setParameter(0,iface_name);
      throw ce;

    } // end catch

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface NamedObject
   *--------------------------------------------------------------------------------
   */

  public String getName()
  {
    return m_name;

  } // end getName

  /*--------------------------------------------------------------------------------
   * Implementations from interface InvocationHandler
   *--------------------------------------------------------------------------------
   */

  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
  {
    if (m_target_obj==null)
      throw new ProxyException(new NullPointerException("target not yet connected for " + m_name + "/"
							+ m_interface.getName()));
    try
    { // invoke the method on the real target!
      return method.invoke(m_target_obj,args);

    } // end try
    catch (IllegalAccessException e)
    { // turn into the runtime ProxyException
      throw new ProxyException(e);

    } // end catch
    catch (InvocationTargetException e)
    { // "unwrap" this exception type and throw it
      throw e.getCause();

    } // end catch

  } // end invoke

  /*--------------------------------------------------------------------------------
   * Implementations from interface ComponentShutdown
   *--------------------------------------------------------------------------------
   */

  public void shutdown()
  {
    m_proxy_obj = null;
    m_target_obj = null;

  } // end shutdown

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

  Object getInterface(Class expected_class) throws ConfigException
  {
    if (expected_class.isAssignableFrom(m_interface))
      return m_proxy_obj;
    ConfigException ce = new ConfigException(ConnectionPoint.class,"ConnectionMessages","iface.type");
    ce.setParameter(0,m_name);
    ce.setParameter(1,m_interface.getName());
    ce.setParameter(2,expected_class.getName());
    throw ce;

  } // end getInterface

  synchronized void connectObject(Object obj) throws ConfigException
  {
    if (m_target_obj!=null)
    { // this connection point has already been connected
      ConfigException ce = new ConfigException(ConnectionPoint.class,"ConnectionMessages","connect.already");
      ce.setParameter(0,m_name);
      throw ce;

    } // end if

    if (!(m_interface.isInstance(obj)))
    { // the object is of the wrong type
      ConfigException ce = new ConfigException(ConnectionPoint.class,"ConnectionMessages","connect.wrongType");
      ce.setParameter(0,m_name);
      ce.setParameter(1,m_interface.getName());
      throw ce;
      
    } // end if

    m_target_obj = obj;

  } // end connectObject

} // end class ConnectionPoint
