PortableServer_POA
server_create_multi_threaded_poa (CORBA_ORB                  orb, 
				  PortableServer_POA         poa,
				  PortableServer_POAManager  poa_mgr,
				  CORBA_Environment         *ev)
{
	const static        MAX_POLICIES  = 1;
	PortableServer_POA  child_poa     = CORBA_OBJECT_NIL;
	CORBA_PolicyList   *poa_policies;

	poa_policies           = CORBA_PolicyList__alloc ();
        poa_policies->_maximum = MAX_POLICIES;
        poa_policies->_length  = MAX_POLICIES;
        poa_policies->_buffer  = CORBA_PolicyList_allocbuf (MAX_POLICIES);
        CORBA_sequence_set_release (poa_policies, CORBA_TRUE);
                                                                                
        poa_policies->_buffer[0] = (CORBA_Policy)
		PortableServer_POA_create_thread_policy (
			poa,
			PortableServer_ORB_CTRL_MODEL,
			ev);

	child_poa = PortableServer_POA_create_POA (poa,
                                                   "Thread Per Request POA",
                                                   poa_mgr,
                                                   poa_policies,
                                                   ev);
	if (etk_raised_exception(ev)) 
		goto failed_create_poa;

	ORBit_ObjectAdaptor_set_thread_hint ((ORBit_ObjectAdaptor) child_poa, 
					     ORBIT_THREAD_HINT_PER_REQUEST);

	
        CORBA_Policy_destroy (poa_policies->_buffer[0], ev); 
	if (etk_raised_exception(ev)) 
		goto failed;
        CORBA_free (poa_policies);	
	
	return child_poa;
	
 failed_create_poa:
	/* FIXME, in case of error, ev is set, but destructor should not
	 * return except anyway */
        CORBA_Policy_destroy (poa_policies->_buffer[0], ev); 
        CORBA_free (poa_policies);	
 failed:
	return CORBA_OBJECT_NIL;
}
static PortableServer_POA
create_mult_id_poa (CORBA_Environment *ev)
{
	PortableServer_POA  rootpoa;
	PortableServer_POA  retval;
	CORBA_PolicyList   *policies;

	rootpoa = (PortableServer_POA)
		CORBA_ORB_resolve_initial_references (orb, "RootPOA", ev);
	g_assert (ev->_major == CORBA_NO_EXCEPTION);

	policies           = CORBA_PolicyList__alloc ();
	policies->_maximum = 1;
	policies->_length  = 1;
	policies->_buffer  = CORBA_PolicyList_allocbuf (1);
	CORBA_sequence_set_release (policies, CORBA_TRUE);

	policies->_buffer[0] = (CORBA_Policy)
					PortableServer_POA_create_id_uniqueness_policy (
							rootpoa,
							PortableServer_MULTIPLE_ID,
							ev);
	g_assert (ev->_major == CORBA_NO_EXCEPTION);

	retval = PortableServer_POA_create_POA (rootpoa, "Multiple Id POA",
					        NULL, policies, ev);
	g_assert (ev->_major == CORBA_NO_EXCEPTION);

	CORBA_Policy_destroy (policies->_buffer[0], ev);
	CORBA_free (policies);

	g_assert (ev->_major == CORBA_NO_EXCEPTION);

	CORBA_Object_release ((CORBA_Object) rootpoa, ev);

	g_assert (ev->_major == CORBA_NO_EXCEPTION);

	return retval;
}
poatest
poatest_run (PortableServer_POA        rootpoa,
             PortableServer_POAManager rootpoa_mgr)
{
	CORBA_Environment       ev;
	poatest                 poatest_obj1, poatest_obj2;
	CORBA_PolicyList       *poa_policies;

	CORBA_exception_init( &ev );
 
	/*
	 * Create child POA with MULTIPLE_ID Object Id Uniqueness policy and
	 * IMPLICIT_ACTIVATION Implicit Activation policy.
	 */
	poa_policies           = CORBA_PolicyList__alloc ();
	poa_policies->_maximum = 2;
	poa_policies->_length  = 2;
	poa_policies->_buffer  = CORBA_PolicyList_allocbuf (2);
	CORBA_sequence_set_release (poa_policies, CORBA_TRUE);

	poa_policies->_buffer[0] = (CORBA_Policy)
					PortableServer_POA_create_id_uniqueness_policy (
							rootpoa,
							PortableServer_MULTIPLE_ID,
							&ev);

	poa_policies->_buffer[1] = (CORBA_Policy)
					PortableServer_POA_create_implicit_activation_policy (
							rootpoa,
							PortableServer_IMPLICIT_ACTIVATION,
							&ev);

	child_poa = PortableServer_POA_create_POA (rootpoa,
						   "Multiple Id POA",
						   rootpoa_mgr,
						   poa_policies,
						   &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("create_POA : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	CORBA_Policy_destroy (poa_policies->_buffer[0], &ev);
	CORBA_Policy_destroy (poa_policies->_buffer[1], &ev);
	CORBA_free (poa_policies);

	/*
	 * Initialise the servant.
	 */
	POA_poatest__init (&poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POA_poatest__init : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Implicitly activate two objects.
	 */
	poatest_obj1 = PortableServer_POA_servant_to_reference (child_poa, 
								&poatest_servant, 
								&ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("servant_to_reference : ", &ev);
		return CORBA_OBJECT_NIL;
	}
	CORBA_Object_release (poatest_obj1, &ev);

	poatest_obj2 = PortableServer_POA_servant_to_reference (child_poa, 
								&poatest_servant, 
								&ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("servant_to_reference : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Activate the POAManager. POA will now accept requests
	 */
	PortableServer_POAManager_activate (rootpoa_mgr, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POAManager_activate : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	return poatest_obj2;
}
poatest
poatest_run (PortableServer_POA        rootpoa,
             PortableServer_POAManager rootpoa_mgr)
{
	CORBA_Environment        ev;
	poatest                  poatest_obj;
	CORBA_PolicyList        *poa_policies;
	PortableServer_ObjectId *objid;

	CORBA_exception_init (&ev);
 
	/*
	 * Create child POA with USER_ID Id Assignment and PERSISTENT lifespan policy.
	 */
	poa_policies           = CORBA_PolicyList__alloc ();
	poa_policies->_maximum = 2;
	poa_policies->_length  = 2;
	poa_policies->_buffer  = CORBA_PolicyList_allocbuf (2);
	CORBA_sequence_set_release (poa_policies, CORBA_TRUE);

	poa_policies->_buffer[0] = (CORBA_Policy)
					PortableServer_POA_create_id_assignment_policy (
							rootpoa,
							PortableServer_USER_ID,
							&ev);

	poa_policies->_buffer[1] = (CORBA_Policy)
					PortableServer_POA_create_lifespan_policy (
							rootpoa,
							PortableServer_PERSISTENT,
							&ev);

	child_poa = PortableServer_POA_create_POA (rootpoa,
						   "User Id and Persistent POA",
						   rootpoa_mgr,
						   poa_policies,
						   &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("create_POA : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Initialise the servant.
	 */
	POA_poatest__init (&poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POA_poatest__init : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Set up the ObjectId.
	 */
	objid = PortableServer_string_to_ObjectId ("Test Id", &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("string_to_ObjectId : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Activate object with user assigned Id.
	 */
	PortableServer_POA_activate_object_with_id (child_poa, objid, &poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("activate_object_with_id : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Get reference for object.
	 */
	poatest_obj = PortableServer_POA_servant_to_reference (child_poa, &poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("servant_to_reference : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Deactivate object and destroy POA
	 */
	PortableServer_POA_deactivate_object(child_poa, objid, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("deactivate_object : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	PortableServer_POA_destroy (child_poa, CORBA_FALSE, CORBA_FALSE, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POA_destroy : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Re-create POA and re-activate the object.
	 */
	child_poa = PortableServer_POA_create_POA (rootpoa,
						   "User Id and Persistent POA",
						   rootpoa_mgr,
						   poa_policies,
						   &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("create_POA : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	PortableServer_POA_activate_object_with_id (child_poa, objid, &poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("activate_object_with_id : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Reference should still be valid.
	 */

	CORBA_free (objid);

	CORBA_Policy_destroy (poa_policies->_buffer[0], &ev);
	CORBA_Policy_destroy (poa_policies->_buffer[1], &ev);
	CORBA_free (poa_policies);

	/*
	 * Activate the POAManager. POA will now accept requests
	 */
	PortableServer_POAManager_activate( rootpoa_mgr, &ev );
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POAManager_activate : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	return poatest_obj;
}
poatest
poatest_run (PortableServer_POA        rootpoa,
             PortableServer_POAManager rootpoa_mgr)
{
	CORBA_Environment        ev;
	poatest                  poatest_obj;
	CORBA_PolicyList        *poa_policies;
	PortableServer_ObjectId *objid;

	CORBA_exception_init (&ev);
 
	/*
	 * Create child POA with USER_ID Id Assignment policy.
	 */
	poa_policies           = CORBA_PolicyList__alloc ();
	poa_policies->_maximum = 1;
	poa_policies->_length  = 1;
	poa_policies->_buffer  = CORBA_PolicyList_allocbuf (1);
	CORBA_sequence_set_release (poa_policies, CORBA_TRUE);

	poa_policies->_buffer[0] = (CORBA_Policy)
					PortableServer_POA_create_id_assignment_policy (
							rootpoa,
							PortableServer_USER_ID,
							&ev);

	child_poa = PortableServer_POA_create_POA (rootpoa,
						   "User Id POA",
						   rootpoa_mgr,
						   poa_policies,
						   &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("create_POA : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	CORBA_Policy_destroy (poa_policies->_buffer[0], &ev);
	CORBA_free (poa_policies);

	/*
	 * Initialise the servant.
	 */
	POA_poatest__init (&poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POA_poatest__init : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Set up the ObjectId.
	 */
	objid = PortableServer_string_to_ObjectId ("Test Id", &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("string_to_ObjectId : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Activate object with user assigned Id.
	 */
	PortableServer_POA_activate_object_with_id (child_poa, objid, &poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("activate_object_with_id : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	CORBA_free (objid);

	poatest_obj = PortableServer_POA_servant_to_reference (child_poa, &poatest_servant, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("servant_to_reference : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	/*
	 * Activate the POAManager. POA will now accept requests
	 */
	PortableServer_POAManager_activate (rootpoa_mgr, &ev);
	if (POATEST_EX (&ev)) {
		POATEST_PRINT_EX ("POAManager_activate : ", &ev);
		return CORBA_OBJECT_NIL;
	}

	return poatest_obj;
}
poatest
poatest_run (PortableServer_POA        rootpoa,
             PortableServer_POAManager rootpoa_mgr )
{
    CORBA_Environment        ev;
    poatest                  poatest_obj;
    CORBA_PolicyList        *poa_policies;
    PortableServer_ObjectId *objid;

    CORBA_exception_init (&ev);

    /*
     * Create child POA with RETAIN servant retention policy, USE_DEFAULT_SERVANT
     * request processing policy and MULTIPLE_ID id uniqueness policy.
     */
    poa_policies           = CORBA_PolicyList__alloc ();
    poa_policies->_maximum = 3;
    poa_policies->_length  = 3;
    poa_policies->_buffer  = CORBA_PolicyList_allocbuf (3);
    CORBA_sequence_set_release (poa_policies, CORBA_TRUE);

    poa_policies->_buffer[0] = (CORBA_Policy)
                               PortableServer_POA_create_id_uniqueness_policy (
                                   rootpoa,
                                   PortableServer_MULTIPLE_ID,
                                   &ev);

    poa_policies->_buffer[1] = (CORBA_Policy)
                               PortableServer_POA_create_request_processing_policy (
                                   rootpoa,
                                   PortableServer_USE_DEFAULT_SERVANT,
                                   &ev);

    poa_policies->_buffer[2] = (CORBA_Policy)
                               PortableServer_POA_create_servant_retention_policy (
                                   rootpoa,
                                   PortableServer_RETAIN,
                                   &ev);

    child_poa = PortableServer_POA_create_POA (rootpoa,
                "Default Servant POA",
                rootpoa_mgr,
                poa_policies,
                &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("create_POA : ", &ev);
        return CORBA_OBJECT_NIL;
    }

    CORBA_Policy_destroy (poa_policies->_buffer[0], &ev);
    CORBA_Policy_destroy (poa_policies->_buffer[1], &ev);
    CORBA_Policy_destroy (poa_policies->_buffer[2], &ev);
    CORBA_free (poa_policies);

    /*
     * Initialise the servant.
     */
    POA_poatest__init (&poatest_servant, &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("POA_poatest__init : ", &ev);
        return CORBA_OBJECT_NIL;
    }

    /*
     * Register the default servant.
     */
    PortableServer_POA_set_servant (child_poa, &poatest_servant, &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("set_servant : ", &ev);
        return CORBA_OBJECT_NIL;
    }

    /*
     * Activate an object.
     */
    objid = PortableServer_POA_activate_object (child_poa, &poatest_servant, &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("activate_object : ", &ev);
        return CORBA_OBJECT_NIL;
    }
    CORBA_free (objid);

    /*
     * Create a reference for an inactive object.
     */
    poatest_obj = PortableServer_POA_create_reference (child_poa, "IDL:poatest:1.0", &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("create_reference : ", &ev);
        return CORBA_OBJECT_NIL;
    }

    /*
     * Activate the POAManager. POA will now accept requests
     */
    PortableServer_POAManager_activate (rootpoa_mgr, &ev);
    if (POATEST_EX (&ev)) {
        POATEST_PRINT_EX ("POAManager_activate : ", &ev);
        return CORBA_OBJECT_NIL;
    }

    return poatest_obj;
}