+#include <string.h>
+#include <stdlib.h>
+#include <cspi/spi-private.h>
+
+#undef DEBUG_OBJECTS
+
+static CORBA_Environment ev = { 0 };
+static Accessibility_Registry registry = CORBA_OBJECT_NIL;
+static GHashTable *live_refs = NULL;
+
+static guint
+cspi_object_hash (gconstpointer key)
+{
+ CORBA_Object object = (CORBA_Object) key;
+ guint retval;
+
+ retval = CORBA_Object_hash (object, 0, &ev);
+
+ return retval;
+}
+
+static gboolean
+cspi_object_equal (gconstpointer a, gconstpointer b)
+{
+ CORBA_Object objecta = (CORBA_Object) a;
+ CORBA_Object objectb = (CORBA_Object) b;
+ gboolean retval;
+
+ retval = CORBA_Object_is_equivalent (objecta, objectb, &ev);
+
+ return retval;
+}
+
+static void
+cspi_object_release (gpointer value)
+{
+ Accessible *a = (Accessible *) value;
+
+#ifdef DEBUG_OBJECTS
+ g_print ("releasing %p => %p\n", a, a->objref);
+#endif
+
+ cspi_release_unref (a->objref);
+
+ memset (a, 0xaa, sizeof (Accessible));
+ a->ref_count = -1;
+
+#ifndef DEBUG_OBJECTS
+ free (a);
+#endif
+}
+
+SPIBoolean
+cspi_accessible_is_a (Accessible *obj,
+ const char *interface_name)
+{
+ SPIBoolean retval;
+ Bonobo_Unknown unknown;
+
+ if (obj == NULL)
+ {
+ return FALSE;
+ }
+
+ unknown = Bonobo_Unknown_queryInterface (CSPI_OBJREF (obj),
+ interface_name, cspi_ev ());
+
+ if (ev._major != CORBA_NO_EXCEPTION)
+ {
+ g_error ("Exception '%s' checking if is '%s'",
+ cspi_exception_get_text (),
+ interface_name);
+ }
+
+ if (unknown != CORBA_OBJECT_NIL)
+ {
+ retval = TRUE;
+ cspi_release_unref (unknown);
+ }
+ else
+ {
+ retval = FALSE;
+ }
+
+ return retval;
+}
+
+static GHashTable *
+cspi_get_live_refs (void)
+{
+ if (!live_refs)
+ {
+ live_refs = g_hash_table_new_full (cspi_object_hash,
+ cspi_object_equal,
+ NULL,
+ cspi_object_release);
+ }
+ return live_refs;
+}
+
+CORBA_Environment *
+cspi_ev (void)
+{
+ /* This method is an ugly hack */
+ return &ev;
+}
+
+Accessibility_Registry
+cspi_registry (void)
+{
+ return registry;
+}
+
+SPIBoolean
+cspi_exception (void)
+{
+ SPIBoolean retval;
+
+ if (ev._major != CORBA_NO_EXCEPTION)
+ {
+ CORBA_exception_free (&ev);
+ retval = TRUE;
+ }
+ else
+ {
+ retval = FALSE;
+ }
+
+ return retval;
+}
+
+Accessible *
+cspi_object_add (CORBA_Object corba_object)
+{
+ Accessible *ref;
+
+ if (corba_object == CORBA_OBJECT_NIL)
+ {
+ ref = NULL;
+ }
+ else if (!cspi_check_ev ("pre method check"))
+ {
+ ref = NULL;
+ }
+ else
+ {
+ if ((ref = g_hash_table_lookup (cspi_get_live_refs (), corba_object)))
+ {
+ g_assert (ref->ref_count > 0);
+ ref->ref_count++;
+ cspi_release_unref (corba_object);
+#ifdef DEBUG_OBJECTS
+ g_print ("returning cached %p => %p\n", ref, ref->objref);
+#endif
+ }
+ else
+ {
+ ref = malloc (sizeof (Accessible));
+
+#ifdef DEBUG_OBJECTS
+ g_print ("allocating %p => %p\n", ref, corba_object);
+#endif
+
+ ref->objref = corba_object;
+ ref->ref_count = 1;
+
+ g_hash_table_insert (cspi_get_live_refs (), ref->objref, ref);
+ }
+ }
+
+ return ref;
+}
+
+void
+cspi_object_ref (Accessible *accessible)
+{
+ g_return_if_fail (accessible != NULL);
+
+ accessible->ref_count++;
+}
+
+void
+cspi_object_unref (Accessible *accessible)
+{
+ if (accessible == NULL)
+ {
+ return;
+ }
+
+ if (--accessible->ref_count == 0)
+ {
+ g_hash_table_remove (cspi_get_live_refs (), accessible->objref);
+ }
+}
+
+static void
+cspi_cleanup (void)
+{
+ GHashTable *refs;
+
+ refs = live_refs;
+ live_refs = NULL;
+ if (refs)
+ {
+ g_hash_table_destroy (refs);
+ }
+
+ if (registry != CORBA_OBJECT_NIL)
+ {
+ cspi_release_unref (registry);
+ registry = CORBA_OBJECT_NIL;
+ }
+}
+
+static gboolean SPI_inited = FALSE;