2 //=============================================================================
6 * This program verifies that a POA using the NON_RETAIN policy creates
7 * unique system IDs, also that objects created with a USER id policy is
10 * @author Phil Mesnier
12 //=============================================================================
16 #include "ace/SString.h"
17 #include "tao/PortableServer/POA_Current.h"
19 class ID_Check_i
: public POA_ID_Check
25 void set_id (int index
, PortableServer::ObjectId_var oid
);
27 CORBA::Boolean
check_servant_id (CORBA::Short index
);
31 PortableServer::ObjectId_var
* oids_
;
32 PortableServer::Current_var current_
;
35 ID_Check_i::ID_Check_i (int num
)
42 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
43 CORBA::Object_var obj
= orb
->resolve_initial_references ("POACurrent");
44 current_
= PortableServer::Current::_narrow (obj
.in());
46 oids_
= new PortableServer::ObjectId_var
[num
];
49 ID_Check_i::~ID_Check_i ()
55 ID_Check_i::set_id (int index
, PortableServer::ObjectId_var oid
)
57 this->oids_
[index
] = oid
;
61 ID_Check_i::check_servant_id (CORBA::Short index
)
63 if (index
< 0 || index
>= this->count_
)
66 ACE_TEXT ("check_servant_id: requested index outside of range\n")));
69 PortableServer::ObjectId_var oid
= this->current_
->get_object_id();
72 ACE_TEXT ("check_servant_id: index = %d current objectid ")
73 ACE_TEXT ("len %d octets: "),
74 index
, oid
->length()));
75 for (size_t i
= 0; i
< oid
->length () && i
< 16; i
++)
76 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT("%02x "), oid
[i
]));
77 ACE_DEBUG ((LM_DEBUG
,ACE_TEXT ("\n")));
79 if (oid
->length() != this->oids_
[index
]->length())
81 for (size_t i
= 0; i
< oid
->length(); i
++)
83 if (oid
[i
] != this->oids_
[index
][i
])
86 ACE_TEXT ("check_servant_id: id[%d] mismatch at position %d, ")
87 ACE_TEXT ("got %d, expected %d\n"),
88 index
, i
, oid
[i
], this->oids_
[index
][i
]));
96 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
100 // Initialize the ORB.
101 CORBA::ORB_var orb
= CORBA::ORB_init (argc
, argv
);
103 // Obtain the RootPOA.
104 CORBA::Object_var object
=
105 orb
->resolve_initial_references ("RootPOA");
108 PortableServer::POA_var root_poa
=
109 PortableServer::POA::_narrow (object
.in ());
111 // Get the POAManager of the RootPOA.
112 PortableServer::POAManager_var poa_manager
=
113 root_poa
->the_POAManager ();
115 // Policies for the new POA.
116 CORBA::PolicyList
policies (4);
119 // Request Processing Policy.
121 root_poa
->create_request_processing_policy (PortableServer::USE_DEFAULT_SERVANT
);
123 // Id Uniqueness Policy.
125 root_poa
->create_id_uniqueness_policy (PortableServer::MULTIPLE_ID
);
127 // Servant Retention Policy.
129 root_poa
->create_servant_retention_policy (PortableServer::NON_RETAIN
);
131 // Create POA to host default servant.
132 ACE_CString name
= "System IDs";
133 PortableServer::POA_var sys_id_poa
=
134 root_poa
->create_POA (name
.c_str (),
140 root_poa
->create_id_assignment_policy (PortableServer::USER_ID
);
143 PortableServer::POA_var user_id_poa
=
144 root_poa
->create_POA (name
.c_str (),
149 for (CORBA::ULong i
= 0; i
< policies
.length (); ++i
)
151 CORBA::Policy_ptr policy
= policies
[i
];
157 ID_Check_i
*servant
= new ID_Check_i (objcount
);
158 sys_id_poa
->set_servant (servant
);
159 user_id_poa
->set_servant (servant
);
161 // Activate POA manager.
162 poa_manager
->activate ();
164 // Create object array.
165 ID_Check_var
*refs
= new ID_Check_var
[objcount
];
166 PortableServer::ObjectId_var obj_id
;
167 char *user_id_str
= new char[10];
169 for (int i
= 0; i
< objcount
; i
++)
171 if (i
< objcount
/ 2)
173 object
= sys_id_poa
->create_reference ("IDL:ID_Check:1.0");
174 refs
[i
] = ID_Check::_narrow (object
.in());
175 obj_id
= sys_id_poa
->reference_to_id (object
.in());
176 servant
->set_id (i
, obj_id
);
180 ACE_OS::sprintf (user_id_str
, "id %d", i
);
181 obj_id
= PortableServer::string_to_ObjectId (user_id_str
);
182 object
= user_id_poa
->create_reference_with_id (obj_id
.in(),
184 refs
[i
] = ID_Check::_narrow (object
.in());
185 servant
->set_id (i
, obj_id
);
190 for (int i
= 0; i
< objcount
; i
++)
192 successes
+= refs
[i
]->check_servant_id (i
) ? 1 : 0;
197 if (successes
!= objcount
)
199 ACE_ERROR ((LM_ERROR
,
200 ACE_TEXT ("FAILURE: only got %d ids correct out of %d\n"),
201 successes
, objcount
));
204 ACE_DEBUG ((LM_DEBUG
, ACE_TEXT ("Success: All ids match\n")));
206 catch (const CORBA::Exception
& ex
)
208 ex
._tao_print_exception ("Exception caught");